Pagini recente » Cod sursa (job #1205960) | Cod sursa (job #2340296) | Cod sursa (job #1679102) | Cod sursa (job #1805509) | Cod sursa (job #1179512)
program heap;
var H,poz_intr : array [1..100000] of longint;
poz_heap : array [1..199999999] of longint;
k,i,n,a,b,t,j,l,nod: longint;
b1,b2 : array[0..1 shl 17] of char;
procedure swap(var x,y : longint);
var aux : longint;
begin
aux:=x;
x:=y;
y:=aux;
end;
procedure coboara(k : longint);
var fiu : longint;
begin
repeat
fiu:=0;
if k*2<=n then
begin
fiu:=k*2;
if (k*2<n) then
if (H[k*2+1]<H[k*2]) then fiu:=k*2+1;
if H[fiu]>=H[k] then fiu:=0
end;
if fiu<>0 then begin
swap(poz_heap[H[k]],poz_heap[H[fiu]]);
swap(H[k],H[fiu]);
k:=fiu;
end;
until fiu=0;
end;
procedure ridica(nod : longint);
begin
if (nod>1) and (H[nod]<=H[nod div 2]) then begin
swap(poz_heap[H[nod]],poz_heap[H[nod div 2]]);
swap(H[nod],H[nod div 2]);
ridica(nod div 2);
end;
end;
procedure sterge(var n : longint ; nod : longint);
begin
swap(poz_heap[H[nod]],poz_heap[H[n]]);
swap(H[nod],H[n]);
n:=n-1;
if (nod>1) and (H[nod]<H[nod div 2]) then ridica(nod)
else coboara(nod);
end;
procedure inserare(var n,l : longint; nod : longint) ;
begin
n:=n+1;
H[n]:=nod;
l:=l+1;
poz_intr[l]:=nod;
poz_heap[nod]:=n;
ridica(n);
end;
begin
assign(input,'heapuri.in'); settextbuf(input,b1);reset(input);
assign(output,'heapuri.out'); settextbuf(output,b2);rewrite(output);
readln(t);
for k:=1 to t do begin
read(a);
if a=3 then begin
writeln(H[1]);
readln;
end
else
if a=2 then begin
readln(b);
sterge(n,poz_heap[poz_intr[b]]);
end
else
if a=1 then begin
readln(b);
inserare(n,l,b);
end;
end;
close(input);
close(output);
end.