Cod sursa(job #774790)

Utilizator ionut_blesneagIonut Blesneag ionut_blesneag Data 6 august 2012 15:03:40
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include<stdio.h>
const int m=666013;
using namespace std;

int n,i,c;
long e;

struct hash
{long x;
 hash* next;};
hash* h[666013]; 

void add(long elem)
{hash* q = new hash;
 int modl;
 q->x=elem;
 modl=e%m;
 q->next=h[modl];
 h[modl]=q;
}

int search(long elem)
{int modl;
 modl=e%m;
 hash* q=h[modl];
 while(q)
  {if(q->x==elem)
      return 1;
   q=q->next;}
 return 0;  
}

void del(int elem)
{int modl;
 modl=e%m;
 hash* q=h[modl];
 if(q)
 {if(q->x==elem)
   h[modl]=q->next;
 else    
 {hash* p;
  p=q;
  q=q->next;
  while(q)
  {if(q->x==elem)
    p->next=q->next;
  q=q->next;}
 }
} 
}

int main()
{freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
scanf("%d",&n);    
for(i=1; i<=n; i++)
  {scanf("%d %d",&c,&e);
   if(c==1)
     add(e);
   if(c==2)
     del(e);
   if(c==3)
     printf("%d\n",search(e));
  }   
     
return 0;
}