Cod sursa(job #2623261)

Utilizator ihorvaldsTudor Croitoru ihorvalds Data 2 iunie 2020 20:54:27
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <vector>
#include <fstream>

int p;

int h(int& val) {
	return val % p;
}

int exists(std::vector<unsigned int>* v, int& key) {
	std::vector<unsigned int>& a = v[h(key)];
	std::vector<unsigned int>::iterator found = std::find(a.begin(), a.end(), key);
	return (found != a.end()) ? 1 : 0;
}

int main()
{
	std::ifstream f("hashuri.in");
	std::ofstream g("hashuri.out");

	int n;
	f >> n;
	int command, x;
	p = n / 2 + rand() % (n / 2);

	std::vector<unsigned int>* a = new std::vector<unsigned int>[p];

	for (int i = 0; i < n; i++) {
		f >> command >> x;
		if (command == 1) {
			if (!exists(a, x)) {
				a[h(x)].push_back(x);
			}
		}

		if (command == 2) {
			std::vector<unsigned int>& v = a[h(x)];
			std::vector<unsigned int>::iterator found = std::find(v.begin(), v.end(), x);

			if (found != v.end()) {
				v.erase(found);
			}
		}

		if (command == 3) {
			g << exists(a, x) << "\n";
		}
	}

	delete[] a;
}