莫队
#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define fd(i,a,b) for(register int i=a;i<=b;i=-~i)
#define bd(i,a,b) for(register int i=a;i>=b;i=~-i)
using namespace std;
const int N=1e6+509,M=509,MOD=10007;
int n,siz,m,ans,lp=1,rp=0;
int bel[N],a[N],p[N],col[N];
//int add[N],L[N],R[N];
struct node{int l,r,id;}q[N];
inline bool cmp(node x,node y)
{
if(bel[x.l]==bel[y.l]) return bel[x.l]<bel[y.l];
// else return (bel[x.l]&1)?x.r<y.r:x.r>y.r;
return x.l<y.l;
}
inline void move(int x,int d)
{
col[x]+=d;
if(d>0) ans+=(col[x]==1);
else ans-= !col[x];
}
inline int read()
{
int num=0; bool flag=1; char c=getchar();
while(!isdigit(c)){if(c=='-') flag=0;c=getchar();}
while(isdigit(c)){num=(num<<1)+(num<<3)+(c-'0');c=getchar();}
return flag?num:-num;
}
inline void write(int x)
{
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar((x%10)^48);
}
signed main()
{
// freopen("A.in","r",stdin);
// freopen("A.out","w",stdout);
// #ifdef FJ
// ios::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
// #endif
n=read();
siz=sqrt(n);
fd(i,1,n) a[i]=read(),bel[i]=(i+1)/siz;
// FJ::build(n);
m=read();
fd(i,1,m)
{
q[i].l=read();
q[i].r=read();
q[i].id=i;
}
sort(q+1,q+1+m,cmp);
fd(i,1,m)
{
while(lp<q[i].l) move(a[lp],-1),lp=-~lp;
while(lp>q[i].l) move(a[lp-1],1),lp=~-lp;
while(rp<q[i].r) move(a[rp+1],1),rp=-~rp;
while(rp>q[i].r) move(a[rp],-1),rp=~-rp;
p[q[i].id]=ans;
}
fd(i,1,m) write(p[i]),putchar('\n');
return 0;
}
本文来自博客园,作者:whrwlx,转载请注明原文链接:https://www.cnblogs.com/whrwlx/p/18204803