Cod sursa(job #548750)

Utilizator blastoiseZ.Z.Daniel blastoise Data 7 martie 2011 19:15:00
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <stdio.h>
#include <string.h>

#define MOD 999983

struct hash
{
	int nod;
	hash *link;
}*H[MOD];

int x,y,i,N;

inline int find(int x,int y)
{
	hash *p;

	p=H[x];

	while(p)
	{
		if(p->nod==y) return 1;
		p=p->link;
	}

	return 0;
}

inline void add(int x,int y)
{
	hash *p;

	p=new hash;
	p->nod=y;
	p->link=H[x];
	H[x]=p;
}

inline void del(int x,int y)
{
	hash *p,*q;

	q=NULL;
	p=H[x];

	while(p)
	{
		if(p->nod==y)
		{
			if(q==NULL) H[x]=p->link;
			else
			q->link=p->link;

			delete p;

			return;
		}
		q=p;
		p=p->link;
	}
}

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

	scanf("%d",&N);

	memset(H,NULL,sizeof(H));

	for(i=0;i<N;i++)
	{
		scanf("%d%d",&x,&y);

		if(x==1)
			if(!find(y%MOD,y))
				add(y%MOD,y);

		if(x==2)
			if(find(y%MOD,y))
				del(y%MOD,y);

		if(x==3)
			if(find(y%MOD,y))
				printf("1\n");
			else
				printf("0\n");
	}

	return 0;
}