Cod sursa(job #2580887)

Utilizator borscalinCalin-Stefan Georgescu borscalin Data 14 martie 2020 12:22:38
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 kb
#include <iostream>
#include <fstream>

std::ifstream fin ( "hashuri.in" );
std::ofstream fout ( "hashuri.out" );

const int NMAX = 1000000;
const int MOD = 666019;

class Hash {
    private : 
        int N;
        int val[1 + NMAX], next[1 + NMAX], list[1 + NMAX];
    public :
        Hash();
        ~Hash(){};
        bool inHash ( int x );
        void addHash ( int x );
        void erase ( int x );
};

Hash::Hash() {
    N = 0;
    for ( int i = 1; i <= NMAX; ++i )
        val[i] = next[i] = list[i] = 0;
}

bool Hash::inHash ( int x ) {
    int group = x % MOD;
    for ( int p = list[group]; p != 0; p = next[p] )
        if ( val[p] == x )
            return true;
    return false;
}

void Hash::addHash ( int x ) {
    if ( inHash ( x ) )
        return ;
    int group = x % MOD;
    val[++N] =  x;
    next[N] = list[group];
    list[group] = N;
}

void Hash::erase ( int x ) {
    if ( !inHash ( x ) )
        return ;
    int last = 0;
    int group = x % MOD;
    int p;
    for ( p = list[group]; val[p] != x; p = next[p] )
        last = p;
    if ( last == 0 ) 
        list[group] = next[p];
    else
        next[last] = next[p];
}

int main() {

    int n;
    fin >> n;

    Hash* hash = new Hash();

    for ( int i = 1; i <= n; ++i ) {
        int command, x;
        fin >> command >> x;
        switch ( command ) {
            case 1 : hash -> addHash ( x ); break;
            case 2 : hash -> erase ( x ); break;
            case 3 : fout << hash -> inHash ( x ) << '\n'; break;
        }
    }

    fin.close();
    fout.close();

    return 0;
}