Cod sursa(job #2294660)

Utilizator dragos99Homner Dragos dragos99 Data 2 decembrie 2018 17:39:07
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include<fstream>
#include<vector>
using namespace std;

    ifstream f("hashuri.in");
    ofstream g("hashuri.out");

const int PRIM = 191507;
vector<long> hash_table[1000001];

void adauga(long x){
    long modulo = x % PRIM;
    long ok = 0;
    long long poz_libera = -1;
    long long i;
    for(i = 0 ; i < hash_table[modulo].size() && ok == 0 ; i++){
        if(hash_table[modulo][i] == x)
            ok = 1;
        if(hash_table[modulo][i] == 0 && poz_libera == -1)
            poz_libera = i;
    }

    if(ok == 0 && poz_libera >= 0){
        ok = 1;
        hash_table[modulo][poz_libera] = x;
    }

    if(ok == 0){
        hash_table[modulo].push_back(x);
    }
}

void sterge(long x){
    long modulo = x % PRIM;
    long ok = 0;
    long long i;
    for(i = 0 ; i < hash_table[modulo].size() && ok == 0 ; i++)
        if(hash_table[modulo][i] == x){
            ok = 1;
            hash_table[modulo][i] = 0;
        }
}

long verifica_daca_este_in_multime(long x){
    long modulo = x % PRIM;
    long ok = 0;
    long long i;
    for(i = 0 ; i < hash_table[modulo].size() && ok == 0 ; i++)
        if(hash_table[modulo][i] == x){
            ok = 1;
        }
    return ok;
}

int main()
{
long n, cerinta, x;
f>>n;
for(long i = 0 ; i < n ; i++){
    f>>cerinta>>x;
    if(cerinta == 1){
        adauga(x);
    }
    if(cerinta == 2){
        sterge(x);
    }
    if(cerinta == 3){
        g<<verifica_daca_este_in_multime(x)<<'\n';
    }
}

return 0;
}