Pagini recente » Cod sursa (job #1167978) | Cod sursa (job #3340782) | Cod sursa (job #1167967) | Cod sursa (job #3340630) | Cod sursa (job #1167979)
program heap;
var H,sol,coada : array [1..200000] of longint;
k,i,n,a,b,t,j,l,stinga,dreapta,mijloc,prim,ultim,aux,poz: 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(n,k : longint);
var fiu : longint;
begin
repeat
fiu:=0;
if k*2<=n then
begin
fiu:=k*2;
if (k<n) and (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(H[k],H[fiu]);
k:=fiu;
end;
until fiu=0;
end;
procedure ridica(k : longint);
var aux: longint;
begin
while (k<>1) and (H[k]<H[k div 2]) do begin
swap(H[k],H[k div 2]);
k:=k div 2;
end;
end;
procedure sterge(var n : longint ; k : longint);
begin
H[k]:=H[n];
n:=n-1;
if (k<>1) and (H[k]<H[k div 2]) then ridica(k)
else coboara(n,k);
end;
procedure inserare(var n,l : longint; k : longint) ;
begin
n:=n+1;
H[n]:=k;
l:=l+1;
sol[l]:=k;
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
readln;
writeln(H[1]);
end
else
if a=2 then begin
readln(b);
prim:=1; ultim:=1;
coada[1]:=1;
aux:=0;
while (prim<=ultim) and (aux=0) do begin
poz:=coada[prim];
prim:=prim+1;
if H[poz]=sol[b] then aux:=poz
else
if (poz*2+1<=n) and (H[poz*2+1]<=sol[b]) then begin
ultim:=ultim+1;
coada[ultim]:=poz*2+1;
end
else
if (poz*2<=n) and (H[poz*2]<=sol[b]) then begin
ultim:=ultim+1;
coada[ultim]:=poz*2;
end;
end;
sterge(n,aux);
end
else
if a=1 then begin
readln(b);
inserare(n,l,b);
end;
end;
close(input);
close(output);
end.