Cod sursa(job #629358)

Utilizator sory1806Sandu Sorina-Gabriela sory1806 Data 3 noiembrie 2011 10:38:49
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <cstdio>
#include <list>

using namespace std;

#define MAX_P 666013

int n;


list <int> myhash[MAX_P];

int hash (int x) {
  return x % MAX_P;
}

int hash_exists (int x) {
  int poz = hash(x);

  for (list<int>::iterator it = myhash[poz].begin (); it != myhash[poz].end
      (); it ++  )
    if (*it == x) 
      return 1;
  
  return 0;
}

void hash_add (int x) {
  if (!hash_exists(x))
    myhash[hash(x)].push_back (x);
}

void hash_del (int x) {
  myhash[hash(x)].remove (x);
}

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

  int op, nr;
  
  cin >> n;

  for (int i = 0; i < n; i ++) {
    scanf ("%d %d", &op, &nr);
    switch (op) {
      case 1: {
	hash_add (nr);
	break;
      }
      case 2: {
	hash_del (nr);
	break;
      }
      case 3: {
	printf ("%d\n", hash_exists (nr));
	break;
      }
    }
  }
 return 0;
}