Pagini recente » Cod sursa (job #518521) | Cod sursa (job #3196246) | Cod sursa (job #3184219) | Cod sursa (job #2938521) | Cod sursa (job #907332)
Cod sursa(job #907332)
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <fstream>
#include <iostream>
using namespace std;
class smen {
int *h1;
int *h2;
int sz;
int a1, b1, a2, b2, prime, val;
int num_maxsteps ;
public:
smen(int n) {
prime = 2000000011;
h1 = new int[n];
h2 = new int[n];
a1 = rand() % n;
b1 = rand() % n;
a2 = rand() % n;
b2 = rand() % n;
num_maxsteps = (int)log2(n);
memset(h1, 0, n * sizeof(h1));
memset(h2, 0, n * sizeof(h2));
sz = n;
cerr << a1 << " " << b1 << " " << a2 << " " << b2 << " " << num_maxsteps << "\n";
}
int get1(int);
int get2(int);
bool InsertTable(int);
bool FindTable(int);
bool RehashTable();
void EraseTable(int);
~smen() {
delete[] h1;
delete[] h2;
}
smen CopyTable() {
return *this;
}
};
int smen :: get1(int x) {
return ((1LL * a1 * x + b1 ) % prime ) % sz;
}
int smen :: get2(int x) {
return ((1LL * a2 * x + b2) % prime) % sz;
}
bool smen :: InsertTable(int x) {
int num_s = 0 ;
if( FindTable(x) )
return true;
while(num_s < num_maxsteps) {
num_s += 2;
if( h1[get1(x)] == 0 ) {
h1[get1(x)] = x;
return true;
}
swap(h1[get1(x)], x) ;
if(h2[get2(x)] == 0) {
h2[get2(x)] = x;
return true;
}
swap(h2[get2(x)] , x);
}
return false;
}
bool smen :: FindTable(int x) {
return ( h1[ get1(x) ] == x || h2[get2(x)] == x) ;
}
void smen :: EraseTable(int x) {
if( h1[ get1(x) ] == x )
h1[get1(x)] = 0;
if(h2[ get2(x) ] == x)
h2[ get2(x) ] = 0;
}
bool smen :: RehashTable() {
smen next(1000000);
for(int i = 0; i < sz; ++i)
if(h1[i])
if( next.InsertTable(h1[i]) == false )
return false;
for(int i = 0; i < sz; ++i)
if(h2[i])
if(next.InsertTable(h2[i]) == false)
return false;
*this = next.CopyTable() ;
return true;
}
int main() {
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
srand(time(NULL));
int op, x, y;
smen num_Hash(1000000);
for( fin >> op ; op--; ) {
fin >> x >> y ;
// cerr << x << " " << y << "\n";
if(x == 1) {
num_Hash.InsertTable(y) ;
while( num_Hash.InsertTable(y) == false)
for( ;num_Hash.RehashTable() == false; ) ;
}
if(x == 2)
num_Hash.EraseTable(y);
if(x == 3)
fout << (int)num_Hash.FindTable(y) << "\n";
}
return 0;
}