1#include <cstdio>2#include <iostream>3#include <algorithm>4 #defineM 10000085 using namespacestd;6 intsum[m],mx,n,next[m],p[m],a[m],m;7 structData8 {9 intL,r,o,ans;Ten }q[m]; One BOOLCMP (data a1,data A2) A { - if(a1.l==a2.l) - returna1.r<A2.R; the returna1.l<A2.L; - } - BOOLCMP1 (Data a1,data A2) - { + returna1.o<a2.o; - } + voidAddintA1,intA2) A { at for(inti=a1;i<=n;i+=i&-i) -sum[i]+=A2; - return; - } - intXunintA1) - { in intsu=0; - for(inti=a1;i;i-=i&-i) tosu+=Sum[i]; + returnsu; - } the intMain () * { $scanf"%d",&n);Panax Notoginseng for(intI=1; i<=n;i++) - { thescanf"%d",&a[i]); +mx=Max (mx,a[i]); A } the for(inti=n;i;i--) + { -next[i]=P[a[i]]; $p[a[i]]=i; $ } -scanf"%d",&m); - for(intI=1; i<=m;i++) the { -scanf"%d%d",&q[i].l,&Q[I].R);Wuyiq[i].o=i; the } - for(intI=1; i<=mx;i++) Wu if(P[i]) -Add (P[i],1); AboutSort (q+1, q+m+1, CMP); $ intL=1; - for(intI=1; i<=m;i++) - { - for(;l<q[i].l;) A { + if(Next[l]) theAdd (Next[l],1); -l++; $ } theQ[i].ans=xun (Q[I].R)-xun (q[i].l-1); the } theSort (q+1, q+m+1, CMP1); the for(intI=1; i<=m;i++) -printf"%d\n", Q[i].ans); in return 0; the}
Will read into offline processing, sorted first, tree-like array maintained.
Bzoj 1878: [Sdoi2009]hh's Necklace