Cod sursa(job #382750)

Utilizator philipPhilip philip Data 14 ianuarie 2010 17:30:22
Problema Hashuri Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.36 kb
const c=(sqrt(5)-1)/2;
      m=1000000;

type pnod=^nod;
     nod=record
       nr:longint;
       adr:pnod;
     end;


var n,i,op,x,hx:longint;
    hash:array[0..m] of pnod;
    p,p2,nou:pnod;

function h(x:longint):longint;
  begin
    h:=trunc(((c*x)-trunc(c*x))*m);
  end;

procedure adauga;
  begin
    hx:=h(x);
    p:=hash[hx];
    while (p<>nil) and (p^.nr<>x) do p:=p^.adr;
    if p=nil then begin
      new(nou);
      nou^.nr:=x;
      nou^.adr:=hash[hx];
      hash[hx]:=nou;
    end;
  end;

procedure sterge;
  begin
    hx:=h(x);
    p:=hash[hx];
    while (p<>nil) and (p^.nr<>x) do begin
      p2:=p;
      p:=p^.adr;
    end;
    if p<>nil then begin
      if p=hash[hx] then begin
        hash[hx]:=hash[hx]^.adr;
        dispose(p);
      end else begin
        p2^.adr:=p^.adr;
        dispose(p);
      end;
    end;
  end;

procedure cauta;
  begin
    hx:=h(x);
    p:=hash[hx];
    while (p<>nil) and (p^.nr<>x) do p:=p^.adr;
    if p=nil then writeln(0) else writeln(1);
  end;

begin
  assign(input,'hashuri.in');
  reseT(input);
  assign(output,'hashuri.out');
  rewrite(output);
  readln(n);
  for i:=0 to m do hash[i]:=nil;
  for i:=1 to n do begin
    readln(op,x);
    case op of
      1:adauga;
      2:sterge;
      3:cauta;
    end;
  end;

  close(input);
  close(output);
end.