Cod sursa(job #313466)

Utilizator mlazariLazari Mihai mlazari Data 9 mai 2009 02:36:44
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include<stdio.h>
#define NMAX 1000000
#define min(a,b) ((a<b)?a:b)

struct lista {
  int x;
  lista *next;
};

lista* h[NMAX];
int n,op,x;

void init() {
  for(int i=0;i<min(NMAX,n);i++) h[i]=NULL;
}

int ex(int x) {
  int e=0;
  lista *q=h[x%NMAX];
  while(!e&&q) { if(x==q->x) e=1; q=q->next; }
  return e;
}

void add(int x) {
  int i=x%NMAX;
  if(!ex(x)) {
    lista *q=new lista;
    q->x=x;
    q->next=h[i];
    h[i]=q;
  }
}

void del(int x) {
  lista *q,*w;
  int i=x%NMAX;
  if(h[i]) {
    if(h[i]->x==x) { q=h[i]; h[i]=h[i]->next; delete q; }
    else {
      q=h[i];
      while(q->next) {
        if(x==q->next->x) {
          w=q->next;
          q->next=w->next;
          delete w;
          return;
        }
        q=q->next;
      }
    }
  }
}

int main() {
  init();
  freopen("hashuri.in","r",stdin);
  freopen("hashuri.out","w",stdout);
  scanf("%d",&n);
  init();
  for(int i=0;i<n;i++) {
    scanf("%d %d",&op,&x);
    switch(op) {
      case 1: add(x); break;
      case 2: del(x); break;
      case 3: printf("%d\n",ex(x)); break;
    }
  }
  fclose(stdin);
  fclose(stdout);
  return 0;
}