Pagini recente » Cod sursa (job #422914) | Cod sursa (job #716963) | Cod sursa (job #118250) | Cod sursa (job #648001) | Cod sursa (job #1070569)
#include <iostream>
#include <fstream>
using namespace std;
const unsigned FNV_offset_basis = 2166136261;
const unsigned FNV_prime = 16777619;
const unsigned NR = 14;
const unsigned MASK = ( 1 << NR );
const int STERS = -1;
unsigned MurmurHash2( unsigned x )
{
const unsigned m = 0x5bd1e995;
unsigned h = 0;
h ^= x;
h *= m;
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return ( h & ( MASK - 1 ) );
}
unsigned FNV( unsigned x )
{
unsigned hsh = FNV_offset_basis;
hsh = hsh ^ x;
hsh = hsh * FNV_prime;
return ( hsh & ( MASK - 1 ) );
}
unsigned HT1[MASK][60];
unsigned HT2[MASK][60];
bool find( int value )
{
int key1 = FNV( value );
int key2 = MurmurHash2( value );
if ( HT1[key1][0] > HT2[key2][0] )
{
for ( int i = 1; i <= HT2[key2][0]; ++i )
{
if ( HT2[key2][i] == value )
return 1;
}
}
else
{
for ( int i = 1; i <= HT1[key1][0]; ++i )
{
if ( HT1[key1][i] == value )
return 1;
}
}
return 0;
}
void insert( int value )
{
if ( find( value ) == 0 )
{
int key1 = FNV( value );
int key2 = MurmurHash2( value );
HT1[key1][ ++HT1[key1][0] ] = value;
HT2[key2][ ++HT2[key2][0] ] = value;
}
}
void erase( int value )
{
int key1 = FNV( value );
int key2 = MurmurHash2( value );
for ( int i = 1; i <= HT1[key1][0]; ++i )
{
if ( HT1[key1][i] == value )
{
for ( int j = i + 1; j <= HT1[key1][0]; ++j )
{
HT1[key1][j - 1] = HT1[key1][j];
}
HT1[key1][0]--;
break;
}
}
for ( int i = 1; i <= HT2[key2][0]; ++i )
{
if ( HT2[key2][i] == value )
{
for ( int j = i + 1; j <= HT2[key2][0]; ++j )
{
HT2[key2][j - 1] = HT2[key2][j];
}
HT2[key2][0]--;
break;
}
}
}
int N;
unsigned key, type;
const int DIM_BUFF = ( 1 << 20 );
char buffer[DIM_BUFF];
int position = DIM_BUFF;
char GetChar()
{
if ( position == DIM_BUFF )
{
fread( buffer, 1, DIM_BUFF, stdin );
position = 0;
}
return buffer[ position++ ];
}
unsigned rd()
{
unsigned nr = 0;
char c;
do
{
c = GetChar();
} while ( !isdigit( c ) );
do
{
nr = nr * 10 + ( c - '0' );
c = GetChar();
} while ( isdigit( c ) );
return nr;
}
int main()
{
freopen("hashuri.in", "r", stdin);
freopen("hashuri.out", "w", stdout);
N = rd();
while ( N-- )
{
type = rd(); key = rd();
if ( type == 1 ) insert( key );
if ( type == 2 ) erase( key );
if ( type == 3 ) printf("%d\n", find( key ) );
}
return 0;
}