bzoj 3196二逼平衡树 线段树套平衡树

时间:2023-03-08 20:21:31

比较裸的树套树,对于区间K值bz上有一道裸题,详见题解http://www.cnblogs.com/BLADEVIL/p/3455336.html(其实题解也不是很详细)

//By BLADEVIL
type
rec =record
left, right, root :longint;
end; var
n, m :longint;
a :array[..] of longint;
t :array[..] of rec;
b_left, b_right, b_key :array[..] of longint;
b_size :array[..] of longint;
tot :longint; function min(a,b:longint):longint;
begin
if a>b then min:=b else min:=a;
end; function max(a,b:longint):longint;
begin
if a>b then max:=a else max:=b;
end; procedure left_rotate(var t:longint);
var
k :longint;
begin
k:=b_right[t];
b_right[t]:=b_left[k];
b_left[k]:=t;
b_size[k]:=b_size[t];
b_size[t]:=b_size[b_left[t]]+b_size[b_right[t]]+;
t:=k;
end; procedure right_rotate(var t:longint);
var
k :longint;
begin
k:=b_left[t];
b_left[t]:=b_right[k];
b_right[k]:=t;
b_size[k]:=b_size[t];
b_size[t]:=b_size[b_left[t]]+b_size[b_right[t]]+;
t:=k;
end; procedure maintain(var t:longint;flag:boolean);
begin
if not flag then
begin
if b_size[b_left[b_left[t]]]>b_size[b_right[t]] then
right_rotate(t) else
if b_size[b_right[b_left[t]]]>b_size[b_right[t]] then
begin
left_rotate(b_left[t]);
right_rotate(t);
end else exit;
end else
begin
if b_size[b_right[b_right[t]]]>b_size[b_left[t]] then
left_rotate(t) else
if b_size[b_left[b_right[t]]]>b_size[b_left[t]] then
begin
right_rotate(b_right[t]);
left_rotate(t);
end else exit;
end;
maintain(b_left[t],false);
maintain(b_right[t],true);
maintain(t,true);
maintain(t,false);
end; procedure insert(var t:longint;v:longint);
begin
if t= then
begin
inc(tot);
t:=tot;
b_left[t]:=;
b_right[t]:=;
b_size[t]:=;
b_key[t]:=v;
end else
begin
inc(b_size[t]);
if v>=b_key[t] then insert(b_right[t],v) else insert(b_left[t],v);
maintain(t,v>=b_key[t]);
end;
end; function delete(var t:longint; v:longint):longint;
begin
dec(b_size[t]);
if (b_key[t]=v) or (b_key[t]<v) and (b_right[t]=) or (b_key[t]>v) and (b_left[t]=) then
begin
delete:=b_key[t];
if (b_left[t]=) or (b_right[t]=) then
t:=b_left[t]+b_right[t] else
b_key[t]:=delete(b_left[t],v+);
end else
if v>b_key[t] then delete:=delete(b_right[t],v) else delete:=delete(b_left[t],v);
end; procedure build(x,l,r:longint);
var
mid :longint;
i :longint;
begin
t[x].left:=l; t[x].right:=r; t[x].root:=;
for i:=l to r do insert(t[x].root,a[i]);
if l=r then exit;
with t[x] do mid:=(left+right) div ;
build(x*,l,mid); build(x*+,mid+,r);
end; function b_rank(var t:longint;v:longint):longint;
begin
if t= then exit();
if v<=b_key[t] then
b_rank:=b_rank(b_left[t],v) else
b_rank:=b_rank(b_right[t],v)+b_size[b_left[t]]+;
end; procedure init;
var
i :longint; begin
read(n,m);
for i:= to n do read(a[i]);
build(,,n);
end; function rank(x,l,r,y:longint):longint;
var
mid :longint;
begin
if (t[x].left=l) and (t[x].right=r) then
begin
rank:=b_rank(t[x].root,y);
exit;
end;
with t[x] do mid:=(left+right) div ;
if mid<l then rank:=rank(x*+,l,r,y) else
if mid>=r then rank:=rank(x*,l,r,y) else
rank:=rank(x*,l,mid,y)+rank(x*+,mid+,r,y);
end; procedure askrank(l,r,x:longint);
begin
writeln(rank(,l,r,x)+);
end; procedure change(x,y,z,c:longint);
var
mid :longint;
begin
mid:=delete(t[x].root,c);
insert(t[x].root,z);
if t[x].left=t[x].right then exit;
with t[x] do mid:=(left+right) div ;
if y>mid then change(x*+,y,z,c) else
change(x*,y,z,c);
end; function b_pred(var t:longint;v:longint):longint;
begin
if t= then exit(-);
if v<=b_key[t] then b_pred:=b_pred(b_left[t],v) else
begin
b_pred:=b_pred(b_right[t],v);
if b_pred=- then b_pred:=b_key[t];
end;
end; function pred(x,l,r,y:longint):longint;
var
mid :longint;
begin
if (t[x].left=l) and (t[x].right=r) then
begin
pred:=b_pred(t[x].root,y);
exit;
end;
with t[x] do mid:=(left+right) div ;
if mid<l then pred:=pred(x*+,l,r,y) else
if mid>=r then pred:=pred(x*,l,r,y) else
pred:=max(pred(x*,l,mid,y),pred(x*+,mid+,r,y));
end; procedure askpred(l,r,x:longint);
begin
writeln(pred(,l,r,x));
end; function b_succ(var t:longint;v:longint):longint;
begin
if t= then exit(maxlongint);
if v>=b_key[t] then b_succ:=b_succ(b_right[t],v) else
begin
b_succ:=b_succ(b_left[t],v);
if b_succ=maxlongint then b_succ:=b_key[t];
end;
end; function succ(x,l,r,y:longint):longint;
var
mid :longint;
begin
if (t[x].left=l) and (t[x].right=r) then
begin
succ:=b_succ(t[x].root,y);
exit;
end;
with t[x] do mid:=(left+right) div ;
if mid<l then succ:=succ(x*+,l,r,y) else
if mid>=r then succ:=succ(x*,l,r,y) else
succ:=min(succ(x*,l,mid,y),succ(x*+,mid+,r,y));
end; procedure askselect(a,b,x:longint);
var
mid :longint;
ans :longint;
l, r :longint;
xx :longint;
begin
l:=; r:=;
while l<=r do
begin
mid:=(l+r) div ;
xx:=rank(,a,b,mid)+;
if xx<=x then
begin
ans:=mid;
l:=mid+;
end else r:=mid-;
end;
if rank(,a,b,ans)=x then ans:=succ(,l,r,ans-) else ans:=ans;
writeln(ans);
end; procedure asksucc(l,r,x:longint);
begin
writeln(succ(,l,r,x));
end; procedure main;
var
i :longint;
k, l, r, x :longint; begin
for i:= to m do
begin
read(k);
if k= then
begin
read(l,r,x);
askrank(l,r,x);
end else
if k= then
begin
read(l,r,x);
askselect(l,r,x);
end else
if k= then
begin
read(l,x);
change(,l,x,a[l]);
a[l]:=x;
end else
if k= then
begin
read(l,r,x);
askpred(l,r,x);
end else
if k= then
begin
read(l,r,x);
asksucc(l,r,x);
end;
end;
end; begin
init;
main;
end.