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<=L&&R<=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)