Cod sursa(job #1279014)

Utilizator radudorosRadu Doros radudoros Data 29 noiembrie 2014 17:37:55
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.7 kb
#include <fstream>
#include <chrono>
#define BAZA 10000000
using namespace std;


ifstream fin("hashuri.in");
ofstream fout("hashuri.out");


struct lant
{
	int inf;
	lant *urm = NULL;
	lant(int x){ lant::inf = x; }
};

struct duo
{
	lant* start=NULL;
	lant*crt = NULL;
};


duo t[BAZA];

lant* sterge(lant* p)
{
	if (p->urm != NULL)
	{
		lant* paux = p->urm;
		delete p;
		return paux;
	}
	else
	{
		delete p;
	}
	return NULL;
	
}

int main()
{
	int q; int x; int n;
	fin >> n;
	for (int i = 1; i <= n; i++)
	{
		fin >> q >> x;
		int aux = x%BAZA;
		if (q == 1)
		{
			if (t[aux].start == NULL)
			{
				t[aux].start = new lant(x);
				t[aux].crt = t[aux].start;
			}
			else
			{
				t[aux].crt = t[aux].start;
				while (t[aux].crt->urm != NULL && t[aux].crt->inf != x)
					t[aux].crt = t[aux].crt->urm;
				if (t[aux].crt->inf != x)
				{
					t[aux].crt->urm = new lant(x);
					t[aux].crt = t[aux].crt->urm;
				}
			}
		}
		if (q == 2)
		{
			if (t[aux].start != NULL)
			{
				t[aux].crt = t[aux].start;
				while (t[aux].crt->urm != NULL &&t[aux].crt->inf != x)
					t[aux].crt = t[aux].crt->urm;
				if (t[aux].crt->inf == x)
				{
					if (t[aux].crt == t[aux].start)
					{
						t[aux].start = sterge(t[aux].crt);
						t[aux].crt = t[aux].start;
					}
					else
					{
						t[aux].crt = sterge(t[aux].crt);
					}
				}
			}
			
		}
		if (q == 3)
		{
			if (t[aux].start == NULL)
				fout << '0' << '\n';
			else
			{
				t[aux].crt = t[aux].start;
				while (t[aux].crt->urm != NULL &&t[aux].crt->inf != x)
					t[aux].crt = t[aux].crt->urm;
				if (t[aux].crt->inf == x)
					fout << '1' << '\n';
			}

		}

	}

	
}