Algorithm template--balance tree treap 2

Source: Internet
Author: User

Realization function: With balance tree treap 1 (bzoj3224/tyvj1728)

This template has a lot of improvements, obviously more beautiful, almost every part has a lot of simplification, especially the deletion of the section, this reference to the Hzwer god Ben Writing, in this acknowledgement, and then, paste template to leave

1 var2 I,j,k,l,m,n,head,tot:longint;3A,b,lef,rig,fix:Array[0..100010] ofLongint;4 functionmin (x,y:longint): Longint;5          begin6               ifX<y ThenMin:=xElsemin:=y;7          End;8 functionMax (x,y:longint): Longint;9          beginTen               ifX>y ThenMax:=xElsemax:=y; One          End; A procedureLtvarx:longint); -           varF,r:longint; -           begin the                if(x=0)or(rig[x]=0) Thenexit; -b[rig[x]]:=b[x];b[x]:=b[lef[x]]+b[lef[rig[x]]]+1; -f:=x;r:=Rig[x]; -rig[f]:=Lef[r]; +lef[r]:=F; -x:=R; +           End; A procedureRtvarx:longint); at           varF,l:longint; -           begin -                if(x=0)or(lef[x]=0) Thenexit; -b[lef[x]]:=b[x];b[x]:=b[rig[x]]+b[rig[lef[x]]]+1; -f:=x;l:=Lef[x]; -lef[f]:=Rig[l]; inrig[l]:=F; -x:=l; to           End; + functionNEWP (x:longint): Longint; -          begin theInc (TOT); newp:=tot; *a[tot]:=x;b[tot]:=1; $lef[tot]:=0; rig[tot]:=0;Panax Notoginsengfix[tot]:=random (maxlongint); -          End; the procedureInsvarx:longint;y:longint); +           begin A                ifx=0  Then the                   begin +x:=newp (y); - exit; $                   End; $                ifY&LT;A[X] Then -                   begin -Ins (lef[x],y); b[x]:=b[lef[x]]+b[rig[x]]+1; the                        ifFIX[LEF[X]]&LT;FIX[X] ThenRT (x); -                   EndWuyi                Else the                    begin -Ins (rig[x],y); b[x]:=b[lef[x]]+b[rig[x]]+1; Wu                         ifFIX[RIG[X]]&LT;FIX[X] ThenLT (x); -                    End; About           End; $ procedureDelvarx:longint;y:longint); -           begin -                ifx=0  Thenexit; -                ifA[x]=y Then A                   begin +                        iflef[x]=0  ThenX:=RIG[X]Else the                        ifrig[x]=0  ThenX:=LEF[X]Else -                        ifFIX[LEF[X]]&GT;FIX[RIG[X]] Then $                           begin the LT (x);d El (lef[x],y); theb[x]:=b[lef[x]]+b[rig[x]]+1; the                           End the                        Else -                            begin in RT (x);d El (rig[x],y); theb[x]:=b[lef[x]]+b[rig[x]]+1; the                            End; About                   End the                Else ifY&LT;A[X] Then the                     begin the del (lef[x],y); +b[x]:=b[lef[x]]+b[rig[x]]+1; -                     End the                Else beginBayi del (rig[x],y); theb[x]:=b[lef[x]]+b[rig[x]]+1; the                     End; -           End; - functionGetRank (x,y:longint): Longint; the          begin the               ifx=0  ThenExit0); the               ifY&GT;A[X] ThenExit (b[lef[x]]+1+getrank (Rig[x],y))Elseexit (GetRank (Lef[x],y)); the          End; - functionRankget (x,y:longint): Longint; the          begin the               ifY= (b[lef[x]]+1) Thenexit (A[x]); the               ify< (b[lef[x]]+1) ThenExit (Rankget (Lef[x],y))ElseExit (Rankget (rig[x],y-1-b[lef[x]));94          End; the functionGetpre (x,y:longint): Longint; the          begin the               ifx=0  ThenExit (-maxlongint);98               ifA[x]<y ThenExit (Max (A[x],getpre (rig[x],y)))Elseexit (Getpre (Lef[x],y)); About          End; - functiongetsuc (x,y:longint): Longint;101          begin102               ifx=0  Thenexit (maxlongint);103               ifA[x]>y ThenExit (Min (A[X],GETSUC (lef[x],y)))Elseexit (GETSUC (Rig[x],y));104          End; the begin106READLN (n); tot:=0; head:=0;107       fori:=1  toN Do108          begin109 readln (j,k); the                CaseJ of111                    1: Ins (head,k); the                    2:d El (head,k);113                    3: Writeln (GetRank (head,k) +1); the                    4: Writeln (Rankget (head,k)); the                    5: Writeln (Getpre (head,k)); the                    6: Writeln (GETSUC (head,k));117               End;118          End;119 End.

Algorithm template--balance tree treap 2

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.