Cod sursa(job #273157)

Utilizator pandaemonAndrei Popescu pandaemon Data 8 martie 2009 11:32:22
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>

#define MOD 666013

int n,op,x;

struct map { int val; map *last; } *v[MOD];

int add_node(int x)
{
  int rest= x%MOD; map *p=NULL;

  for(p=v[rest]; p!=NULL && p->val!=x; p=p->last)

  if(p==NULL) return 1;

  p=new map;   p->val=x;

  p->last=v[rest];  v[rest]=p;

}

int delete_node(int x)
{
  int rest= x%MOD;  map *p=NULL,*before=NULL;

  for(p=v[rest]; p!=NULL && p->val!=x; p=p->last) before=p;

  if( p!=NULL )
  {
    if(before==NULL) v[rest]=p->last;

    else before->last=p->last;

    delete p;

  }

}


int search_node(int x)
{
 int rest= x%MOD; map *p=NULL;

 for(p=v[rest]; p!=NULL && p->val!=x; p=p->last) ;

 if( p!=NULL ) printf("1\n");
 else               printf("0\n");

}


int main()
{

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

   scanf("%d",&n);

   while(n--)
   {
      scanf("%d %d",&op,&x);

      if(op==1) add_node(x);

      if(op==2) delete_node(x);

      if(op==3) search_node(x);

   }

  return 0;
}