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
| #include<bits/stdc++.h> #define N 200500 using namespace std; int last[N],team[N],c[N],Ans[N]; inline int lowbit(x){return (x&(-x));} void updata(int x,int v){for(;x<N;x+=lowbit(x))c[x]+=v;} int Sum(int x) { int ans=0; for(;x>0;x-=lowbit(x))ans+=c[x]; return ans; } struct ss { int l,r,ans,index; }qu[N]; int n,q,sum; int cmp(ss a,ss b){return a.r<b.r;} int main() { while(scanf("%d %d",&n,&q)==2) { memset(last,0,sizeof(last)); memset(c,0,sizeof(c)); sum=0; for(int i=1;i<=n;i++) { scanf("%d",&team[i]); team[i+n]=team[i]; } for(int i=1;i<=q;i++)scanf("%d %d",&qu[i].r,&qu[i].l),qu[i].index=i,qu[i].r+=n; sort(qu+1,qu+1+q,cmp); int c1=1; for(int i=1;i<=q;i++) { for(int j=c1;j<=qu[i].r;j++) { if(last[team[j]]==0) { updata(j,1); last[team[j]]=j; } else { updata(j,1); updata(last[team[j]],-1); last[team[j]]=j; } } c1=qu[i].r+1; Ans[qu[i].index]=Sum(qu[i].r)-Sum(qu[i].l-1); } for(int i=1;i<=q;i++)printf("%d\n",Ans[i]); } return 0; }
|