Cod sursa(job #2040242)

Utilizator robuvedVictor Robu robuved Data 15 octombrie 2017 15:37:33
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#include <vector>
using namespace std;

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

#define max 333331
vector<int> T[max];
int Hash_Function(int x)
{
	return x%max;
}
void Add(int x)
{
	int h = Hash_Function(x);
	for (auto it = T[h].begin(); it != T[h].end(); it++)
	{
		if (x == *it)
			return;
	}
	T[h].push_back(x);
}
void Remove(int x)
{
	int h = Hash_Function(x);
	for (int i = 0; i<T[h].size(); i++)
	{
		if (x == T[h][i])
		{
			T[h].erase(T[h].begin() + i);
			return;
		}
	}
}
bool Exists(int x)
{
	int h = Hash_Function(x);
	for (auto it = T[h].begin(); it != T[h].end(); it++)
	{
		if (x == *it)
			return true;
	}
	return false;
}
int main()
{
	int n;
	in >> n;
	while (n--)
	{
		int opt, x;
		in >> opt >> x;
		switch (opt)
		{
		case 1:
			Add(x);
			break;
		case 2:
			Remove(x);
			break;
		case 3:
			out << (Exists(x) == true ? 1 : 0) << '\n';
			break;
		}
	}
}