Pagini recente » Cod sursa (job #2494183) | Cod sursa (job #1964062) | Cod sursa (job #971467) | Cod sursa (job #2747097) | Cod sursa (job #708992)
Cod sursa(job #708992)
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <ctime>
using namespace std;
#define LL (long long)
class cockooHash {
int tableSize;
int maxSteps;
// h (x) = ((a * x + b) % prime) % tableSize)
int a1, b1;
int a2, b2;
int prime;
int *T1, *T2;
bool *set1, *set2;
void getHashFunction (int &a, int &b) {
a = rand () % tableSize;
b = rand () % tableSize;
}
int h1 (int key) {
return ((LL this->a1 * LL key + LL this->b1) % LL this->prime ) % LL tableSize;
}
int h2 (int key) {
return ((LL this->a2 * LL key + LL this->b2) % LL this->prime ) % LL tableSize;
}
void rehash () {
getHashFunction (this->a1, this->b1);
getHashFunction (this->a2, this->b2);
}
public:
cockooHash (int tableSize) {
this->tableSize = tableSize;
maxSteps = (int) log2 (tableSize);
this->prime = 1000000009;
this->T1 = new int [tableSize];
this->T2 = new int [tableSize];
this->set1 = new bool[tableSize];
this->set2 = new bool[tableSize];
memset (this->T1, 0, sizeof (this->T1));
memset (this->T2, 0, sizeof (this->T2));
memset (this->set1, 0, sizeof (this->set1));
memset (this->set2, 0, sizeof (this->set2));
getHashFunction (this->a1, this->b2);
getHashFunction (this->a2, this->b2);
}
bool find (int key) {
int loc1 = this->h1(key);
int loc2 = this->h2(key);
if ( this->T1[ loc1 ] == key && this->set1[ loc1 ] == true) return true;
if ( this->T2[ loc2 ] == key && this->set2[ loc2 ] == true) return true;
return false;
}
bool erase (int key) {
int loc1 = this->h1(key);
int loc2 = this->h2(key);
if (this->T1[ loc1 ] == key && this->set1 [ loc1 ]) {
this->set1[ loc1] = 0;
return true;
}
if (this->T2[ loc2 ] == key && this->set2[ loc2 ]) {
this->set2[ loc2 ] = 0;
return true;
}
return false;
}
bool insert (int key) {
int x, y;
int loc1, loc2;
int steps = 0;
x = key;
loc1 = this->h1 (x);
if ( this->set1[ loc1 ] == 0 ) {
this->T1[loc1] = x;
this->set1[loc1] = 1;
return true;
}
while (steps < maxSteps) {
steps+= 2;
loc2 = this->h2(x);
if ( this->set2[ loc2 ] == 0 ) {
this->set2[ loc2 ] = 1;
this->T2[ loc2 ] = x;
return true ;
}
y = this->T2[loc2];
this->T2[ loc2 ] = x;
x = y;
loc1 = this->h1(x);
if ( this->set1[ loc1 ] == 0 ) {
this->set1[ loc1 ] = 1;
this->T1[ loc1 ] = x;
return true ;
}
y = this->T1[ loc1 ];
this->T1[ loc1 ] = x;
x = y;
}
printf ("rehashhh!!");
return false;
}
};
int main () {
srand ( time (0) );
freopen ("hashuri.in", "r", stdin);
freopen ("hashuri.out", "w", stdout);
cockooHash myHash (1000000);
int T, tip, key;
scanf ("%d", &T);
for (; T; T--) {
scanf ("%d %d", &tip, &key);
if (tip == 1 && !myHash.find (key)) myHash.insert (key);
if (tip == 2 && myHash.find (key)) myHash.erase (key);
if (tip == 3) printf ("%d\n", (int) myHash.find (key));
}
return 0;
}