Cod sursa(job #2291228)

Utilizator LucianTLucian Trepteanu LucianT Data 27 noiembrie 2018 19:36:34
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#include <vector>
using namespace std;

const int MOD=666013;

int q;
vector<int> hashTable[MOD];

int findHash(int x){
    int where=x%MOD;
    for(auto it:hashTable[where])
        if(it==x)
            return 1;
    return 0;
}

void insertHash(int x){
    if(findHash(x))
        return;

    int where=x%MOD;
    hashTable[where].push_back(x);
}

void removeHash(int x){
    if(!findHash(x))
        return;

    int where=x%MOD;
    vector<int>::iterator pos=hashTable[where].begin();
    while(pos!=hashTable[where].end() && *pos!=x)
        pos++;

    hashTable[where].erase(pos);
}

int main(){
    ifstream cin("hashuri.in");
    ofstream cout("hashuri.out");

    cin>>q;
    while(q--){
        int op,x;
        cin>>op>>x;

        if(op==1)
            insertHash(x);
        else if(op==2)
            removeHash(x);
        else
            cout<<findHash(x)<<'\n';
    }

    return 0;
}