Pagini recente » Cod sursa (job #425761) | Cod sursa (job #2441933) | Solutii preONI 2006, Runda a 4-a | Cod sursa (job #1836296) | Cod sursa (job #929182)
Cod sursa(job #929182)
program heapuri;
type heap=array[1..200 * 1000] of longword;
var h,a,pos:heap;
n,m,r,i,op,x:longword;
f,g:text;
intrare,iesire:array[1..1 shl 17]of char;
procedure swap(var a,b:longword);
var aux:longword;
begin
aux:=a;a:=b;b:=aux;
end;
procedure insert(var n,r:longword; x:longword);
var tata,fiu:longword;
begin
r:=r+1;
n:=n+1;
h[n]:=x;
a[n]:=r;
pos[r]:=n;
fiu:=n;
tata:=fiu div 2;
while (tata>0)and(h[tata]>h[fiu]) do
begin
swap(h[tata],h[fiu]);
pos[a[tata]]:=fiu;
pos[a[fiu]]:=tata;
swap(a[tata],a[fiu]);
fiu:=tata;
tata:=fiu div 2;
end;
end;
procedure delete(var n:longword; r:longword);
var tata,fiu:longword;
begin
h[pos[r]]:=h[n];
a[pos[r]]:=a[n];
pos[a[n]]:=pos[r];
tata:=pos[r];
fiu:=2*tata;
if (fiu+1<=n)and(h[fiu+1]<h[fiu]) then inc(fiu);
while (fiu<=n)and(h[fiu]<h[tata]) do
begin
swap(h[tata],h[fiu]);
pos[a[tata]]:=fiu;
pos[a[fiu]]:=tata;
swap(a[tata],a[fiu]);
tata:=fiu;
fiu:=2*tata;
if (fiu+1<=n)and(h[fiu+1]<h[fiu])then inc(fiu);
end;
end;
begin
assign(f,'heapuri.in');reset(f);settextbuf(f,intrare);
assign(g,'heaputi.out');rewrite(g);settextbuf(g,iesire);
readln( m );
n:=0; r:=0;
for i:=1 to m do
begin
read(op);
if (op=3) then writeln(g,h[1])
else begin
read(x);
if (op=1) then insert(n,r,x)
else delete(n,x);
end;
end;
close(f);close(g);
end.