Pagini recente » Cod sursa (job #968641) | Cod sursa (job #810360) | Cod sursa (job #1846094) | Cod sursa (job #1147274) | Cod sursa (job #826005)
Cod sursa(job #826005)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
const int kBucketCount = (1 << 21);
const int prime = 41;
int buckets[kBucketCount];
inline int next(const int &number) {
return ((5 * number) + 1) % kBucketCount;
}
inline int search(const int &number) {
int hash = ((number % kBucketCount) * prime) % kBucketCount;
int first = -1;
while (buckets[hash]) {
//if (buckets[hash] < 0 && first != -1) {
// first = hash;
//}
if (buckets[hash] == number) {
//if (first != -1) {
// buckets[first] = buckets[hash];
// buckets[hash] = -1;
// hash = first;
//}
return hash;
}
hash = next(hash);
}
return -1;
}
inline void insert(const int &number) {
int hash = ((number % kBucketCount) * prime) % kBucketCount;
while (buckets[hash] > 0) {
hash = next(hash);
}
buckets[hash] = number;
}
int main() {
ifstream cin("hashuri.in");
ofstream cout("hashuri.out");
int N; cin >> N;
for (int i = 0; i < N; ++i) {
int operation; cin >> operation;
int number; cin >> number;
switch (operation) {
case 1:
if (search(number) < 0);
insert(number);
break;
case 2:
int x;
if ((x = search(number)) >= 0);
buckets[x] = -1;
break;
case 3:
cout << int(search(number) >= 0) << "\n";
}
}
}