HNOI2002营业额统计(平衡树)

时间:2021-02-10 06:41:24

标准的平衡树。

贴个splay吧

var
v,l,r,fa:array[..] of longint;
root,x,i,n,ans:longint;
procedure zig(x:longint);
var y,z:longint;
begin
y:=fa[x];z:=fa[y];
if root=y then root:=x;
l[y]:=r[x];
if r[x]<> then fa[r[x]]:=y;
r[x]:=y;
fa[y]:=x;
fa[x]:=z;
if z= then exit;
if l[z]=y then l[z]:=x else r[z]:=x;
end;
procedure zag(x:longint);
var y,z:longint;
begin
y:=fa[x];z:=fa[y];
if root=y then root:=x;
r[y]:=l[x];
if l[x]<> then fa[l[x]]:=y;
l[x]:=y;
fa[y]:=x;
fa[x]:=z;
if z= then exit;
if l[z]=y then l[z]:=x else r[z]:=x;
end;
procedure splay(x:longint);
var y,z:longint;
begin
while x<>root do
begin
y:=fa[x];
if y=root then
begin
if x=l[y] then zig(x) else zag(x);
exit;
end;
z:=fa[y];
if l[y]=x then
if l[z]=y then
begin
zig(y);zig(x)
end
else
begin
zig(x);zag(x);
end
else
if r[z]=y then
begin
zag(y);zag(x);
end
else
begin
zag(x);zig(x);
end;
end;
end;
procedure insert(x,k:longint);
var i,j:longint;
begin
if root= then
begin
root:=k;
v[k]:=x;l[k]:=;r[k]:=;fa[k]:=;
exit;
end;
i:=root;
v[k]:=x;
while true do
begin
if x<=v[i] then j:=l[i] else j:=r[i];
if j= then
begin
if x<=v[i] then l[i]:=k else r[i]:=k;
fa[k]:=i;
break;
end;
i:=j;
end;
splay(k);
end;
function min(x,k:longint):longint;
var i,j,tmp:longint;
begin
tmp:=maxlongint;
i:=l[k];
while i<> do
begin
if abs(v[i]-x)<tmp then begin tmp:=abs(v[i]-x);j:=i;end;
i:=r[i];
end;
i:=r[k];
while i<> do
begin
if abs(v[i]-x)<tmp then begin tmp:=abs(v[i]-x);j:=i;end;
i:=l[i];
end;
splay(j);
exit(tmp);
end;
begin
read(n);
ans:=;
for i:= to n do
begin
read(x);
insert(x,i);
if i<> then ans:=ans+min(x,i) else ans:=x;
end;
writeln(ans);
end.