Pagini recente » Cod sursa (job #2258209) | Cod sursa (job #1475578) | Cod sursa (job #2738647) | Cod sursa (job #1951794) | Cod sursa (job #760655)
Cod sursa(job #760655)
Program heapuri;
const inf=2000000000;
var h:array [0..1 shl 19] of longint;
a:array [1..200000] of longint;
b1,b2:array [1..1 shl 17] of char;
n,lev,nr,op,x,i:longint;
fi,fo:text;
procedure urca(nod:longint);
var aux:longint;
begin
while (nod>=1) and (h[nod div 2]>h[nod]) do begin
aux:=h[nod];
h[nod]:=h[nod div 2];
h[nod div 2]:=aux;
nod:=nod div 2;
end;
end;
procedure coboara(nod:longint);
var aux:longint;
begin
if h[nod]>h[2*nod] then begin aux:=h[nod]; h[nod]:=h[2*nod]; h[2*nod]:=aux; coboara(2*nod); end
else if h[nod]>h[2*nod+1] then begin aux:=h[nod]; h[nod]:=h[2*nod+1]; h[2*nod+1]:=aux; coboara(2*nod+1); end;
end;
function poz(nod,val:longint):longint;
begin
if h[nod]=val then poz:=nod
else begin
if h[2*nod]<=val then poz:=poz(2*nod,val);
if h[2*nod+1]<=val then poz:=poz(2*nod+1,val);
end;
end;
procedure insert(x:longint);
var nod:longint;
begin
inc(nr); h[nr]:=x; nod:=nr; inc(lev); a[lev]:=x;
if h[nod]<h[nod div 2] then urca(nod);
end;
procedure erase(x:longint);
var nod:longint;
begin
nod:=poz(1,a[x]);
h[nod]:=h[nr]; h[nr]:=inf; dec(nr);
if h[nod]<h[nod div 2] then urca(nod)
else if (h[nod]>h[2*nod]) or (h[nod]>h[2*nod+1]) then coboara(nod);
end;
begin
assign(fi,'heapuri.in');
assign(fo,'heapuri.out');
settextbuf(fi,b1); settextbuf(fo,b2);
reset(fi); rewrite(fo); readln(fi,n); for i:=1 to 2*n do h[i]:=inf; h[0]:=-inf;
for i:=1 to n 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); erase(x); end
else begin readln(fi); writeln(fo,h[1]); end;
end;
close(fo);
end.