Cod sursa(job #2531501)

Utilizator radustn92Radu Stancu radustn92 Data 26 ianuarie 2020 12:47:14
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

const int MOD = 1e6+3;
const int SENTINEL = -1;
vector<int> buckets[MOD];

int ops;

inline int hashFunction(int key) {
	return key % MOD;
}

vector<int>::iterator searchBucket(vector<int>& bucket, int value) {
	for (auto it = bucket.begin(); it != bucket.end(); it++) {
		if (*it == value) {
			return it;
		}
	}

	return bucket.end();
}

void insertHashTable(int bucket, int value) {
	auto it = searchBucket(buckets[bucket], value);
	if (it == buckets[bucket].end()) {
		buckets[bucket].push_back(value);
	}
}

void deleteHashTable(int bucket, int value) {
	auto it = searchBucket(buckets[bucket], value);
	if (it != buckets[bucket].end()) {
		*it = SENTINEL;
	}
}

bool searchHashTable(int bucket, int value) {
	return searchBucket(buckets[bucket], value) != buckets[bucket].end();
}

int main() {
	freopen("hashuri.in", "r", stdin);
	freopen("hashuri.out", "w", stdout);

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> ops;
	int opType, x;
	for (int op = 0; op < ops; op++) {
		cin >> opType >> x;
		switch (opType) {
			case 1: {
				insertHashTable(hashFunction(x), x);
				break;
			}
			case 2: {
				deleteHashTable(hashFunction(x), x);
				break;
			}
			case 3: {
				printf("%d\n", searchHashTable(hashFunction(x), x));
				break;
			}
		}
	}

	return 0;
}