Cod sursa(job #544708)

Utilizator Luncasu_VictorVictor Luncasu Luncasu_Victor Data 1 martie 2011 23:22:44
Problema Heapuri Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.52 kb
program pheapuri;
type
        vt = array[1..200000] of longint;
var
        h ,u : vt;
        n ,x : longint;

procedure pt(c : longint);
var
        k ,p : longint;
begin
 inc(x);
 u[x] := x;
 h[x] := c;
 p := x;
 while p > 1 do
 begin
  if h[p div 2] > h[p] then
  begin
   k := h[p div 2];
   h[p div 2] := h[p];
   h[p] := k;
   u[p div 2] := p;
   u[p] := p div 2;
   p := p div 2;
  end else p := 1;
 end;
end;

procedure del(c : longint);
var
        p ,s ,d ,k : longint;
begin
 h[u[c]] := h[x];
 u[x] := c;
 dec(x);
 p := u[c];
 if h[p] <> 0 then
 while p < x do
 begin
  s := h[p] + 1; d := s;
  if (2 * p <= x) and (h[2 * p] < s) then s := h[2 * p];
  if (2 * p + 1 <= x) and (h[2 * p + 1] < d) then d := h[2 * p + 1];
  if s < d then
  begin
   k := h[2 * p];
   h[2 * p] := h[p];
   h[p] := k;
   u[2 * p] := p;
   u[p] := 2 * p;
   p := 2 * p;
  end else
  if d < s then
  begin
   k := h[2 * p + 1];
   h[2 * p + 1] := h[p];
   h[p] := k;
   u[2 * p + 1] := p;
   u[p] := 2 * p + 1;
   p := 2 * p + 1;
  end else p := x;
 end;
end;

procedure rd;
var
        f ,q : text;
        i : longint;
        p ,c : integer;
begin
        assign(f,'heapuri.in'); reset(f);
        assign(q,'heapuri.out'); rewrite(q);
 readln(f,n);
 for i := 1 to n do
 begin
  read(f,p);
  if p = 1 then
  begin
   read(f,c);
   pt(c);
  end else
  if p = 2 then
  begin
   read(f,c);
   del(c);
  end else writeln(q,h[1]);
 end;
 close(f); close(q);
end;

begin
rd;
end.