1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int MAXN=500005,MAXM=200005,MAX=1000005; struct node{ int no,st,ed; };
node q[MAXM]; int a,t[MAX]={0},pre[MAXN]={0},sgt[MAXN*4]; int ans[MAXM]={0}; bool cmp(node &a,node &b){ return a.ed<b.ed; }
int query(int rt,int nst,int ned,int qst,int qed){ if(qst>ned||qed<nst) return 0; if(qst<=nst&&qed>=ned) return sgt[rt]; int mid=(nst+ned)>>1; return query(rt<<1,nst,mid,qst,qed)+query(rt<<1|1,mid+1,ned,qst,qed); }
void update(int rt,int nst,int ned,int idx,int val){ if(nst==ned){ if(nst==idx) sgt[rt]+=val; return; } int mid=(nst+ned)>>1; if(idx<=mid) update(rt<<1,nst,mid,idx,val); else update(rt<<1|1,mid+1,ned,idx,val); sgt[rt]=sgt[rt<<1]+sgt[rt<<1|1]; }
int main(){ int n,m; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a); pre[i]=t[a]; t[a]=i; } scanf("%d",&m); for(int i=0;i<m;i++){ scanf("%d%d",&q[i].st,&q[i].ed); q[i].no=i; } sort(q,q+m,cmp); int tmp=1; for(int i=0;i<m;i++){ while(tmp<=q[i].ed){ update(1,1,n,tmp,1); update(1,1,n,pre[tmp],-1); tmp++; } ans[q[i].no]=query(1,1,n,q[i].st,q[i].ed); } for(int i=0;i<m;i++) printf("%d\n",ans[i]); return 0; }
|