Language: C++

#include <iostream>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <cstring>
#include <string.h>
#include <sstream>
#include <iomanip>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <tuple>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

#define F first
#define S second
#define siz(v) ((int)v.size())
#define rs(n) resize(n)
#define ALL(v) v.begin(),v.end()
#define reset(v) memset((v),0,sizeof(v))
#define EB emplace_back
#define MP make_pair
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,a,b) for(int i=(a);i<=(b);i++)
#define debug(x) cout << " > " << #x << ':' << x << endl;
#define kiyohime ios::sync_with_stdio(false);cin.tie(0);
#define endl '\n'

using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T> using vec = vector<T>;
template<typename T> using Prior = priority_queue<T>;
template<typename T> using prior = priority_queue<T, vec<T>, greater<T>>;

const int INF = 1e9;
const ll LLINF = (ll)4*1e18;
const ll MOD = 1e9+7;
const double PI  = 3.14159265358;
const double EPS = 1e-8;
const int xx[8] = {0,1,0,-1,1,1,-1,-1};
const int yy[8] = {1,0,-1,0,1,-1,-1,1};

void pmod(ll &a, ll b) {a = (a+b)%MOD;}
void mmod(ll &a, ll b) {a = (a-b+MOD)%MOD;}
void tmod(ll &a, ll b) {a = (a*b)%MOD;}
ll POW(ll a, ll b) {ll res=1; do{if(b%2)tmod(res,a);tmod(a,a);}while(b>>=1); return res;}
template<typename T> void amax(T &a, T b) {if(a < b) a = b;}
template<typename T> void amin(T &a, T b) {if(a > b) a = b;}

#define N 500010
#define sqN 710

struct data{
    int l, r, idx, ans;
};

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

bool cmp2(data a, data b){
    return a.idx < b.idx;
}

int cnt[N], tot;

void solve(){
    int n, m;
    
    cin >> n >> m;
    vec<int> v(n);
    for(auto &i:v) cin >> i;
    
    vec<data> q(m);
    rep(i, m) {
        cin >> q[i].l >> q[i].r;
        q[i].l--; q[i].r--;
        q[i].idx = i;
    }
    
    sort(ALL(q), cmp1);
    
    int l, r;
    l = r = 0;
    for(auto &i:q){
        while(r <= i.r){
            if(cnt[v[r]] == 0) tot++;
            cnt[v[r++]]++;
        }
        while(r > i.r+1){
            cnt[v[--r]]--;
            if(cnt[v[r]] == 0) tot--;
        }
        while(l < i.l){
            if(cnt[v[l]] == 1) tot--;
            cnt[v[l++]]--;
        }
        while(l > i.l){
            cnt[v[--l]]++;
            if(cnt[v[l]] == 1) tot++;
        }
        
        i.ans = tot;
        
//        cout << '#' << i.l << ' ' << i.r << ' ' << l << ' ' << r << endl;
    }
    
    sort(ALL(q), cmp2);
    
    for(auto i:q) cout << i.ans << endl;
}

int main(){
//    kiyohime
    int T = 1;
    
//    cin >> T;
    while(T--){
        solve();
    }
}



Compiler output:
a.cc:66:11: error: reference to 'data' is ambiguous
   66 | bool cmp1(data a, data b){
      |           ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 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:62:8: note:                 'struct data'
   62 | struct data{
      |        ^~~~
a.cc:66:19: error: reference to 'data' is ambiguous
   66 | bool cmp1(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:62:8: note:                 'struct data'
   62 | struct data{
      |        ^~~~
a.cc:66:25: error: expression list treated as compound expression in initializer [-fpermissive]
   66 | bool cmp1(data a, data b){
      |                         ^
a.cc:70:11: error: reference to 'data' is ambiguous
   70 | bool cmp2(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:62:8: note:                 'struct data'
   62 | struct data{
      |        ^~~~
a.cc:70:19: error: reference to 'data' is ambiguous
   70 | bool cmp2(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:62:8: note:                 'struct data'
   62 | struct data{
      |        ^~~~
a.cc:70:25: error: expression list treated as compound expression in initializer [-fpermissive]
   70 | bool cmp2(data a, data b){
      |                         ^
a.cc: In function 'void solve()':
a.cc:83:13: error: template argument 1 is invalid
   83 |     vec<data> q(m);
      |             ^
a.cc:85:17: error: invalid types 'int[int]' for array subscript
   85 |         cin >> q[i].l >> q[i].r;
      |                 ^
a.cc:85:27: error: invalid types 'int[int]' for array subscript
   85 |         cin >> q[i].l >> q[i].r;
      |                           ^
a.cc:86:10: error: invalid types 'int[int]' for array subscript
   86 |         q[i].l--; q[i].r--;
      |          ^
a.cc:86:20: error: invalid types 'int[int]' for array subscript
   86 |         q[i].l--; q[i].r--;
      |                    ^
a.cc:87:10: error: invalid types 'int[int]' for array subscript
   87 |         q[i].idx = i;
      |          ^
a.cc:26:18: error: request for member 'begin' in 'q', which is of non-class type 'int'
   26 | #define ALL(v) v.begin(),v.end()
      |                  ^~~~~
a.cc:90:10: note: in expansion of macro 'ALL'
   90 |     sort(ALL(q), cmp1);
      |          ^~~
a.cc:26:28: error: request for member 'end' in 'q', which is of non-class type 'int'
   26 | #define ALL(v) v.begin(),v.end()
      |                            ^~~
a.cc:90:10: note: in expansion of macro 'ALL'
   90 |     sort(ALL(q), cmp1);
      |          ^~~
a.cc:94:17: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
   94 |     for(auto &i:q){
      |                 ^
      |                 std::begin
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:94:17: error: 'end' was not declared in this scope; did you mean 'std::end'?
   94 |     for(auto &i:q){
      |                 ^
      |                 std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc:26:18: error: request for member 'begin' in 'q', which is of non-class type 'int'
   26 | #define ALL(v) v.begin(),v.end()
      |                  ^~~~~
a.cc:117:10: note: in expansion of macro 'ALL'
  117 |     sort(ALL(q), cmp2);
      |          ^~~
a.cc:26:28: error: request for member 'end' in 'q', which is of non-class type 'int'
   26 | #define ALL(v) v.begin(),v.end()
      |                            ^~~
a.cc:117:10: note: in expansion of macro 'ALL'
  117 |     sort(ALL(q), cmp2);
      |          ^~~
a.cc:119:16: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
  119 |     for(auto i:q) cout << i.ans << endl;
      |                ^
      |                std::begin
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:119:16: error: 'end' was not declared in this scope; did you mean 'std::end'?
  119 |     for(auto i:q) cout << i.ans << endl;
      |                ^
      |                std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~