1878: [Sdoi2009]hh's Necklace time limit:4 Sec Memory limit:64 MB
submit:4857 solved:2401
[Submit] [Status] [Discuss] DESCRIPTIONHH has a chain of beautiful shells. HH believes that different shells will bring good luck, so after each walk, he will take out a shell and think about what they mean. HH keeps collecting new shells, so his necklace is getting longer. One day, he suddenly raised a question: how many different shells are included in a certain piece of shell? The question is difficult to answer ... Because the necklace is too long. So he had to ask the wise you to solve the problem. Input first line: An integer n that represents the length of the necklace. Second line: n integers representing the number of shells in the necklace (integers numbered 0 to 1000000, in turn). Line three: An integer m that indicates the number of HH queries. Next m line: two integers per line, L and R (1≤l≤r≤n), indicating the interval of the inquiry. n≤50000,m≤200000. Output
M lines, one integer per line, in turn, to ask the corresponding answer.
Sample Input6
1 2 3 4 3 5
3
1 2
3 5
2 6
Sample Output2
2
4
HINT Source
Day2
MMP In fact, this problem is very simple, but this spicy chicken because a less than the number is written greater than the number and the eyes are not good, so wasted one hours to do well fuck
1#include"bits/stdc++.h"2 using namespacestd;3typedefLong LongLL;4 Const intmax1=5e4+5;5 Const intmax2=2e5+5;6 intn,m;7 inta[max1],b[1000005],POS[MAX1],BAS,ANS,AN[MAX2];8 structnode{9 intID;Ten intL,r; One BOOL operator< (ConstNode &tt)Const { A if(pos[l]!=POS[TT.L]) - returnpos[l]<POS[TT.L]; - returnr<TT.R; the } - }QUE[MAX2]; -InlineintRead () { - intan=0, x=1;CharC=GetChar (); + while(c<'0'|| C>'9') {if(c=='-') x=-1; c=GetChar ();} - while(c>='0'&& c<='9') {an=an*Ten+c-'0'; c=GetChar ();} + returnan*x; A } at voidUpdateintXinty) { -ans-= (b[a[x]]==0?0:1); -b[a[x]]+=y; -ans+= (b[a[x]]==0?0:1); - } - intMain () { inFreopen ("neck.in","R", stdin); -Freopen ("Neck.out","W", stdout); to inti,j; +N=read (); Bas= (int) sqrt (n1.0); - for(i=1; i<=n;i++) A[i]=read (), pos[i]= (i-1)/bas+1; thememset (b,0,sizeof(b)); *m=read (); $ for(i=1; i<=m;i++){Panax NotoginsengQue[i].id=i; -Que[i].l=read (); que[i].r=read (); the } +Sort (que+1, que+m+1); A intL=1, r=0; the for(i=1; i<=m;i++){ + while(R<QUE[I].R) {R++;update (R,1);} - while(R>QUE[I].R) {Update (r,-1); r--;} $ while(L>QUE[I].L) {L--;update (L,1);} $ while(L<QUE[I].L) {Update (l,-1); l++;} -an[que[i].id]=ans; - } the for(i=1; i<=m;i++) -printf"%d\n", An[i]);Wuyi return 0; the}
BZOJ-1878: [Sdoi2009]hh's Necklace (Mo team algorithm)