Pagini recente » Cod sursa (job #2949135) | Cod sursa (job #755669) | Cod sursa (job #2918549) | Cod sursa (job #2880401) | Cod sursa (job #2824385)
#include <stdio.h>
#include <ctype.h>
class ReadOnSteroids {
protected:
static const int BUFSIZE = (128 * 1024);
FILE *fin;
char ibuf[BUFSIZE];
int ibp = BUFSIZE - 1;
bool close;
public:
ReadOnSteroids( char *fname ){
fin = fopen( fname, "r" );
close = true;
}
ReadOnSteroids( FILE *fp ){
fin = fp;
close = false;
}
~ReadOnSteroids(){
if( close )
fclose( fin );
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
inline char getch(){
if( (ibp = ((ibp + 1) & (BUFSIZE - 1))) == 0 )
fread( ibuf, sizeof(char), BUFSIZE, fin );
return ibuf[ibp];
}
#pragma GCC diagnostic pop
template<typename T> inline T getnum(){
T n = 0;
char ch;
while( !isdigit( ch = getch() ) );
do
n = n * 10 + ch - '0';
while( isdigit( ch = getch() ) );
return n;
}
};
class WriteOnSteroids {
protected:
static const int BUFSIZE = (128 * 1024);
FILE *fout;
char wbuf[BUFSIZE];
int wbp = 0;
bool close;
public:
WriteOnSteroids( char *fname ){
fout = fopen( fname, "w" );
close = true;
}
WriteOnSteroids( FILE *fp ){
fout = fp;
close = false;
}
~WriteOnSteroids(){
flush();
if( close )
fclose( fout );
}
inline void putch( char ch ){
wbuf[wbp] = ch;
if( (wbp = ((wbp + 1) & (BUFSIZE - 1))) == 0 )
fwrite( wbuf, sizeof(char), BUFSIZE, fout );
}
inline void flush(){
fwrite( wbuf, sizeof(char), wbp, fout );
wbp = 0;
}
template<typename T, const int maxdigit> inline void putnum( T n ){
char out[maxdigit + 2];
int i = maxdigit;
if( n < 0 ){
n = -n;
putch( '-' );
}
if( n == 0 )
out[--i] = '0';
while( n ){
out[--i] = '0' + (n % 10);
n /= 10;
}
while( i < maxdigit )
putch( out[i++] );
}
};
class HashTable {
protected:
static const int NIL = -1;
static const int MAXHASH = 65536;// 2^16
static const int MAXN = 1'000'000;
int list[MAXHASH];
int num[MAXN];
int next[MAXN];
int sp;
// mid-square-sum algorithm
inline int hash( int x ){
return ((((long long)x) * x) >> 24) & (MAXHASH - 1);
}
inline int find( int x ){
int i = list[hash( x )];
while( i != NIL && num[i] != x )
i = next[i];
if( i == NIL )
return NIL;
return i;
}
public:
HashTable(){
int i;
for( i = 0 ; i < MAXHASH ; i++ )
list[i] = NIL;
sp = 0;
}
void insert( int x ){
int h = hash( x );
if( exists( x ) )
return;
next[sp] = list[h];
num[sp] = x;
list[h] = sp++;
}
void del( int x ){
int h = hash( x ), *prev_ptr = list + h;
if( *prev_ptr == NIL )
return;
while( *prev_ptr != NIL ){
if( num[*prev_ptr] == x )
*prev_ptr = next[*prev_ptr];
prev_ptr = &next[*prev_ptr];// next + (*prev_ptr)
}
}
inline int exists( int x ){
return find( x ) != NIL;
}
};
// pun inafara stivei ca sa nu dau stackoverflow dar nu dau new ca sa nu incetineasca pointerii
ReadOnSteroids fin( (char *)"hashuri.in" );
WriteOnSteroids fout( (char *)"hashuri.out" );
HashTable map;
int main(){
int q;
for( q = fin.getnum<int>() ; q-- ; ){
switch( fin.getnum<int>() ){
case 1:
map.insert( fin.getnum<int>() );
break;
case 2:
map.del( fin.getnum<int>() );
break;
case 3:
fout.putnum<int, 1>( map.exists( fin.getnum<int>() ) );
fout.putch( '\n' );
break;
}
}
return 0;
}