Pagini recente » Cod sursa (job #2886051) | Cod sursa (job #2967545) | Cod sursa (job #2969667) | Cod sursa (job #2082284) | Cod sursa (job #1167591)
program heapuri;
type heap=array[0..200010] of longint;
var h,poz:heap;
n,m,i,j,k,op,nr,x:longint;
procedure swap(var a,b:longint);
var aux:longint;
begin
aux:=a;
a:=b;
b:=aux;
end;
procedure heapdown(n,k:longint);
var son:longint;
begin
repeat
son:=0;
if 2*k<=n then begin
son:=2*k;
if (2*k+1<=n) and (h[2*k+1]<h[2*k])then son:=2*k+1;
if h[k]<h[son] then son:=0;
end;
if son<>0 then begin
swap(h[k],h[son]);
swap(poz[k],poz[son]);
k:=son;
end;
until son=0;
end;
procedure heapup(n,k:longint);
var key,l:longint;
begin
key:=h[k];
l:=poz[k];
while (k>1) and (h[k]<h[k div 2]) do begin
h[k]:=h[k div 2];
poz[k]:=poz[k div 2];
k:=k div 2;
end;
poz[k]:=l;
h[k]:=key;
end;
procedure delete(var n:longint;k:longint);
begin
h[k]:=h[n];
n:=n-1;
if (k>1) and (h[k]<h[k div 2]) then heapup(n,k)
else heapdown(n,k);
end;
procedure insert(var n:longint; key:longint);
begin
inc(n);
h[n]:=key;
poz[nr]:=n;
heapup(n,k);
end;
procedure builtheap(var n:longint);
var i:longint;
begin
for i:=n div 2 downto 1 do
heapdown(n,i);
end;
procedure hsort(n:longint);
begin
builtheap(n);
for i:=n downto 1 do
begin
swap(h[i],h[1]);
heapdown(i-1,1);
end;
end;
begin
assign(input,'heapuri.in');
assign(output,'heapuri.out');
reset(input);
rewrite(output);
readln(m);
for i:=1 to m do
begin
read(op);
if op=3 then begin readln; writeln(h[1]) end
else readln(x);
if op=1 then begin
inc(nr);
insert(n,x);
end
else
delete(n,poz[x]);
end;
close(input);
close(output);
end.