Cod sursa(job #625662)

Utilizator zeeboBuzatu Vlad zeebo Data 25 octombrie 2011 10:32:14
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>
#define M 666013
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
int q,x,n,i;
struct point {
	int inf;
	point *leg;
};
point *h[M];
void inserare (int x)
{
	point *p;
	int ind=x%M;
	p=new point;
	p->inf=x; p->leg=h[ind];
	h[ind]=p;
}
bool caut (int x)
{
   point *p;
   int ind=x%M;
   p=h[ind];
   while (p) 
   {
      if (p->inf==x) return true;
	  p=p->leg;
   }
   return false;
}
void sterge (int x)
{
	point *p,*u;
	int ind=x%M;
	p=h[ind];u=h[ind];
	while (p->inf!=x && p) {u=p; p=p->leg;}
	if (p)
		if (u==p)
		{
		  h[ind]=h[ind]->leg;
		  delete p;
		}
 	    else
		{
        u->leg = p->leg;
		delete p;
		}
}	
int main ()
{
	f>>n;
	for (i=1;i<=n;i++)
	{
		f>>q>>x;
		if (q==1) 
			if (!caut(x)) inserare(x);
		if (q==2) 
			if (caut(x)) sterge(x);
		if (q==3) 
			if (caut(x)) g<<1<<'\n'; 
			else g<<0<<'\n';
	}
	f.close();
	g.close();
return 0;
}