Cod sursa(job #248846)

Utilizator alex3el_n2oAlex Vladescu alex3el_n2o Data 26 ianuarie 2009 22:02:10
Problema Hashuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.66 kb
#include <stdio.h>

struct nod{
	long a;
	nod *adr;
	} *prim[179540], *sf[179540];  //666030
long X=179533; //666013 ; 179533

void f1(long t)
	{
	int p=1;
	long rest=t%X;
	nod *aux;
	aux=new nod;
	aux->a=t;
	aux->adr=NULL;
	if (prim[rest]==NULL)
		{
		prim[rest]=aux;
		sf[rest]=prim[rest];
		}
	else
		{
		//verif - go
		aux=new nod;
		aux=prim[rest];
		while (aux!=NULL&&p)
			{
			if (aux->a==t) p=0;
			aux=aux->adr;
			}
		if (p) {
			aux=new nod;
			aux->a=t;
			aux->adr=NULL;
			sf[rest]->adr=aux;
			sf[rest]=aux;
			}
		}
	}

void f2(long t)
{
	nod *aux;
	int p=0;
	long rest=t%X;
	if (prim[rest]!=NULL)
	{
		if (prim[rest]->adr==NULL&&prim[rest]->a==t)
		{
			prim[rest]=NULL;
			sf[rest]=NULL;
		}
		else
		{
		if (prim[rest]->a==t&&prim[rest]->adr->adr==NULL)
		{
			prim[rest]=prim[rest]->adr;
		}
		else
		{
		aux=new nod;
		aux=prim[rest];
		while (aux->adr!=NULL&&!p)
		{
			if (aux->adr->a==t) 
			{
				if (aux->adr->adr==NULL)
				{
					aux->adr=NULL;
					sf[rest]=aux;
				}
				else 
				{
					aux->adr=aux->adr->adr;
				}
			}
			else aux=aux->adr;
		}
		}
		}
	}
}
	
int f3(long t)
	{
	long rest=t%X;
	nod *aux;
	int p=0;
	if (prim[rest]==NULL)
		return 0;
	else
		{
		aux=new nod;
		aux=prim[rest];
		while (aux!=NULL&&!p)
			{
			if (aux->a==t) p=1;
			aux=aux->adr;
			}
		return p;
		}
	}

int main()
{
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
long n,i,x;
int tip;
scanf("%ld",&n);
for (i=1;i<=n;i++)
	{
	scanf("%d %ld",&tip,&x);
	if (tip==1) f1(x);
	if (tip==2) f2(x);
	if (tip==3) printf("%d\n",f3(x));
	}
return 0;
}