BZOJ-1878: [Sdoi2009]hh's Necklace (Mo team algorithm)

Source: Internet
Author: User

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&LT;QUE[I].R) {R++;update (R,1);} -          while(R&GT;QUE[I].R) {Update (r,-1); r--;} $          while(L&GT;QUE[I].L) {L--;update (L,1);} $          while(L&LT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.