左偏树用来合并堆,并查集用来维护所在堆的堆顶编号。
然而有的时候要删除堆顶x,然后对并查集进行这样的操作:fa[x]:=merge(x^.l,x^.r); fa[fa[x]]:=fa[x]。这样就给并查集换了个根。
注意被杀了的点不要真正扔出堆,要不然会影响并查集的getfa,打一个kill标记不管它就是了。
代码:
type
heap=^heapnode;
heapnode=record
t,dist,id:longint;
l,r:heap;
end;
var
n,m,i,j,x,y:longint;
opt:char;
h:array[0..1001000]of heap;
kill:array[0..1001000]of boolean;
fa:array[0..1001000]of longint;
function getfa(v:longint):longint;
begin
if fa[v]=v then exit(v);
fa[v]:=getfa(fa[v]);
exit(fa[v]);
end;
procedure swap(var x,y:heap);
var
t:heap;
begin
t:=x;
x:=y;
y:=t;
end;
function merge(x,y:heap):heap;
begin
if x=nil then exit(y);
if y=nil then exit(x);
if x^.t>y^.t then swap(x,y);
x^.r:=merge(x^.r,y);
if (x^.l=nil)or((x^.r<>nil)and(x^.l^.dist<x^.r^.dist)) then swap(x^.l,x^.r);
if x^.r=nil then x^.dist:=0
else x^.dist:=x^.r^.dist+1;
exit(x);
end;
procedure mer(x,y:longint);
var
tt:longint;
begin
if x=y then exit;
tt:=merge(h[x],h[y])^.id;
fa[x]:=tt;
fa[y]:=tt;
end;
function del(x:longint):longint;
var
p,q:heap;
begin
del:=h[x]^.t;
p:=h[x]^.l;
q:=h[x]^.r;
if (p=nil)and(q=nil) then exit;
fa[x]:=merge(p,q)^.id;
fa[fa[x]]:=fa[x];
end;
begin
readln(n);
for i:=1 to n do
begin
new(h[i]);
read(h[i]^.t);
h[i]^.dist:=0;
h[i]^.id:=i;
h[i]^.l:=nil;h[i]^.r:=nil;
fa[i]:=i;
end;
fillchar(kill,sizeof(kill),false);
readln(m);
for i:=1 to m do
begin
read(opt);
if opt='M' then
begin
readln(x,y);
if kill[x] or kill[y] then continue;
x:=getfa(x);
y:=getfa(y);
mer(x,y);
end;
if opt='K' then
begin
readln(x);
if kill[x] then begin writeln(0); continue; end;
x:=getfa(x);
kill[x]:=true;
writeln(del(x));
end;
end;
end.