Pagini recente » Cod sursa (job #1142137) | Istoria paginii runda/ojiprep | Cod sursa (job #2302885) | Cod sursa (job #1294461) | Cod sursa (job #906981)
Cod sursa(job #906981)
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<time.h>
#include<fstream>
#include<ctype.h>
using namespace std;
class CuckooHash
{
private:
int hash1,hash2,M1,M2,size;
vector<int> Hash1,Hash2;
int hashFunction1(int x);
int hashFunction2(int x);
void remakeFunctions(void);
int reinsert(int x);
void rehash(int x);
public:
CuckooHash(void);
CuckooHash(int M1,int M2);
int insert(int x);
int remove(int x);
int find(int x);
};
CuckooHash::CuckooHash(void)
{
srand(time(0));
this->M1 = 1000014 + rand()%200111;
this->M2 = 1000014 + rand()%200111;
this->M1 += 1-(this->M1 & 1);
this->M2 += 1-(this->M2 & 1);
Hash1.resize(M1);
Hash2.resize(M2);
remakeFunctions();
}
CuckooHash::CuckooHash(int M1,int M2)
{
this->M1 = M1;
this->M2 = M2;
Hash1.resize(M1);
Hash2.resize(M2);
remakeFunctions();
}
int CuckooHash::hashFunction1(int x)
{
return (1LL*hash1*x+hash2)%M1;
}
int CuckooHash::hashFunction2(int x)
{
return (1LL*hash2*x+hash1)%M2;
}
void CuckooHash::remakeFunctions(void)
{
hash1 = (M1>>1) + rand()%(M1>>1);
hash2 = (M2>>1) + rand()%(M2>>1);
}
inline void CuckooHash::rehash(int x)
{
CuckooHash Hash3;
Hash3.insert(x);
for(int i=0;i<M1;i++)
if(Hash1[i])
Hash3.insert(Hash1[i]);
for(int i=0;i<M2;i++)
if(Hash2[i])
Hash3.insert(Hash2[i]);
}
inline int CuckooHash::reinsert(int x)
{
int hash_funct2 = hashFunction2(x);
if(size == 0)
{
rehash(x);
return 0;
}
if(Hash2[hash_funct2])
{
int y = Hash2[hash_funct2];
Hash2[hash_funct2] = x;
-- size;
return reinsert(y);
}
else
{
Hash2[hash_funct2] = x;
return 1;
}
}
inline int CuckooHash::insert(int x)
{
int hash_funct1 = hashFunction1(x);
if(find(x))
{
return 1;
}
if(Hash1[hash_funct1])
{
int y = Hash1[hash_funct1];
Hash1[hash_funct1] = x;
size = 7;
return reinsert(y);
}
else
{
Hash1[hash_funct1] = x;
return 1;
}
}
inline int CuckooHash::remove(int x)
{
int hash_funct1 = hashFunction1(x);
int hash_funct2 = hashFunction2(x);
if(Hash1[hash_funct1] == x)
{
Hash1[hash_funct1] = 0;
return 1;
}
else if(Hash2[hash_funct2] == x)
{
Hash2[hash_funct2] = 0;
return 1;
}
return -1;
}
inline int CuckooHash::find(int x)
{
int hash_funct1 = hashFunction1(x);
int hash_funct2 = hashFunction2(x);
if(Hash1[hash_funct1] == x)
{
return 1;
}
else if(Hash2[hash_funct2] == x)
{
return 1;
}
return 0;
}
int SolV[1000002];
char S[10000002];
inline int getInt(int *i)
{
int rez = 0;
for(;!isdigit(S[*i]);++(*i));
for(;isdigit(S[*i]);rez = rez*10+(S[*i])-'0',++(*i));
return rez;
}
int Rezolvare(void)
{
int N,op,a,i,k = 0;
SolV[0] = 0;
CuckooHash Hash;
N = getInt(&k);
for(int i=1;i<=N;i++)
{
op = getInt(&k);
a = getInt(&k);
switch(op)
{
case 1 : {
if(!Hash.insert(a))
return 0; }
break;
case 2 : Hash.remove(a);
break;
case 3 : SolV[++SolV[0]] = Hash.find(a);
}
}
return 1;
}
int main()
{
ifstream f("hashuri.in");
ofstream g("hashuri.out");
f.getline(S,sizeof(S),EOF);
for(;!Rezolvare(););
for(int i=1;i<=SolV[0];i++)
g << SolV[i] << "\n";
f.close();
g.close();
}