Cod sursa(job #1171229)

Utilizator astronoviceAlexandru Pana astronovice Data 15 aprilie 2014 13:45:30
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <vector>
#include <fstream>

static const int BUCKET_COUNT = 666013;

typedef std::vector<std::vector<int>> hash_t;

int hash_f(int n) {
	return n % BUCKET_COUNT;
}

void add(hash_t& hash, int n) {
	hash[hash_f(n)].push_back(n);
}

void remove(hash_t& hash, int n) {
	int bucket = hash_f(n);
	for (auto i : hash) { 
		if (i == n) {
			i = -1;
		}
	}
}

bool search(hash_t& hash, int n) {
	int bucket = hash_f(n);
	for (auto const& i : hash[bucket]) { 
		if (i == n) {
			return true;
		}
	}
	return false;
}

int main() {
	hash_t h(BUCKET_COUNT);

	std::ifstream fin("hashuri.in");
	std::ofstream fout("hashuri.out");

	int n;
	fin >> n;

	while (n) {
		int op, arg;
		fin >> op >> arg;
		switch(op) {
		case 1:
			add(h, arg);
			break;
		case 2:
			remove(h, arg);
			break;
		case 3:
			fout << search(h, arg) << std::endl;
			break;
		}
		n--;
	}

	fin.close();
	fout.close();

	return 0;
}