Cod sursa(job #1170529)

Utilizator wollyFusy Wool wolly Data 13 aprilie 2014 19:30:59
Problema Heapuri Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.07 kb
uses crt;
type tab=array[0..200100] of longint;
	heap=array[0..200100] of longint;
var a,b:text;
	num,op,n,i,j:longint;
	h:heap;
	t:tab;
	
procedure add(num,p:longint);
var a,b:longint;
begin
	t[p]:=p;
	h[p]:=num;
	while (h[p div 2]>h[p]) do
		begin
		a:=h[p div 2];
		b:=t[p div 2];
		h[p div 2]:=h[p];
		t[p div 2]:=t[p];
		h[p]:=a;
		t[p]:=b;		
		p:=p div 2;
		end;
end;	
	
procedure del(p:longint);
var k:longint;
begin
	k:=1;
	while (t[k]<>p) do k:=k+1;
	p:=k;
	while (p<=j) do
		begin
		if ((h[2*p]<h[2*p+1]) and (t[2*p]<>0)) or (h[2*p+1]=0) then
			begin
			h[p]:=h[2*p];
			t[p]:=t[2*p];
			p:=2*p;
			end else
			if (t[2*p+1]<>0) or (h[2*p]=0) then
				begin
				h[p]:=h[2*p+1];
				t[p]:=t[2*p+1];
				p:=2*p+1;
				end;
		end;
	j:=j-1;
end;	
	
begin
assign(a,'heap.in'); reset(a);
assign(b,'heap.out'); rewrite(b);
readln(a,n);
for i:=1 to n do
	begin
	read(a,op);
	case op of
		1:begin j:=j+1; read(a,num); add(num,j); end;
		2:begin read(a,num); del(num); end;
		3:writeln(b,h[1]);
	end;
	end;
close(a);
close(b);
end.