Pagini recente » Cod sursa (job #685147) | Cod sursa (job #2532332) | Cod sursa (job #1647057) | Cod sursa (job #1469960) | Cod sursa (job #929148)
Cod sursa(job #929148)
program heappp;
var j,t,q,x,n,hp:longint;
h,valoare,poz:array[0..1 shl 18] of longint;
f,g:text;
intrare,iesire:array[0..1 shl 18] of char;
procedure swap(var a:longint; var b:longint);
var aux:longint;
begin
aux:=a; a:=b; b:=aux;
end;
procedure sift(k:longint);
var nod:longint;
begin
nod:=5;
while nod>0 do
begin
nod:=0;
if 2*k<=hp then
begin
nod:=2*k;
if (2*k+1<=hp)and(valoare[h[nod]]>valoare[h[nod+1]]) then inc(nod);
if valoare[h[nod]]>=valoare[h[k]] then nod:=0;
end;
if nod<>0 then begin
swap(poz[h[nod]],poz[h[k]]);
swap(h[nod],h[k]);
k:=nod;
end;
end;
end;
procedure percolate(k:longint);
begin
while (k>1) and (valoare[h[k]]<valoare[h[k div 2]])do
begin
swap(poz[h[k]],poz[h[k div 2]]);
swap(h[k],h[k div 2]);
k:=k div 2;
end;
end;
procedure adaug(x:longint);
begin
n:=n+1; valoare[n]:=x;
hp:=hp+1; h[hp]:=n;
poz[n]:=hp;
percolate(poz[n]);
end;
procedure sterg(k:longint);
begin
poz[h[hp]]:=k;
swap(h[k],h[hp]);
hp:=hp-1;
if (k>1) and (valoare[h[k]]<valoare[h[k div 2]]) then percolate(k) else sift(k);
end;
begin
assign(f,'heapuri.in'); reset(f); settextbuf(f,intrare);
assign(g,'heapuri.out'); rewrite(g);settextbuf(g,iesire);
readln(f,q);
n:=0; hp:=0;
for j:=1 to q do
begin
read(f,t);
if t=1 then begin readln(f,x); adaug(x); end
else if t=2 then begin readln(f,x); sterg(poz[x]); end
else if t=3 then begin readln(f); writeln(g,valoare[h[1]]); end;
end;
close(f); close(g);
end.