Balance Tree Bare Topic
Only require the precursor to drive
Treap notation
Constmm=1<< -; MaxNumber=500000; MAXN=500000;varLeft,right,fix,key:Array[0.. MAXN] ofLongint; T,n,ans,i,j,k,l,tot:longint;functionmin (x,y:longint): Longint;begin ifX<y Thenexit (x); Exit (y);End;functionMax (x,y:longint): Longint;begin ifX<y Thenexit (y); Exit (x);End;procedureLtvart:longint);varK:longint;beginK:=Right[t]; RIGHT[T]:=Left[k]; LEFT[K]:=T; T:=K;End;procedureRtvart:longint);varK:longint;beginK:=Left[t]; LEFT[T]:=Right[k]; RIGHT[K]:=T; T:=K;End;procedureInsertvart:longint;v:longint);begin ift=0 Then beginInc (TOT); T:=tot; KEY[T]:=v; FIX[T]:=random (MaxNumber) +1; LEFT[T]:=0; RIGHT[T]:=0; Exit End; ifV<=KEY[T] Then beginInsert (LEFT[T],V); ifFIX[LEFT[T]]>FIX[T] ThenRT (t); End Else beginInsert (RIGHT[T],V); ifFIX[RIGHT[T]]>FIX[T] ThenLT (t); End;End;functionpred (t,v:longint): Longint;begin ift=0 ThenExit (-mm); ifV=KEY[T] Thenexit (v); ifV<KEY[T] Thenexit (pred (LEFT[T],V))Elseexit (Max (key[t],pred (right[t],v)));End;functionsucc (t,v:longint): Longint;begin ift=0 Thenexit (mm); ifV=KEY[T] Thenexit (v); ifV<KEY[T] Thenexit (Min (KEY[T],SUCC (left[t],v)))Elseexit (SUCC (RIGHT[T],V));End;beginT:=0; READLN (n); Read (j); Insert (T,J); Ans:=J; fori:=2 toN Do beginRead (j); K:=pred (T,J); L:=succ (T,J); Writeln (J,' 'K' ', L); ifJ-k<l-j Thenans:=ans+j-kElseans:=ans+l-J; Insert (T,J); End; Writeln (ANS); READLN; READLN;End.
View Code
bzoj1588 [HNOI2002] turnover statistics (balance tree)