Cod sursa(job #1799813)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 6 noiembrie 2016 20:28:44
Problema Hashuri Scor 100
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) {
	  if (contains(value)) {
      table[getKey(value)].erase(find(value));
	  }
	}

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

 private:
	static const int U = 666013;

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

	vector<int>::iterator find(const int value) {
    const int key = getKey(value);
		return std::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;
}