Cod sursa(job #1396955)

Utilizator BLz0rDospra Cristian BLz0r Data 23 martie 2015 10:17:03
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

#define Mod 666013

FILE *f = fopen ( "hashuri.in", "r" );
FILE *g = fopen ( "hashuri.out", "w" );

vector < int > Hash[Mod+1];
int poz;

inline int Key ( int x ){
    return x % Mod;
}

int Exist ( int x ){
    vector < int > :: iterator it;
    int aux = Key ( x );

    for ( it = Hash[aux].begin(); it < Hash[aux].end(); ++it )
        if ( *it  == x )
            return it - Hash[aux].begin();
    return -1;
}

void Insert ( int x ){
    Hash[Key(x)].push_back ( x );
}

void Delete ( int x ){
    int aux = Key ( x );

    Hash[aux].erase ( Hash[aux].begin() + poz );

}

int main(){
    int N, type, x, aux;

    fscanf ( f, "%d", &N );

    for ( int i = 1; i <= N; ++i ){
        fscanf ( f, "%d%d", &type, &x );

        aux = Key ( x );
        poz = Exist ( x );

        if ( type == 1 && poz == -1 ){
            Insert ( x );
            continue;
        }
        if ( type == 2 && poz != -1 ){
            Delete ( x );
            continue;
        }
       if ( type == 3 )
            fprintf ( g, "%d\n", poz == -1 ? 0 : 1 );
    }

    return 0;
}