"Bzoj [1878" [Sdoi2009]hh's Necklace

Source: Internet
Author: User

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. OUTPUTM 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


For 20% of data, n≤100,m≤1000;
For 40% of data, n≤3000,m≤200000;
For 100% of data, n≤50000,m≤200000.

MO Team Water over
1#include <cstdio>2#include <cmath>3#include <algorithm>4 using namespacestd;5 structnode{intNo,l,r,ans;} b[200010];6 intsum[1000010],a[50010],bel[50010];7 intn,m,ans,i;8 BOOLCMP1 (node A,node b) {9     if(BEL[A.L]==BEL[B.L])returna.r<B.R;Ten     returna.l<B.L; One } A   - BOOLCMP2 (node A,node b) { -     returna.no<b.no; the } -   - voidUpdata (intWzintAddintk) { -     if(add==-1&&sum[a[wz]]==1) ans--; +     if(add==1&&sum[a[wz]]==0) ans++; -sum[a[wz]]+=add; + } A   at intMain () { -scanf"%d",&n); -      for(intI=1; i<=n;i++) scanf ("%d",&a[i]); -scanf"%d",&m); -      for(intI=1; i<=m;i++){ -scanf"%d%d",&b[i].l,&B[I].R); inb[i].no=i; -     } to     intblock=sqrt (n); +      for(intI=1; i<=n;i++) bel[i]= (i/block) +1; -Sort (b +1, b+m+1, CMP1); the     intL=1, r=0; *      for(intI=1; i<=m;i++){ $          for(; r<b[i].r;r++) Updata (r+1,1, i);Panax Notoginseng          for(; r>b[i].r;r--) Updata (r,-1, i); -          for(; l>b[i].l;l--) Updata (L-1,1, i); the          for(; l<b[i].l;l++) Updata (l,-1, i); +b[i].ans=ans; A     } theSort (b +1, b+m+1, CMP2); +      for(intI=1; i<=m;i++) printf ("%d\n", B[i].ans); -}

"Bzoj [1878" [Sdoi2009]hh's Necklace

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.