Cod sursa(job #1171204)

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

static const int BUCKET_COUNT = 44729;
static const int DOMAIN_SIZE = 2000000000;

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

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

void add(hash_t& hash, int n) {
	int bucket = hash_f(n);
	int free_slot = -1;
	int crt = 0;
	for (auto const& i : hash[bucket]) { 
		if (i == n) {
			return;
		}
		if (free_slot == -1 && i == -1) {
			free_slot = crt;
		}
		++crt;
	}
	if (free_slot != -1) {
		hash[bucket][free_slot] = n;
	} else {
		hash[bucket].push_back(n);
	}
}

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

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(DOMAIN_SIZE / BUCKET_COUNT + 1);

	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;
}