Cod sursa(job #402238)

Utilizator nickyyLal Daniel Emanuel nickyy Data 23 februarie 2010 18:10:42
Problema Heapuri Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.35 kb
const infile='heapuri.in';
  outfile='heapuri.out';
  maxn=200001;
type elem=record  inf,ind:longint; end;
var poz:array[0..maxn]of longint;
  h:array[0..maxn]of elem;
  n,vf,nr:longint;

 procedure comb(i:longint);
 var tata,fiu:longint;
   v:elem;
 begin
   v:=h[i]; tata:=i; fiu:=i shl 1;
   while(fiu<=vf)do begin
     if(fiu<vf)and(h[fiu].inf>h[fiu+1].inf)then inc(fiu);
     if(v.inf>h[fiu].inf)then begin
       poz[h[fiu].ind]:=tata;
       h[tata]:=h[fiu]; tata:=fiu; fiu:=fiu shl 1;
       end
     else fiu:=vf+1;
     end;
   h[tata]:=v; poz[h[tata].ind]:=tata;
 end;

 procedure insert(x:longint);
 var tata,fiu:longint;
 begin
   inc(vf); fiu:=vf; tata:=fiu shr 1;
   while(tata<>0)and(x<h[tata].inf)do begin
     poz[h[tata].ind]:=fiu;
     h[fiu]:=h[tata]; fiu:=tata; tata:=tata shr 1;
     end;
   inc(nr); h[fiu].ind:=nr; poz[h[fiu].ind]:=fiu; h[fiu].inf:=x;
 end;

 procedure solve;
 var i,x:longint;
   op:1..3;
 begin
   assign(input,infile); reset(input);
   assign(output,outfile); rewrite(output);
   readln(n); vf:=0; nr:=0;
   for i:=1 to n do begin
     read(op);
     case op of
       1:begin readln(x); insert(x) end;
       2:begin readln(x); h[poz[x]]:=h[vf]; dec(vf); comb(poz[x]); end;
       3:writeln(h[1].inf);
       end;
     end;
   close(input); close(output);
 end;

begin
solve;
end.