Cod sursa(job #2281576)

Utilizator isav_costinVlad Costin Andrei isav_costin Data 12 noiembrie 2018 15:20:40
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <cstdio>
#include <set>

#define MAXN 1000000
#define MOD 666013

using namespace std;

class Hash
{
  public:
    int x;
    int lista[MOD+5];
    int next[MAXN+5];
    int val[MAXN+5];

  inline void insert( int element )
  {
    x++;

    val[x]=element;
    next[x]=lista[element%MOD];
    lista[element%MOD]=x;
  }

  inline void erase( int element )
  {
    int p=lista[element%MOD];

    if( p==0 )
      return;

    if( val[p]==element )
    {
      lista[element%MOD]=next[lista[element%MOD]];
      return;
    }

    while( next[p]!=0 && val[next[p]]!=element )
      p=next[p];

    if( next[p]!=0 )
      next[p]=next[next[p]];
  }

  inline int find( int element )
  {
    int p=lista[element%MOD];

    while( p!=0 && val[p]!=element )
      p=next[p];

    return p;
  }

} h;

int main()
{
  freopen( "hashuri.in", "r", stdin );
  freopen( "hashuri.out", "w", stdout );

  int n, op, k;

  scanf( "%d", &n );

  for( int i=1;i<=n;i++ )
  {
    scanf( "%d%d", &op, &k );

    if( op==1 )
      h.insert(k);
    else
      if( op==2 )
        h.erase(k);
      else
        printf( "%d\n", (h.find(k)!=0) );
  }

  return 0;
}