Cod sursa(job #1799798)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 6 noiembrie 2016 20:06:33
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

class Hash {
 public:
	void insert(const int value) {
		const int key = getKey(value);
		if (find(value) == table[key].end()) {
			table[key].push_back(value);
		}
	}


	void erase(const int value) {
		const int key = getKey(value);
		table[key].erase(find(value));
	}


	bool contains(const int value) const {
		const int key = getKey(value);
		return find(value) != table[key].end();
	}


 private:
	static const int U = 666013;


	static int getKey(const int value) {
		return (value % U + U) % U;
	}


	vector<int>::const_iterator find(const int value) const {
		return find(table[key].begin(), table[key].end(), value);
	}


	vector<int> table[U];
};

int main() {
	ifstream in("hashuri.in");
	ofstream out"(hashuri.out");
	int q;
	in >> q;
	Hash hash;
	for (; q > 0; --q) {
		int op, x;
		in >> op >> x;
		if (op == 1) {
			hash.insert(x);
		} else if (op == 2) {
			hash.erase(x);
		} else if (op == 3) {
			out << (hash.contains(x) ? 1 : 0) << "\n";
		}
	}
	in.close();
	out.close();
	return 0;
}