BZOJ1878[SDOI2009]HH's Necklace
Test instructions
n number, the number of different numbers contained in the M query interval [l,r].
Exercises
Mo team seems to be able to do ~ But the positive solution is a tree-like array. The query is sorted by the left endpoint, and the next number is equal to each number, and the position of the first occurrence of each number is set to 1 in the tree array, at which point it is asked how many different numbers are in 1 to X. After enumerating the ordered queries, move the current left endpoint to the right, and each right shifts the position of the number of the original position to its same number in the tree array to 1, guaranteeing that the number will not be omitted if it appears in [L,r]. When the left endpoint is moved to the left end of the query, it outputs query (R)-query (L-1), which indicates how many different numbers are in L to R.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineInc (I,J,K) for (int i=j;i<=k;i++)5 #defineMAXN 501006 #defineLB (x) x&-x;7 using namespacestd;8 9 inta[maxn],last[maxn* -],next[maxn],sm[maxn],n,m;Ten structask{intL,r,ans,id;}; Ask Asks[maxn*6]; One BOOLCMP1 (ask A,ask b) {returna.l<B.L;} A BOOLCMP2 (ask A,ask b) {returna.id<b.id;} -InlinevoidUpdateintXintV) { while(x<=n) {sm[x]+=v,x+=lb (x);}} -InlineintQueryintx) {intq=0; while(x>0) {q+=sm[x],x-=lb (x);}returnq;} the intMain () { -scanf"%d", &n); Inc (I,1, N) scanf ("%d",&a[i]); -scanf"%d", &m); Inc (I,1, m) scanf ("%d%d", &ASKS[I].L,&ASKS[I].R), asks[i].id=i; -Memset (Last,0,sizeof(last)); Inc (I,1, N) {if(Last[a[i]]) next[last[a[i]]]=i;ElseUpdate (I,1); last[a[i]]=i;} +Sort (asks+1, asks+1+M,CMP1);intL=1; -Inc (I,1, M) { + while(L<ASKS[I].L) {if(Next[l]) update (Next[l],1); l++;} AAsks[i].ans=query (ASKS[I].R)-query (asks[i].l-1); at } -Sort (asks+1, asks+1+M,CMP2); Inc (I,1, m) printf ("%d\n", Asks[i].ans); - return 0; -}
20160525
BZOJ1878[SDOI2009]HH's Necklace