Cod sursa(job #625928)

Utilizator Lokycatalin petre Loky Data 25 octombrie 2011 21:16:24
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <fstream>
#define max 666013

using namespace std;

struct point {
	int inf;
	point *leg;
};

point *h[max];

bool cautare (int x)
{
       bool ok;
       int i;
       point *p;
       i=x%max;
       p=h[i];
       ok=false;
       while (p) {
       if (p->inf==x)  {ok=true;break;}
       p=p->leg;
       }
       return ok;
}

void inserare (int x)
{
       int i;
	point *p;
	i=x%max;
	p=new point;
	p->inf=x;
	p->leg=h[i];
	h[i]=p;
}

void stergere (int x)
{
	point *p,*u;
	int i;
	i=x%max;
	p=h[i];
	u=h[i];
	while (p->inf!=x &&p) {u=p;p=p->leg;}
	if (p) if (u==p)
		{
		  h[i]=h[i]->leg;
		  delete p;
		}
              else
		{
              u->leg=p->leg;
		delete p;
		}
}

int n,j,a,b;
bool ok1;

int main()
{
    ifstream f("hashuri.in");
    ofstream g("hashuri.out");
    f>>n;
	for (j=1;j<=n;j++) {
		f>>a>>b;
		if (a==1) {
              ok1=cautare(b);if (ok1==false) inserare(b); }
		if (a==2) {
              ok1=cautare(b);if (ok1) stergere(b);}
		if (a==3) {
              ok1=cautare(b); if (ok1) g<<1<<'\n';
              else g<<0<<'\n'; }
	}
    f.close();
    g.close();
    return 0;
}