Cod sursa(job #338755)

Utilizator sapiensCernov Vladimir sapiens Data 6 august 2009 20:14:31
Problema Heapuri Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.65 kb
Program heapuri;
 var f,g:text; heap:array[0..400001]of longint;
     b:array[1..200000]of 1..200000;
     c:array[1..200000]of 1..200000;
     i,j,l,k,num,n:longint;
 procedure init_heap;
  var x:longint;
  begin
   heap[0]:=-maxlongint-1;
   for x:=1 to 400001 do heap[x]:=maxlongint;
   num:=0;
  end;
 procedure swap (x,y:longint);
  var z:longint;
  begin
   z:=heap[x];
   heap[x]:=heap[y];
   heap[y]:=z;
   z:=b[c[x]];
   b[c[x]]:=b[c[y]];
   b[c[y]]:=z;
   z:=c[x];
   c[x]:=c[y];
   c[y]:=z;
  end;
 procedure cerne (x:longint);
  var y:longint;
  begin
   if heap[2*x]<heap[2*x+1] then y:=2*x else y:=2*x+1;
   if heap[y]<heap[x] then begin
     swap (x,y);
     cerne (y);
   end;
  end;
 procedure ridica (x:longint);
  begin
   if heap[x div 2]>heap[x] then begin
     swap (x div 2,x);
     ridica (x div 2);
   end;
  end;
 procedure adauga (x:longint);
  begin
   num:=num+1;
   k:=k+1;
   heap[num]:=x;
   b[k]:=num;
   c[num]:=k;
   ridica (num);
  end;
 procedure sterge (x:longint);
  var y:longint;
  begin
   y:=b[x];
   swap (b[x],num);
   heap[num]:=maxlongint;
   num:=num-1;
   cerne (y);
   ridica (y);
  end;
 begin
  assign (f,'heapuri.in'); reset (f);
  assign (g,'heapuri.out'); rewrite (g);
  readln (f,n);
  init_heap;
  k:=0;
  for i:=1 to n do begin
    read (f,j);
    case j of
      1: begin
           readln (f,l);
           adauga (l);
         end;
      2: begin
           readln (f,l);
           sterge (l);
         end;
      3: begin
           writeln (g,heap[1]);
           readln (f);
         end;
    end;
  end;
  close (f); close (g);
 end.