Pagini recente » Cod sursa (job #649797) | Cod sursa (job #529882) | Cod sursa (job #1866101) | Cod sursa (job #1338808) | Cod sursa (job #760705)
Cod sursa(job #760705)
Program heapuri;
var v,h,poz:array [1..1 shl 18] of longint;
b1,b2:array [1..1 shl 17] of char;
n,m,hp,i,op,x:longint;
fi,fo:text;
procedure swap(var a,b:longint);
var aux:longint;
begin
aux:=a; a:=b; b:=aux;
end;
procedure urca(k:longint);
begin
while (k>1) and (v[h[k]]<v[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 coboara(k:longint);
var nod:longint;
begin
nod:=1;
while nod>0 do begin
nod:=0;
if 2*k<=hp then begin
nod:=2*k;
if (2*k+1<=hp) and (v[h[nod]]>v[h[nod+1]]) then inc(nod);
if v[h[nod]]>=v[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 insert(x:longint);
begin
inc(hp); inc(n); v[n]:=x; h[hp]:=n; poz[n]:=hp;
urca(hp);
end;
procedure delet(k:longint);
begin
poz[h[hp]]:=k; swap(h[k],h[hp]); dec(hp);
if (k>1) and (v[h[k]]<v[h[k div 2]]) then urca(k) else coboara(k);
end;
begin
assign(fi,'heapuri.in');
assign(fo,'heapuri.out');
settextbuf(fi,b1); settextbuf(fo,b2);
reset(fi); rewrite(fo); readln(fi,m);
for i:=1 to m do begin
read(fi,op);
if op=1 then begin readln(fi,x); insert(x); end
else if op=2 then begin readln(fi,x); delet(poz[x]); end
else begin readln(fi); writeln(fo,v[h[1]]); end;
end;
close(fo);
end.