BZOJ1878[SDOI2009]HH's Necklace

Source: Internet
Author: User

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", &AMP;ASKS[I].L,&AMP;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&LT;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

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.