Http://www.cnblogs.com/autsky-jadek/p/4020296.html
With bzoj2120.
1#include <cstdio>2#include <cmath>3#include <cstring>4#include <algorithm>5 using namespacestd;6 intn,m,sz,sum,l[ -],r[ -],num[10001],pre[10001],preb[10001],a[10001],x,y,pos[1000001];7 intRes,num;Charc,ch[ A];8InlineintG ()9 {Tenres=0; C='*'; One while(c<'0'|| C>'9') c=GetChar (); A while(c>='0'&&c<='9') {res=res*Ten+ (C-'0'); C=GetChar ();} - returnRes; - } theInlinevoidPintx) - { -num=0;if(!x) {Putchar ('0');p UTS ("");return;} - while(x>0) ch[++num]=x%Ten, x/=Ten; + while(Num) Putchar (ch[num--]+ -); -Puts""); + } A voidMakeblock () at { -Sz=sqrt ((Double) n*log2 (n));if(!SZ) sz=1; - for(sum=1; sum*sz<n;sum++) - { -L[sum]= (sum-1) *sz+1; -r[sum]=sum*sz; in for(inti=l[sum];i<=r[sum];i++) -num[i]=sum; to } +l[sum]=sz* (sum-1)+1; -r[sum]=N; the for(inti=l[sum];i<=r[sum];i++) *num[i]=sum; $ }Panax Notoginseng voidMakepre () - { the for(intI=1; i<=n;i++) {pre[i]=pos[a[i]]; pos[a[i]]=i;} +memcpy (Preb,pre,sizeof(pre)); A for(intI=1; i<=sum;i++) Sort (pre+l[i],pre+r[i]+1); the } +Inlinevoidquery () - { $ intres=0; $ if(num[x]+1>=num[y]) { for(inti=x;i<=y;i++)if(preb[i]<x) res++;} - Else - { the for(inti=x;i<=r[num[x]];i++)if(preb[i]<x) res++; - for(inti=l[num[y]];i<=y;i++)if(preb[i]<x) res++;Wuyi for(inti=num[x]+1; i<num[y];i++) Res+=lower_bound (pre+l[i],pre+r[i]+1, x)-(pre+l[i]); the } - P (res); Wu } -InlinevoidUpdate () About { $ intt=0; - for(inti=x+1; i<=n;i++) - if(a[i]==y) -{*lower_bound (pre+l[num[i]],pre+r[num[i]],preb[i]) =x; preb[i]=x; ASort (pre+l[num[i]],pre+r[num[i]]+1); Break;} +a[0]=y; the for(inti=x-1; i>=0; i--) - if(a[i]==y) ${*lower_bound (pre+l[num[x]],pre+r[num[x]],preb[x]) =i; preb[x]=i; theSort (pre+l[num[x]],pre+r[num[x]]+1); Break;} the intT2=A[X]; a[x]=y; the for(inti=x;i>=1; i--) the if(a[i]==T2) -{t=i; Break;} in for(inti=x+1; i<=n;i++) the if(a[i]==T2) the{*lower_bound (pre+l[num[i]],pre+r[num[i]],preb[i]) =t; preb[i]=T; AboutSort (pre+l[num[i]],pre+r[num[i]]+1); Break;} the}Charop[1]; the intMain () the { +N=g (); m=G (); - for(intI=1; i<=n;i++) a[i]=G (); the Makeblock (); Makepre ();Bayi for(intI=1; i<=m;i++) the { thescanf"%s", op); x=g (); y=G (); - if(op[0]=='Q') query (); - Elseupdate (); the } the return 0; the}
"chunking" bzoj2453 maintenance queue