rt, [SDOI2009] HH的项链 - 洛谷
#include <bits/stdc++.h>
#define op return
#define AK 0
#define IOI ;
#define ll long long
#define ld long double
#define ull unsigned ll
#define lll __int128
#define pii pair<int,int>
#define pip pair<int,pii>
#define ppp pair<pii,pii>
#define deb cerr<<"op AK IOI\n"
#define tostring(a) #a
#define cono(a,b) a##b
#define psa(i) (i=-~(i))
#define range(i) (-~(i))
#define mid (l+r>>1)
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
#define GetChar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
inline int read()
{
char c=GetChar();int x=0;bool f=0;
for(;!isdigit(c);c=GetChar())f^=!(c^45);
for(;isdigit(c);c=GetChar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
const int N=1e6+10;
int len,n,a[N],idx[N],cur,cnt[N],ans[N];
struct Query
{
int l,r,id;
bool operator <(const Query &p)const
{
if(idx[l] xor idx[p.l])return l<p.l;
if(idx[l]&1)return r<p.r;
return r>p.r;
}
}q[N];
void build()
{
len=pow(n,0.5);
for(int i=1;i<=len;psa(i))idx[i]=1;
for(int i=len+1;i<=n;psa(i))idx[i]=idx[i-len]+1;
}
inline void add(int p)
{
if(!cnt[a[p]])psa(cur);
psa(cnt[a[p]]);
}
inline void del(int p)
{
--cnt[a[p]];
if(!cnt[a[p]])--cur;
}
int main()
{
n=read();build();
for(int i=1;i<=n;psa(i))a[i]=read();
int m=read();
for(int i=1;i<=m;psa(i))
q[i].l=read(),q[i].r=read(),q[i].id=i;
sort(q+1,q+1+m);
for(int i=1,l=1,r=0;i<=m;psa(i))
{
while(l<q[i].l)del(l++);
while(l>q[i].l)add(--l);
while(r<q[i].r)add(++r);
while(r>q[i].r)del(r--);
ans[q[i].id]=cur;
}
for(int i=1;i<=m;psa(i))printf("%d\n",ans[i]);
op AK IOI
}