Pagini recente » Cod sursa (job #2319522) | Cod sursa (job #2096364) | Cod sursa (job #2510362) | Cod sursa (job #627734) | Cod sursa (job #1526086)
type el=record
val,poz:longint
end;
var h:array[1..200000]of el;
p:array[1..200000]of longint;
unde,n,i,nr,nh,np:longint;
f,fo:text;
tip:byte;
procedure HeapUp(pz,nrel,valoare:longint);
var aux:el;
j,a2:longint;
begin
j:=pz;
while ((j div 2)>=1)and(valoare<h[j div 2].val)do
begin
a2:=p[h[j].poz];
p[h[j].poz]:=p[h[j div 2].poz];
p[h[j div 2].poz]:=a2;
aux:=h[j];
h[j]:=h[j div 2];
h[j div 2]:=aux;
j:=j div 2;
end;
end;
procedure HeapDown(pz,nrel,valoare:longint);
var aux:el;
j,min,a2:longint;
mai_mere:boolean;
begin
j:=pz;mai_mere:=true;
while (j<nrel)and(mai_mere)do
begin
min:=j;
if 2*j<=nrel then
if h[2*j].val<h[min].val then min:=2*j;
if 2*j+1<=nrel then
if h[2*j+1].val<h[min].val then min:=2*j+1;
if min=j then
mai_mere:=false
else
begin
a2:=p[h[j].poz];
p[h[j].poz]:=p[h[min].poz];
p[h[min].poz]:=a2;
aux:=h[j];
h[j]:=h[min];
h[min]:=aux;
j:=min;
end;
end;
end;
begin
assign(f,'heapuri.in');
assign(fo,'heapuri.out');
reset(f);
rewrite(fo);
readln(f,n);
nh:=0;np:=0;
for i:=1 to n do
begin
read(f,tip);
if tip<>3 then
readln(f,nr)
else
readln(f);
case tip of
1:begin
inc(nh);
inc(np);
h[nh].val:=nr;
h[nh].poz:=np;
p[np]:=nh;
if nh>1 then HeapUp(nh,nh,nr);
end;
2:begin
unde:=p[nr];
p[h[nh].poz]:=p[nr];
h[p[nr]]:=h[nh];
p[nr]:=0;
h[nh].val:=0;
h[nh].poz:=0;
dec(nh);
if nh>1 then HeapDown(unde,nh,h[p[nr]].val);
end;
3:writeln(fo,h[1].val);
end;
end;
close(fo);
close(f);
end.