Rokua P3380 "template" Two-force balance tree (tree set tree, tree array, segment tree)

Source: Internet
Author: User

Rokua Topic Portal

Emm ... The title of the topic wrote a balance tree, but the theoretical complexity of the problem of the optimal solution should be a tree array set of domain segment tree bar.

Just like the dynamic ranking (Konjac Konjac sol, put a link scam traffic 233)

All values (including the initial a array, operations 1, 3, 4, 5 K) are all thrown in first discretization

For 1 operation to check the number of smaller than it, pick log tree segment, find the interval is less than the number of +1, this is also more like

Operation 2 is the dynamic Ranking,log Tree segment tree along with the addition, like the static chairman of the tree to find a small k jump, Operation 3 dynamic ranking also have

Operation 4 first to ask for less than the number of numbers, then the precursor ranking is equal to this number, pay attention to the special 0 is good.

Operation 5 is also the first to seek the ranking to find this number, the ranking is less than and equal to the number of numbers (equal to check the number in the range of +1 in the number of rankings)

Desperately Kathang (small tricks, found that the current jump to the point size is 0, you don't have to jump down), but the constant is still ugly ... Maybe we should break the rumor that the non-recursive version runs a little faster than the recursive version?

#include <cstdio>#include <cstring>#include <algorithm>#define RG Register#define R RG int#define II Inline int#define IV-inline void#define GC if (++pi==iend) fread (Pi=ibuf,1,sz,stdin)#define PC (C) *po=c;if (++po==oend) fwrite (po=obuf,1,sz,stdout)#define Q b+1,b+l+1#define LB (X) x=lower_bound (q,x)-Busing namespaceStdConst intsz=1<< -, n=50009, m=5000009, inf=2147483647;//Card space, m not to nlog^2CharIbuf[sz],obuf[sz],*pi=ibuf+sz-1, *po=obuf;Const Char*iend=ibuf+sz,*oend=obuf+sz;iv in (r&x) {GC; while(*pi<'-') GC; x=*pi& the; GC; while(*pi>'-') {x*=Ten;x+=*pi& the; GC;}} IV out (R x) {if(x>9) Out (x/Ten); PC (XTen|' 0 ');}intn,l,p,a[n],b[n<<1],op[n],ql[n],qr[n],qk[n];intrt[n],lc[m],rc[m],s[m],ra[ -],rs[ -];iv upd (R p,r k,r v) {//Update     for(R u,l,r,m,i=p;i<=n;i+=i&-i) {if(!rt[i]) rt[i]=++p;u=rt[i];l=1; r=l; while(L^R) {s[u]+=v;m= (l+r) >>1;if(k<=m) {r=m;if(!lc[u]) lc[u]=++p;u=lc[u];}Else{L=m+1;if(!rc[u]) rc[u]=++p;u=rc[u];}    } s[u]+=v; }}ii kth (R p,r k) {//Ask for a small value of KR i,l=1, r=l,m,sum,pa=0. p1=0; for(i=qr[p]; i;i-=i&-i) ra[++pa]=rt[i]; for(I=ql[p]-1; i;i-=i&-i) Rs[++ps]=rt[i]; while(L^R) {sum=0; m= (L+r) >>1; for(i=1; i<=pa;++i) Sum+=s[lc[ra[i]]; for(i=1; i<=ps;++i) Sum-=s[lc[rs[i]];if(k<=sum) {r=m; for(i=1, p=pa,pa=0; i<=p;++i) Ra[++pa]=lc[ra[i]]; for(i=1, p=ps,ps=0; i<=p;++i) Rs[++ps]=lc[rs[i]]; }Else{L=m+1; k-=sum; for(i=1, p=pa,pa=0; i<=p;++i) Ra[++pa]=rc[ra[i]]; for(i=1, p=ps,ps=0; i<=p;++i) Rs[++ps]=rc[rs[i]]; }    }returnB[L];} II rank (R p,r x) {//To rank the value after discretization (from 0, that is, the number of numbers less than or equal to the value)R i,u,l,r,m,k=0; for(i=qr[p];i;i-=i&-i) {u=rt[i];l=1; r=l; while(S[U]&AMP;&AMP;L^R) {m= (l+r) >>1;if(x<=m) r=m,u=lc[u];ElseK+=s[lc[u]],l=m+1, U=rc[u]; }    } for(I=ql[p]-1; i;i-=i&-i) {u=rt[i];l=1; r=l; while(S[U]&AMP;&AMP;L^R) {m= (l+r) >>1;if(x<=m) r=m,u=lc[u];ElseK-=s[lc[u]],l=m+1, U=rc[u]; }    }returnK;}intMain () {R m,i,rk; In (n); in (m); L=n; for(i=1; i<=n;++i) in (A[i]); memcpy (B,a, (n+1) <<2); for(i=1; i<=m;++i) {in (Op[i]);//Save up firstIn (Ql[i]);if(op[i]!=3) in (Qr[i]); In (Qk[i]);if(op[i]!=2) B[++l]=qk[i]; } b[++l]=inf;sort (Q); L=unique (Q)-B-1; for(i=1; i<=n;++i) upd (i,lb (A[i]),1); for(i=1; i<=m;++i) {if(op[i]!=2) lb (qk[i]);Switch(Op[i]) { Case 1: Out (rank (i,qk[i])+1);p C ('\ n'); Break; Case 2: Out (KTH (i,qk[i)));p C ('\ n'); Break; Case 3: Upd (Ql[i],a[ql[i]),-1); UPD (Ql[i],a[ql[i]]=qk[i],1); Break; Case 4: Rk=rank (I,qk[i]);if(RK) Out (KTH (I,RK));Else{PC ('-'); Out (INF);} Pc'\ n'); Break; Case 5: Rk=rank (I,qk[i]+1); Out (Rk>qr[i]-ql[i]? Inf:kth (I,rk+1));p C ('\ n'); }} fwrite (Obuf,1, po-obuf,stdout);return 0;}

Rokua P3380 "template" Two-force balance tree (tree set tree, tree array, segment 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.