Language: C++

#include<bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
#define int long long
#define ld long double
#define sp fixed<<setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(),x.end()
#define pii pair<long long,long long>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

const int M = (1e9)+7;
const int N = 500005;
const int B = 700;

int mp[N];

struct data{
    int l,r,i;
};

bool cmp(data &a, data &b)
{
    return (a.l/B < b.l/B) || (a.l/B == b.l/B && a.r < b.r);
}

signed main()
{
    FAST
    int tc=1;
    //cin>>tc;
    for(int ti=1;ti<=tc;ti++)
    {
        int n,q;
        cin>>n>>q;
        int a[n];
        for(int i=0;i<n;i++)cin>>a[i];
        vector<data> queries(q);
        int ans[q];
        for(int i=0;i<q;i++)
        {
            cin>>queries[i].l>>queries[i].r;
            queries[i].i = i;
            queries[i].l--;
            queries[i].r--;
        }
        sort(all(queries),cmp);
        int L = queries[0].l;
        int R = queries[0].r;
        set<int> s;
        for(int i=L;i<=R;i++)
        {
            mp[a[i]]++;
            s.insert(a[i]);
        }
        ans[queries[0].i] = s.size();
        for(int i=1;i<q;i++)
        {
            int l = queries[i].l;
            int r = queries[i].r;
            int ind = queries[i].i;
            while(L < l)
            {
                mp[a[L]]--;
                if(!mp[a[L]])s.erase(s.find(a[L]));
                L++;
            }
            while(L > l)
            {
                L--;
                mp[a[L]]++;
                s.insert(a[L]);
            }
            while(R > r)
            {
                mp[a[R]]--;
                if(!mp[a[R]])s.erase(s.find(a[R]));
                R--;
            }
            while(R < r)
            {
                R++;
                mp[a[R]]++;
                s.insert(a[R]);
            }
            ans[ind] = s.size();
        }
        for(int i=0;i<q;i++)cout<<ans[i]<<endl;
    }
    return 0;
}


Compiler output:
a.cc:26:10: error: reference to 'data' is ambiguous
   26 | bool cmp(data &a, data &b)
      |          ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:22:8: note:                 'struct data'
   22 | struct data{
      |        ^~~~
a.cc:26:16: error: 'a' was not declared in this scope
   26 | bool cmp(data &a, data &b)
      |                ^
a.cc:26:19: error: reference to 'data' is ambiguous
   26 | bool cmp(data &a, data &b)
      |                   ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:22:8: note:                 'struct data'
   22 | struct data{
      |        ^~~~
a.cc:26:25: error: 'b' was not declared in this scope
   26 | bool cmp(data &a, data &b)
      |                         ^
a.cc:26:26: error: expression list treated as compound expression in initializer [-fpermissive]
   26 | bool cmp(data &a, data &b)
      |                          ^
a.cc: In function 'int main()':
a.cc:42:20: error: template argument 1 is invalid
   42 |         vector<data> queries(q);
      |                    ^
a.cc:42:20: error: template argument 2 is invalid
a.cc:46:25: error: invalid types 'int[long long int]' for array subscript
   46 |             cin>>queries[i].l>>queries[i].r;
      |                         ^
a.cc:46:39: error: invalid types 'int[long long int]' for array subscript
   46 |             cin>>queries[i].l>>queries[i].r;
      |                                       ^
a.cc:47:20: error: invalid types 'int[long long int]' for array subscript
   47 |             queries[i].i = i;
      |                    ^
a.cc:48:20: error: invalid types 'int[long long int]' for array subscript
   48 |             queries[i].l--;
      |                    ^
a.cc:49:20: error: invalid types 'int[long long int]' for array subscript
   49 |             queries[i].r--;
      |                    ^
a.cc:12:18: error: request for member 'begin' in 'queries', which is of non-class type 'int'
   12 | #define all(x) x.begin(),x.end()
      |                  ^~~~~
a.cc:51:14: note: in expansion of macro 'all'
   51 |         sort(all(queries),cmp);
      |              ^~~
a.cc:12:28: error: request for member 'end' in 'queries', which is of non-class type 'int'
   12 | #define all(x) x.begin(),x.end()
      |                            ^~~
a.cc:51:14: note: in expansion of macro 'all'
   51 |         sort(all(queries),cmp);
      |              ^~~
a.cc:52:24: error: invalid types 'int[int]' for array subscript
   52 |         int L = queries[0].l;
      |                        ^
a.cc:53:24: error: invalid types 'int[int]' for array subscript
   53 |         int R = queries[0].r;
      |                        ^
a.cc:60:20: error: invalid types 'int[int]' for array subscript
   60 |         ans[queries[0].i] = s.size();
      |                    ^
a.cc:63:28: error: invalid types 'int[long long int]' for array subscript
   63 |             int l = queries[i].l;
      |                            ^
a.cc:64:28: error: invalid types 'int[long long int]' for array subscript
   64 |             int r = queries[i].r;
      |                            ^
a.cc:65:30: error: invalid types 'int[long long int]' for array subscript
   65 |             int ind = queries[i].i;
      |                              ^