Cod sursa(job #868268)

Utilizator Andrei1998Andrei Constantinescu Andrei1998 Data 30 ianuarie 2013 21:08:19
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>

using namespace std;

#define clasa 210000

struct nod
{
	int val;
	nod *ante;
};

nod *v[clasa+1];

bool exista(int x)
{
	nod *p;
	
	bool gasit=false;
	
	for(p=v[x%clasa];p!=NULL;p=p->ante)
		if(p->val==x)
		{
			gasit=true;
			break;
		}
		if(gasit)
			return 1;
		return 0;
}

void push(int x)
{
	if(exista(x))
		return;
	
	nod *p=new nod;
	p->val=x;
	p->ante=v[x%clasa];
	v[x%clasa]=p;
}

void pop(int x)
{
	nod *p=new nod;
	nod *t=new nod;
	
	if(v[x%clasa]!=NULL)
	if(v[x%clasa]->val==x)
	{
		v[x%clasa]=v[x%clasa]->ante;
		return;
	}
	
	for(p=v[x%clasa];p!=NULL;p=p->ante)
	{
		if(p->val==x)
		{
			if(t!=NULL) //luam 40 cu t==null, prostie
				t->ante=p->ante;
			break;
		}
		t=p;
	}
}

int main()
{
	ifstream fin("hashuri.in");
	ofstream fout("hashuri.out");
	int n,x,y,i;
	fin>>n;
	for(i=0;i<n;i++)
	{
		fin>>x;
		fin>>y;
		if(x==1)
			push(y);
		else if(x==2)
			pop(y);
		else
			fout<<exista(y)<<'\n';
	}
	fin.close();
	fout.close();
	return 0;
}