Language: C++

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int a[500010],b[500010],c[500010],n,q0,now,last[500010],ans[500010];
struct query
{
    int l,r,id,ans;
}q[500010];
bool cmp(const query &x,const query &y)
{
     return x.r<y.r;
}
struct node
{
          int s[2];
          int val,rd;
          int sz,cnt;
}t[100010];
int root,num;
int op,x;
void maintain(int rt)
{
     t[rt].sz=t[t[rt].s[0]].sz+t[t[rt].s[1]].sz+t[rt].cnt;
}
void rotate(int&rt,int d)
{
     int k=t[rt].s[d^1];
     t[rt].s[d^1]=t[k].s[d];
     t[k].s[d]=rt;
     maintain(rt);
     maintain(k);
     rt=k;
} 
void insert(int &rt,int v)
{
     if(rt==0)
     {
         rt=++num;
         t[rt].val=v;
         t[rt].s[0]=t[rt].s[1]=0;
         t[rt].rd=rand();
         t[rt].sz=t[rt].cnt=1;
         return;
     }
     if(t[rt].val==v)
     {
         t[rt].sz++;
         t[rt].cnt++;
         return;
     }
     if(v<t[rt].val) insert(t[rt].s[0],v);
     else insert(t[rt].s[1],v);
     if(t[t[rt].s[0]].rd>t[rt].rd) rotate(rt,1);
     if(t[t[rt].s[1]].rd>t[rt].rd) rotate(rt,0);
     maintain(rt);
}
void remove(int &rt,int v)
{
     if(t[rt].val==v)
     {
          if(t[rt].cnt>1)
          {
              t[rt].sz--;
              t[rt].cnt--;
              return;
          } 
          if(t[rt].s[0]*t[rt].s[1]==0)
          {
              rt=t[rt].s[0]+t[rt].s[1];
              return;
          }
          if(t[t[rt].s[0]].rd>t[t[rt].s[1]].rd)
          {
              rotate(rt,1);
              remove(t[rt].s[1],v);
              maintain(rt);
              return;
          }
          else
          {
              rotate(rt,0);
              remove(t[rt].s[0],v);
              maintain(rt);
              return;
          }
     }
     if(v<t[rt].val) remove(t[rt].s[0],v);
     else remove(t[rt].s[1],v);
     maintain(rt);
}
int rank(int rt,int v)
{
    if(rt==0) return 0;
    if(t[rt].val==v) return t[t[rt].s[0]].sz+1;
    if(v<t[rt].val) return rank(t[rt].s[0],v);
    else return rank(t[rt].s[1],v)+t[t[rt].s[0]].sz+t[rt].cnt;
}
int rankl(int rt,int v)
{
    if(rt==0) return 0;
    if(t[rt].val==v) return t[t[rt].s[0]].sz+t[rt].cnt;
    if(v<t[rt].val) return rankl(t[rt].s[0],v);
    else return rankl(t[rt].s[1],v)+t[t[rt].s[0]].sz+t[rt].cnt;
}
int kth(int rt,int k)
{
    if(k>=t[t[rt].s[0]].sz+1&&k<=t[t[rt].s[0]].sz+t[rt].cnt) return t[rt].val;
    if(k<t[t[rt].s[0]].sz+1) return kth(t[rt].s[0],k);
    else return kth(t[rt].s[1],k-t[t[rt].s[0]].sz-t[rt].cnt);
}
int main()
{
    int i;
    srand(0);
    cin>>n>>q0;
    for(i=1;i<=n;i++) 
    {
        scanf("%d",&a[i]);
        c[i]=c[i-1];
        if(b[a[i]]==0) c[i]++;
        b[a[i]]++;
    }
    for(i=1;i<=q0;i++) 
    {
        scanf("%d%d",&q[i].l,&q[i].r);
        q[i].id=i;
    }
    sort(q+1,q+q0+1,cmp);
    //for(i=1;i<=n;i++) cout<<c[i]<<endl;
    for(i=1;i<=q0;i++)
    {
        while(now<q[i].r)
        {
            now++;
            if(last[a[now]]!=0) remove(root,last[a[now]]);
            last[a[now]]=now;
            insert(root,now);
        }
        ans[q[i].id]=c[q[i].r]-rankl(root,q[i].l-1);
    }
    for(i=1;i<=q0;i++) printf("%d\n",ans[i]);
    system("pause");
}


Compiler output:
a.cc: In function 'int rank(int, int)':
a.cc:96:28: error: reference to 'rank' is ambiguous
   96 |     if(v<t[rt].val) return rank(t[rt].s[0],v);
      |                            ^~~~
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:92:5: note:                 'int rank(int, int)'
   92 | int rank(int rt,int v)
      |     ^~~~
a.cc:97:17: error: reference to 'rank' is ambiguous
   97 |     else return rank(t[rt].s[1],v)+t[t[rt].s[0]].sz+t[rt].cnt;
      |                 ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:92:5: note:                 'int rank(int, int)'
   92 | int rank(int rt,int v)
      |     ^~~~
a.cc: In function 'int main()':
a.cc:129:5: error: 'sort' was not declared in this scope; did you mean 'short'?
  129 |     sort(q+1,q+q0+1,cmp);
      |     ^~~~
      |     short