Pagini recente » Cod sursa (job #1903596) | Cod sursa (job #2589672) | Cod sursa (job #2835681) | Cod sursa (job #3042245) | Cod sursa (job #1568227)
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
struct HT {
struct E {
int Key;
int T;
E() : T(0) {}
};
std::vector<E> _table;
unsigned int _mask;
HT(unsigned int N) {
unsigned int n = N;
while (n & (n + 1)) {
n |= (n + 1);
}
// Make it bigger for sparser collisions
// minimum 65k elements will need to 255k buckets
// with 90% access of first time hash, 99% second next bucket...
n |= (n + 1);
_table.resize(size_t(n) + 1);
_mask = n;
}
inline static unsigned int hash32(unsigned int h) {
h ^= h >> 16;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
return h;
}
inline static unsigned int hash(unsigned long long h) {
h ^= h >> 33;
h *= 0xff51afd7ed558ccdLL;
h ^= h >> 33;
h *= 0xc4ceb9fe1a85ec53LL;
h ^= h >> 33;
return h;
}
bool find(int key) {
auto h = hash32(key);
while (true) {
h = h & _mask;
if (_table[h].T == 0) {
return false;
}
else if (_table[h].T == 2 && _table[h].Key == key) {
return true;
}
h++;
}
}
void insert(int key) {
auto h = hash32(key);
while (true) {
h = h & _mask;
if (_table[h].T == 0 || _table[h].T == 1) {
_table[h].T = 2;
_table[h].Key = key;
return;
}
// Double insert
else if (_table[h].Key == key) {
return;
}
h++;
}
}
void remove(int key) {
auto h = hash32(key);
while (true) {
h = h & _mask;
// We didn't find the element.
if (_table[h].T == 0) {
return;
}
// If the key matches we set the state to deleted.
else if (_table[h].Key == key) {
_table[h].T = 1;
return;
}
h++;
}
}
};
int main() {
FILE *out = fopen("hashuri.out", "w");
FILE *in = fopen("hashuri.in", "r");
int n;
fscanf(in, "%d", &n);
HT ht(n);
for (int i = 0; i < n; i++) {
int op, c;
fscanf(in, "%d%d", &op, &c);
if (op == 1) {
ht.insert(c);
}
else if (op == 2) {
ht.remove(c);
}
else if (op == 3) {
if (ht.find(c))
fprintf(out, "1\n");
else
fprintf(out, "0\n");
}
}
return 0;
}