Test instructions: Given a sequence of colors, each group asks for the interval [l,r], the number of different colors in [l,r]
Exercises
First, all the colors are discretized, then offline, and the query is sorted in ascending order in the right interval. Sweep the entire sequence from 1-n, set pos[i] to the last occurrence of the first color, assuming that the current sweep to the position is I, update pos[a[i]], then the problem becomes: a sequence (Pos), a number greater than or equal to a number (L).
The number of pos=j is maintained in a tree-like array, and each query is l-n in the tree array.
Seemingly sdoi do not like to test large data structure ah ... waiting for this year's face-beating
#include <cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#include<map>using namespacestd;#defineLowbit (x) (x& (×))Const intmaxn=50000+2;Const intmaxm=200000+2;structhash{intu,t; Hash*Next; Hash () {} hash (int_u,int_t,hash *_next): U (_u), T (_t), Next (_next) {}}*TAB[MAXN],MEM[MAXM];intN,m,a[maxn],cnt,ans[maxm],pos[maxn],s[maxn];map<int,int>m;voidUpdate (intPintx) { while(p<=n) s[p]+=x,p+=lowbit (P);}intSum (intp) { intret=0; while(p) ret+=s[p],p-=lowbit (P); returnRet;}intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) {scanf ("%d", A +i); if(!m[a[i]]) m[a[i]]=++CNT; A[i]=M[a[i]]; } scanf ("%d", &m), cnt=0; for(intI=1, l,r;i<=m;i++) {scanf ("%d%d",&l,&R); MEM[CNT].U=L,MEM[CNT].T=I,MEM[CNT]. next=tab[r],tab[r]=&mem[cnt],cnt++; } for(intI=1; i<=n;i++){ if(Pos[a[i]]) Update (pos[a[i]],-1); Pos[a[i]]=i,update (I,1); for(Hash *p=tab[i];p; p=p->next) Ans[p->t]=sum (N)-sum (p->u-1); } for(intI=1; i<=m;i++) printf ("%d\n", Ans[i]); return 0;}
View Code
BZOJ1878 SDOI2009 hh Necklace Tree-shaped array