Cod sursa(job #1070544)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 1 ianuarie 2014 15:21:52
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.89 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

const unsigned FNV_offset_basis = 2166136261;
const unsigned FNV_prime = 16777619;

const unsigned NR = 18;
const unsigned MASK = ( 1 << NR );

vector <unsigned> HT[MASK];

unsigned fhash( unsigned x )
{
    unsigned hsh = FNV_offset_basis;
    hsh = hsh ^ x;
    hsh = hsh * FNV_prime;
    return ( hsh & ( MASK - 1 ) );
}

bool find( unsigned value )
{
    unsigned key = fhash( value );

    for ( auto x: HT[key] )
            if ( x == value )
                    return 1;

    return 0;
}

void insert( unsigned value )
{
    unsigned key = fhash( value );

    if ( !find( value ) )
            HT[key].push_back( value );
}

void erase( unsigned value )
{
    unsigned key = fhash( value );

    for ( vector<unsigned>::iterator x = HT[key].begin(); x != HT[key].end(); ++x )
            if ( *x == value )
            {
                HT[key].erase( x );
                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;
}