Cod sursa(job #1264419)

Utilizator dnprxDan Pracsiu dnprx Data 15 noiembrie 2014 19:53:14
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include<fstream>
#define modulo 100003

using namespace std;

struct Nod
{
	int info;
	Nod *leg;
};

Nod *L[modulo];

inline Nod* Cauta(int x)
{
	int k;
	Nod *p, *q;
	k = x % modulo;
	for (p = L[k]; p != NULL && p->info != x; p = p->leg)
		q = p;
	if (p == NULL) return NULL;
	return q;
}

inline void Sterge(Nod *q)
{
	Nod *p;
	p = q->leg;
	q->leg = p->leg;
	delete p;
}

inline void Insereaza(int x)
{
	int k;
	k = x % modulo;
	Nod *p;
	p = new Nod;
	p->info = x;
	p->leg = L[k];
	L[k]->leg = p;
	L[k] = p;
}

void Rezolvare()
{
	int n, x, op, i;
	Nod *r;
	
	ifstream fin("hashuri.in");
	ofstream fout("hashuri.out");
	fin >> n;
	for (i = 1; i <= n; i++)
	{
		fin >> op >> x;
		if (op == 1)
		{
			r = Cauta(x);
			if (r == NULL)
				Insereaza(x);
		}
		else if (op == 2)
		{
			r = Cauta(x);
			if (r != NULL)
				Sterge(r);
		}
		else if (op == 3)
		{
			r = Cauta(x);
			if (r == NULL)
				fout << "0\n";
			else fout << "1\n";
		}
	}
	fin.close();
	fout.close();
}

int main()
{
	Rezolvare();
	return 0;
}