Pagini recente » Cod sursa (job #1813863) | Cod sursa (job #303874) | Cod sursa (job #2373036) | Cod sursa (job #1855468) | Cod sursa (job #581395)
Cod sursa(job #581395)
const
nmax=200000;
type
heap=array[1..nmax] of longint;
var
h,v,pos:heap;
n,i,w,ww,nh,m:longint;
function father(nod:longint):longint; begin father:=nod div 2; end;
function left_son(nod:longint):longint; begin left_son:=2*nod; end;
function right_son(nod:longint):longint; begin right_son:=2*nod+1; end;
procedure swap(var a,b:longint); begin a:=a+b; b:=a-b; a:=a-b; end;
procedure percolate(k:longint);
var
key,keyp:longint;
begin
key:=h[k];
keyp:=k;
while (k>1) and (v[key] < v[h[father(k)]]) do begin
pos[h[father(k)]]:=k;
h[k]:=h[father(k)];
k:=father(k);
end;
h[k]:=key;
pos[keyp]:=k;
end;
procedure sift(k:longint);
var
son:longint;
begin
repeat
son:=0;
if left_son(k)<=nh then begin
son:=left_son(k);
if (right_son(k)<=nh) and (v[h[right_son(k)]] < v[h[left_son(k)]]) then son:=right_son(k);
if v[h[son]] >= v[h[k]] then son:=0;
end;
if son>0 then begin
swap(pos[h[son]],pos[h[k]]);
swap(h[son],h[k]);
k:=son;
end;
until son=0;
end;
procedure insert(key:longint);
begin
inc(n);inc(nh); //incrementez lungimea vectorului si a heap-ului
v[n]:=key; //adaug valoarea in vector
h[nh]:=n; //adaug pozitia din vector in heap
pos[n]:=nh; //adaug pozitia din heap in vector
percolate(nh);
{write('h='); //debugging
for j:=1 to n do write(h[j],' ');
writeln;
write('p=');
for j:=1 to n do write(pos[j],' ');
writeln;}
end;
procedure cut(k:longint);
begin
pos[h[nh]]:=k;
h[k]:=h[nh];
k:=pos[h[k]];
dec(nh);
if (k>1) and (v[h[k]] < v[h[father(k)]]) then percolate(k) else sift(k);
end;
begin
assign(input,'heapuri.in');reset(input);
assign(output,'heapuri.out');rewrite(output);
nh:=0;
n:=0;
readln(m);
for i:=1 to m do begin
read(w);
case w of
1:begin read(ww); insert(ww); end;
2:begin read(ww); cut(pos[ww]); end;
3:begin writeln(v[h[1]]); end;
end;
end;
end.