Tree with modified chair

Source: Internet
Author: User

The general Chairman tree is considered to be a prefix set of segment tree, then this is the tree array set of segment tree

The prefix interval consists of the original prefix of a segment tree into a number of line segments that are composed of bits.

Each segment tree maintains or is a discrete sorted sequence.

Each node is also the same as a chairman tree, I feel more like a line tree, but the modification of the use of the chairman of the tree method, is on the original basis of the modification (but these original are not saved)

Add operation to modify some chairman tree

Sum operation to add some of the Chairman tree interval and

The outer bit is to handle the query interval, the inner layer is to find the K value

KTH to put the chairman tree used, each left to the right

Code (not submitted)

////main.cpp//zoj2112////Created by Candy on 2016/12/11.//copyright©2016 year Candy. All rights reserved.//#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespaceStd;typedefLong Longll;Const intn=6e4+5; inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; C=GetChar ();} returnx*F;}intn,q,m,a[n],mp[n],i,j,k;Chars[2];structquestion{Chars[2]; intI,j,k,x,d;} Q[n];inlineintBin (intv) {    intL=1, r=m;  while(l<=R) {        intMid= (l+r) >>1; if(MP[MID]==V)returnmid; if(MP[MID]&GT;V) r=mid-1; ElseL=mid+1; }    return-1;} InlineintLowbit (intx) {returnx&-x;}intC[n];structnode{intLc,rc,w;} T[n* -];intCnt=0, root[n];voidInsint&x,intLintRintNumintV) {//1-1t[++cnt]=t[x];x=CNT; T[X].W+=v; if(L==R)return; intMid= (l+r) >>1; if(num<=mid) Ins (t[x].lc,l,mid,num,v); ElseINS (t[x].rc,mid+1, r,num,v);}voidAddintPintv) {    int_=Bin (a[p]);  for(inti=p;i<=n;i+=lowbit (i)) ins (Root[i],1, m,_,v);//1...M}intCUR[N],QL,QR;intSumintp) {    intret=0;  for(intI=p;i;i-=lowbit (i)) RET+=T[T[CUR[I]].LC].W;//LC    returnret;}intQueryintLintRintK) {//printf ("Query%d%d%d\n", l,r,k);    if(L==R)returnl; intLs=sum (QR)-sum (ql-1), mid= (l+r) >>1; if(k<=ls) {         for(inti=ql-1; I;i-=lowbit (i)) cur[i]=t[cur[i]].lc;  for(intI=qr;i;i-=lowbit (i)) cur[i]=t[cur[i]].lc; returnquery (L,MID,K); }Else{         for(inti=ql-1; I;i-=lowbit (i)) cur[i]=t[cur[i]].rc;  for(intI=qr;i;i-=lowbit (i)) cur[i]=t[cur[i]].rc; returnQuery (mid+1, r,k-ls); }}voidsolve () {CNT=0; memset (Root,0,sizeof(root));  for(intI=1; i<=n;i++) Add (i,1);  for(intI=1; i<=q;i++){        if(q[i].s[0]=='Q') {QL=q[i].i,qr=Q[I].J;  for(inti=ql-1; I;i-=lowbit (i)) cur[i]=Root[i];  for(intI=qr;i;i-=lowbit (i)) cur[i]=Root[i]; printf ("%d\n", Mp[query (1, M,Q[I].K)]); }Else{Add (q[i].x,-1); A[q[i].x]=Q[I].D; Add (q[i].x,1); }    }}intMainintargcConst Char*argv[]) {    intt=read ();  while(t--){        //Initm=0; Memset (MP,0,sizeof(MP)); N=read (); q=read ();  for(intI=1; i<=n;i++) a[i]=mp[++m]=read ();  for(intI=1; i<=q;i++) {scanf ("%s", Q[I].S); if(q[i].s[0]=='Q') Q[i].i=read (), Q[i].j=read (), q[i].k=read (); ElseQ[i].x=read (), q[i].d=mp[++m]=read (); } Sort (MP+1, mp+1+m); intp=0; mp[++p]=mp[1];  for(intI=2; i<=m;i++)if(mp[i]!=mp[i-1]) mp[++p]=Mp[i]; M=p; //for (int i=1;i<=m;i++) printf ("MP%d\n", Mp[i]);solve (); }        return 0;}

Tree with modified chair

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.