Cod sursa(job #1706171)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 21 mai 2016 19:34:45
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.15 kb
#include <bits/stdc++.h>
#include <tr1/unordered_set>

const int DIM = 1 << 20;
const int MOD = (1 << 16) - 1;
using namespace std;

int n, q, x, k, p; char buffer[DIM];
vector <int> my_hash[MOD + 1];

class input_reader {
    private:
        FILE *input_file;
        static const int SIZE = 1 << 12;
        char buffer[SIZE]; int cursor;

        inline void advance() {
            if( ++cursor == SIZE ) {
                cursor = 0;
                fread( buffer, SIZE, 1, input_file );
            }

            return;
        }

        inline char current() {
            return buffer[cursor];
        }
    public:
        input_reader( const char *file_name, const char *file_type ) {
            input_file = fopen( file_name, file_type ); cursor = 0;
            fread( buffer, SIZE, 1, input_file );
        }

        input_reader &operator >>( int &value ) {
            value = 0;

            while( current() < '0' || current() > '9' )
                advance();

            while( current() >= '0' && current() <= '9' ) {
                value = value * 10 + ( current() - '0' );
                advance();
            }

            return *this;
        }
} input_file( "hashuri.in", "r" );
FILE *output_file = fopen( "hashuri.out", "w" );

int main( void ) {

    input_file >> n;
    for( int i = 1; i <= n; i ++ ) {
        input_file >> q >> x; p = x & MOD;

        switch(q) {
            case 1: {
                if( find( my_hash[p].begin(), my_hash[p].end(), x ) == my_hash[p].end() )
                    my_hash[p].push_back(x);
                break;
            }
            case 2: {
                if( find( my_hash[p].begin(), my_hash[p].end(), x ) != my_hash[p].end() )
                    my_hash[p].erase( find( my_hash[p].begin(), my_hash[p].end(), x ) );
                break;
            }
            case 3: {
                buffer[k++] = '0' + ( find( my_hash[p].begin(), my_hash[p].end(), x ) != my_hash[p].end() );
                buffer[k++] = '\n';
                break;
            }
        }
    }

    fwrite( buffer, 1, DIM, output_file );

    return 0;
}