Pagini recente » Cod sursa (job #1005461) | Cod sursa (job #2712475) | Cod sursa (job #896754) | Cod sursa (job #78567) | Cod sursa (job #1477590)
#include <bits/stdc++.h>
using namespace std;
template <const size_t MAX_SIZE>
class LinearProbing
{
private:
static const int NIL = -1;
static const int DELETED = -2;
int table[MAX_SIZE];
inline bool check(const size_t pos, const int key) const
{
return table[pos] != NIL && table[pos] != key;
}
size_t h1(const int key) const
{
return (1U * key) * 2654435761;
}
size_t h2(const int key) const
{
unsigned a = key;
a = (a+0x7ed55d16) + (a<<12);
a = (a^0xc761c23c) ^ (a>>19);
a = (a+0x165667b1) + (a<<5);
a = (a+0xd3a2646c) ^ (a<<9);
a = (a+0xfd7046c5) + (a<<3);
a = (a^0xb55a4f09) ^ (a>>16);
return a;
}
int supposedPosition(const int key) const
{
size_t pos = 0;
while (check((h1(key) + pos * h2(key)) % MAX_SIZE, key) == true)
pos++;
return (h1(key) + pos * h2(key)) % MAX_SIZE;
}
public:
LinearProbing()
{
for (int i = 0; i < MAX_SIZE; ++i)
table[i] = NIL;
}
void insert(const int key)
{
table[supposedPosition(key)] = key;
}
bool find(const int key)
{
return table[supposedPosition(key)] == key;
}
void erase(const int key)
{
int pos = supposedPosition(key);
if (table[pos] == key)
table[pos] = DELETED;
}
};
const int M = 1 << 21;
LinearProbing<M> L;
int main()
{
ifstream in("hashuri.in");
ofstream out("hashuri.out");
int N;
in >> N;
while ( N-- )
{
int tip, x;
in >> tip >> x;
switch(tip)
{
case 1: L.insert(x); break;
case 2: L.erase(x); break;
case 3: out << L.find(x) << '\n'; break;
}
}
return 0;
}