Cod sursa(job #1994878)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 26 iunie 2017 14:27:11
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.57 kb
#include <fstream>
#include <vector>
using namespace std;

#define MAX 1000001

class Set {
public:
	Set() : m{ MAX }, values(MAX, -1), next(MAX, -1)
	{
		/*for (int i = 0; i < m; ++i)
		{
			values[i] = -1;
			next[i] = -1;
		}*/
	}
	int d(int i)
	{
		return i % m;
	}
	void add_value(int value)
	{
		if (find_value(value))
			return;
		int poz = d(value);
		if (values[poz] == -1)
		{
			values[poz] = value;
			return;
		}
		do {
			poz = next[poz];
		} while (next[poz] != -1);
		next[poz] = first;
		values[first] = value;
		int aux = first;
		while (values[first] != -1)
			++first;
		next[aux] = first;
	}
	void delete_value(int value)
	{
		int poz = d(value);
		if (next[poz] = -1)
		{
			values[poz] = -1;
			if (first > poz)
				first = poz;
			return;
		}
		do {
			values[poz] = values[next[poz]];
			values[next[poz]] = -1;
		} while (next[poz] != -1);
	}
	bool find_value(int value)
	{
		int poz = d(value);
		do {
			if (values[poz] == value)
				return true;
			poz = next[poz];
		} while (poz != -1);
		return false;
	}
private:
	int m, first = 0;
	vector<int> values, next;
};

int main()
{
	int op, type, x;
	Set s;

	ifstream is("hashuri.in");
	ofstream os("hashuri.out"); 
	//ifstream is("heapuri.in");
	//ofstream os("heapuri.out");

	is >> op;
	while (op--)
	{
		is >> type >> x;
		switch (type)
		{
		case 1:
			s.add_value(x);
			break;
		case 2:
			s.delete_value(x);
			break;
		default:
			os << s.find_value(x) << "\n";
			break;
		}
	}

	is.close();
	os.close();
	return 0;
}