Can't split blocks (obviously the complexity will explode ...) )
Offline +bit. Each color appears only once in each query.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#defineMAXN 50050#defineMAXM 200050#defineMAXC 1000500using namespacestd;intn,a[maxn],aft[maxn],pre[maxn],regis[maxc],ans[maxm],m,p=1, T[MAXN];structquery{intL,r,id;} Q[MAXM];BOOLcmp (query X,query y) {if(X.L==Y.L)returnx.r<Y.R; returnx.l<Y.L;}intLowbit (intx) { return(x& (-x));}voidAddintXintval) { for(inti=x;i<=n;i+=lowbit (i)) T[i]+=Val;}intAskintx) { intret=0; for(inti=x;i>=1; i-=lowbit (i)) RET+=T[i]; returnret;}intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); Pre[i]=regis[a[i]];aft[pre[i]]=i; Regis[a[i]]=i; } scanf ("%d",&m); for(intI=1; i<=m;i++) {scanf ("%d%d",&q[i].l,&Q[I].R); Q[i].id=i; } for(intI=1; i<=n;i++) { if(!pre[i]) Add (i,1); if(!aft[i]) aft[i]=n+1; } sort (Q+1, q+m+1, CMP); for(intI=1; i<=m;i++) { for(intj=p;j<=q[i].l-1; j + +) {Add (J,-1); Add (Aft[j],1); } P=Q[I].L; Ans[q[i].id]=ask (Q[I].R)-ask (q[i].l-1); } for(intI=1; i<=m;i++) printf ("%d\n", Ans[i]); return 0;}
Bzoj 1878 hh Necklace