Cod sursa(job #408663)

Utilizator hungntnktpHungntnktp hungntnktp Data 3 martie 2010 10:09:47
Problema Hashuri Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.95 kb
{FP}
{$M 64000000,0}
{$MODE OBJFPC}
{$IFDEF ANHDQ}
  {$INLINE OFF}
  {$H-,I+,Q+,R+,S+}
{$ELSE}
  {$INLINE ON}
  {$H+,I-,Q-,R-,S-}
{$ENDIF}

// Result:
program hashuri_AnhDQ;

const
  FI_NAME = 'hashuri.in';
  FO_NAME = 'hashuri.out';
  PHash   = 1000003;

type
  HLink = ^HNode;
  HNode = record
            x   : LongInt;
            next: HLink;
          end;

var
  n, t, x: LongInt;
  H      : array[0..PHash - 1] of HLink;
(*------------------------------*)
  procedure Add(x: LongInt); inline;
  var
    xx  : LongInt;
    node: HLink;
  begin
    xx := x mod PHash;
    node := H[xx];
    while node <> nil do
    begin
      if node^.x = x then exit;
      node := node^.next;
    end;
    New(node);
    node^.x := x;
    node^.next := H[xx];
    H[xx] := node;
  end;
(*------------------------------*)
  procedure Del(x: LongInt); inline;
  var
    xx        : LongInt;
    last, node: HLink;
  begin
    xx := x mod PHash;
    if (H[xx] <> nil) and (H[xx]^.x = x) then
    begin
      H[xx] := H[xx]^.next;
      exit;
    end;
    if H[xx] = nil then exit;
    last := H[xx];
    node := last^.next;
    while node <> nil do
    begin
      if node^.x = x then
      begin
        last^.next := node^.next;
        exit;
      end;
      last := node;
      node := node^.next;
    end;
  end;
(*------------------------------*)
  function Find(x: LongInt): LongInt; inline;
  var
    xx  : LongInt;
    node: HLink;
  begin
    xx := x mod PHash;
    node := H[xx];
    while node <> nil do
    begin
      if node^.x = x then exit(1);
      node := node^.next;
    end;
    exit(0);
  end;
(*------------------------------*)
begin
  Assign(Input, FI_NAME); Reset(Input);
  Assign(Output, FO_NAME); Rewrite(Output);
  read(n);
  while n > 0 do
  begin
    Dec(n);
    read(t, x);
    case t of
      1: Add(x);
      2: Del(x);
      3: WriteLn(Find(x));
    end;
  end;
end.