Hdu_5919_sequence II (Chairman tree)

Source: Internet
Author: User

Title Link: hdu_5919_sequence II

Test instructions

Give you the number of N, M inquiry, each time you ask each of the number of intervals in the interval of the first occurrence of the position of the median, forced online.

Exercises

A look is the chairman of the tree, but here to ask the first occurrence of the position, there is a technique is to pour the number in the back, if there are the same number, the position of the number of contributions canceled, so the query only need to query root[l], because Root[l] includes the l to n information, The first position of the same number has been reserved previously, so it is OK to query Root[l] 's L to R directly when querying a total number of numbers.

1#include <bits/stdc++.h>2 #defineF (I,A,B) for (int i=a;i<=b;i++)3 using namespacestd;4 5 Const intn=2e5+7;6 structnode{intL,r,sum;} t[ +*N];7 8 intT,n,m,a[n],pre[n],root[n],tot,ic=1;9 Ten voidUpdateint&x,intYintPosintValintL=1,intR=N) One { AT[++tot]=x? t[x]:t[y],t[tot].sum+=val,x=tot; -     if(L==R)return; -     intM=l+r>>1; the     if(pos<=m) update (T[X].L,T[Y].L,POS,VAL,L,M); -     ElseUpdate (t[x].r,t[y].r,pos,val,m+1, R); - } -  + intQueryintXintKintL=1,intR=N) - { +     if(L==R)returnl; A     intM=l+r>>1; at     if(k<=t[t[x].l].sum)returnquery (t[x].l,k,l,m); -     returnQuery (t[x].r,k-t[t[x].l].sum,m+1, R); - } -  - intGetsum (intXintLintRintL=1,intR=N) - { in     if(L&LT;=L&AMP;&AMP;R&LT;=R)returnt[x].sum; -     intM=l+r>>1, ans=0; to     if(l<=m) ans+=getsum (t[x].l,l,r,l,m); +     if(r>m) Ans+=getsum (t[x].r,l,r,m+1, R); -     returnans; the } *  $ intMain ()Panax Notoginseng { -scanf"%d",&t); the      while(t--) +     { Aprintf"Case #%d:", ic++); thescanf"%d%d",&n,&m); +tot=0; -F (I,1, N) scanf ("%d", A +i); $          for(intI=n;i>0; i--) $         { -             if(Pre[a[i]]) update (root[i],root[i+1],pre[a[i]],-1); -Update (root[i],root[i+1],i,1); thepre[a[i]]=i; -         }Wuyi         intans=0, L,r,sum; theF (I,1, M) -         { Wuscanf"%d%d",&l,&R); -L= (L+ans)%n+1, r= (R+ans)%n+1; About             if(L>r) l^=r,r^=l,l^=R; $sum=getsum (root[l],l,r); -printf"%d%c", Ans=query (root[l],sum+1>>1),"\ n"[i==m]); -         } -          while(TOT) t[tot].l=t[tot].r=t[tot].sum=0, tot--; AF (I,1, N) root[i]=0, pre[a[i]]=0; +     } the     return 0; -}
View Code

Hdu_5919_sequence II (Chairman tree)

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.