Cod sursa(job #1703342)

Utilizator TincaMateiTinca Matei TincaMatei Data 16 mai 2016 20:24:34
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <cstdio>

const int MAX_HASH = 2000000;
const int MAX_N = 1000000;

int start[MAX_HASH];
int last[MAX_HASH];
int next[1+MAX_N];
int val[1+MAX_N];

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

void add(int x, int poz) {
  int p, i;
  p = hash(x);
  i = start[p];
  while(i != 0 && val[i] != x)
    i = next[i];
  if(i == 0 && start[p] == 0)
    start[p] = last[p] = poz;
  else if(i == 0)
    last[p] = next[last[p]] = poz;
}

void del(int x) {
  int p, i;
  p = hash(x);
  if(val[start[p]] == x)
    start[p] = next[start[p]];
  else {
    i = start[p];
    while(next[i] != 0 && val[next[i]] != x)
      i = next[i];
    if(next[i] != 0)
      next[i] = next[next[i]];
  }
}

int query(int x) {
  int i, p;
  p = hash(x);
  i = start[p];
  while(i != 0 && val[i] != x)
    i = next[i];
  return i != 0;
}

int main() {
  int n, op, i;
  //nu folosesc freopen des,dar cand il folosesc sunt pe telefon
  freopen("hashuri.in", "r", stdin);
  freopen("hashuri.out", "w", stdout);
  scanf("%d", &n);
  for(i = 1; i <= n; i++) {
    scanf("%d%d", &op, &val[i]);
    if(op == 1)
      add(val[i], i);
    else if(op == 2)
      del(val[i]);
    else
      printf("%d\n", query(val[i]));
  }
}