Language: C++

//By Lin
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<cctype>
#include<cmath>

#define eps 1e-9
#define sqr(x) ((x)*(x))
#define Rep(i,n) for(int i = 0; i<n; i++)
#define foreach(i,n) for( __typeof(n.begin()) i = n.begin(); i!=n.end(); i++)
#define X first
#define Y second
#define mp(x,y) make_pair(x,y)

using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<pii,int> piii;

#define N 500010

deque<int> que[N];
int fa[N], dis[N], a[N], mark[N];
int tol, num, n, q, ans[N];
map<int,int> mm;
vector<piii> data[N];

int Find(int x) {
    return fa[x] == x? x: (fa[x] = Find(fa[x]));
}

bool check(int val) {
    if ( que[val].empty() ) return false;
    int l = que[val].front(), r = que[val].back();
    if ( l == r ) return true;
    que[val].pop_front();
    int g = que[val].front();
    bool ret = Find(g) == Find(r);
    que[val].push_front(l);
    return ret;
}

void insert(int x, bool flag) {
    int val = a[x];
    if ( mark[val]++ == 0 ) tol ++;
    if ( check(val) ) num --;
    if ( flag ) 
        que[val].push_front(x);
    else 
        que[val].push_back(x);
    if ( check(val) ) num ++;
}

void del(int x) {
    int val = a[x];
    if ( --mark[val] == 0 ) tol --;
    if ( check(val) ) num --;
    que[val].pop_front();
    if ( check(val) ) num ++;
}

int     main(){
    scanf("%d", &n);
    scanf("%d", &q);
    Rep(i,n) scanf("%d", &a[i]);
    Rep(i,n) {
        if ( mm.find(a[i]) == mm.end() ) {
            fa[i] = -1;
            dis[i] = 0;
        }
        else {
            int j = mm[a[i]];
            if ( i - j == dis[j] ) fa[i] = j;
            else fa[i] = i;
            dis[i] = i - j;
        }
        mm[a[i]] = i;
    }
    int L = 0;
    for( ; sqr(L)<=n; L++);
    
    int l, r;
    Rep(i,q) {
        scanf("%d%d", &l, &r);
        l--, r--;
        data[l/L].push_back(mp(mp(r,l),i));
    }
    for(int i = 0; i*L<n; i++) {
        sort( data[i].begin(), data[i].end() );
        int l = i * L, r = i * L - 1;
        num = tol = 0;
        Rep(j, data[i].size()) {
            for( ; r<data[i][j].X.X; insert(++r,0) );
            for( ; l>data[i][j].X.Y; insert(--l,1) );
            for( ; l<data[i][j].X.Y; del(l++) );
            ans[data[i][j].Y] = tol + (num==0?1:0);
        }
        for(; l<=r; del(l++));
    }
    Rep(i,q) printf("%d\n", ans[i] );
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:92:9: error: reference to 'data' is ambiguous
   92 |         data[l/L].push_back(mp(mp(r,l),i));
      |         ^~~~
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:4:
/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:32:14: note:                 'std::vector<std::pair<std::pair<int, int>, int> > data [500010]'
   32 | vector<piii> data[N];
      |              ^~~~
a.cc:95:15: error: reference to 'data' is ambiguous
   95 |         sort( data[i].begin(), data[i].end() );
      |               ^~~~
/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:32:14: note:                 'std::vector<std::pair<std::pair<int, int>, int> > data [500010]'
   32 | vector<piii> data[N];
      |              ^~~~
a.cc:95:32: error: reference to 'data' is ambiguous
   95 |         sort( data[i].begin(), data[i].end() );
      |                                ^~~~
/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:32:14: note:                 'std::vector<std::pair<std::pair<int, int>, int> > data [500010]'
   32 | vector<piii> data[N];
      |              ^~~~
a.cc:98:16: error: reference to 'data' is ambiguous
   98 |         Rep(j, data[i].size()) {
      |                ^~~~
a.cc:15:35: note: in definition of macro 'Rep'
   15 | #define Rep(i,n) for(int i = 0; i<n; i++)
      |                                   ^
/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:32:14: note:                 'std::vector<std::pair<std::pair<int, int>, int> > data [500010]'
   32 | vector<piii> data[N];
      |              ^~~~
a.cc:99:22: error: reference to 'data' is ambiguous
   99 |             for( ; r<data[i][j].X.X; insert(++r,0) );
      |                      ^~~~
/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:32:14: note:                 'std::vector<std::pair<std::pair<int, int>, int> > data [500010]'
   32 | vector<piii> data[N];
      |              ^~~~
a.cc:100:22: error: reference to 'data' is ambiguous
  100 |             for( ; l>data[i][j].X.Y; insert(--l,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:32:14: note:                 'std::vector<std::pair<std::pair<int, int>, int> > data [500010]'
   32 | vector<piii> data[N];
      |              ^~~~
a.cc:101:22: error: reference to 'data' is ambiguous
  101 |             for( ; l<data[i][j].X.Y; del(l++) );
      |                      ^~~~
/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:32:14: note:                 'std::vector<std::pair<std::pair<int, int>, int> > data [500010]'
   32 | vector<piii> data[N];
      |              ^~~~
a.cc:102:17: error: reference to 'data' is ambiguous
  102 |             ans[data[i][j].Y] = tol + (num==0?1:0);
      |                 ^~~~
/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:32:14: note: