SBT(pet)

原題:HNOI2004 寵物收養所

方法:SBT 維護

program size_balanced_tree;{$inline on}
const
    maxn=80000;
type
    point=^longint;
var
    s,l,r,w:array[0..maxn] of longint; a,b:point;
    ans,m,t,k,now,root,sto:longint;
procedure left(var i:longint);inline;
var j:longint;
begin
    j:=r[i];r[i]:=l[j];l[j]:=i;
    s[j]:=s[i];s[i]:=s[l[i]]+s[r[i]]+1;i:=j;
end;
procedure right(var i:longint);inline;
var j:longint;
begin
    j:=l[i];l[i]:=r[j];r[j]:=i;
    s[j]:=s[i];s[i]:=s[l[i]]+s[r[i]]+1;i:=j;
end;
procedure maintain(var t:longint; flag:boolean);inline;
begin
    if not flag then begin
	if s[l[l[t]]]>s[r[t]] then right(t) else
	if s[r[l[t]]]>s[r[t]] then begin left(l[t]); right(t);end else exit;
    end else begin
	if s[r[r[t]]]>s[l[t]] then left(t) else
	if s[l[r[t]]]>s[l[t]] then begin right(r[t]); left(t);end else exit;
    end;
    maintain(l[t],false);
    maintain(r[t],true);
    maintain(t,false);
    maintain(t,true);
end;
procedure insert(var x:longint; const k:longint);
begin
    if x=0 then begin inc(sto); x:=sto; w[x]:=k; s[x]:=1; exit; end;
    inc(s[x]); if k<w[x] then insert(l[x],k) else insert(r[x],k);
    maintain(x,k>=w[x]);
end;
procedure delete(var x:longint);
begin
    if (l[x]=0)or(r[x]=0) then begin x:=l[x]+r[x]; exit; end;
    left(x);right(l[x]);delete(r[l[x]]);
end;
function pred(var x:longint; const k:longint):point;
begin
    if (x=0)or(k=w[x]) then exit(@x);
    if k<w[x] then pred:=pred(l[x],k)
    else begin
	pred:=pred(r[x],k);
	if pred^=0 then exit(@x);
    end;
end;
function succ(var x:longint; const k:longint):point;
begin
    if (x=0)or(k=w[x]) then exit(@x);
    if k>w[x] then succ:=succ(r[x],k)
    else begin
	succ:=succ(l[x],k);
	if succ^=0 then exit(@x);
    end;
end;
function getch:char; var k:byte;
begin
    for k:=1 to 5 do read(getch);
end;
procedure readspace; var ch:char;
begin repeat read(ch); until ch=' '; end;

begin
    assign(input,'input.txt'); reset(input); 
    assign(output,'output.txt'); rewrite(output);
    readln(m);for m:=1 to m do begin
	readln(t,k);if root=0 then now:=t;
	if t=now then begin insert(root,k); continue; end;
	a:=pred(root,k);
	b:=succ(root,k);
	if a^=0 then begin add(ans,w[b^]-k); delete(b^); end else
	if b^=0 then begin add(ans,k-w[a^]); delete(a^); end else
	if k-w[a^]<=w[b^]-k then
		    begin add(ans,k-w[a^]); delete(a^); end else
		    begin add(ans,w[b^]-k); delete(b^); end; 
    end;
    writeln(ans);
    close(input); close(output);
end.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章