Cod sursa(job #1495369)

Utilizator gabi.cristacheGabi Cristache gabi.cristache Data 2 octombrie 2015 23:32:19
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <vector>

#define MOD 666013

using namespace std;

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

vector<int> myhash[MOD];

vector<int>::iterator myfind(int key) {
    int bucketIndex = key % MOD;
    vector<int> & bucket = myhash[bucketIndex];
    for (vector<int>::iterator it = bucket.begin();
         it != bucket.end(); ++it) {
        if (*it == key)
            return it;
    }
    return bucket.end();
}

void myinsert(int x) {
    if (myfind(x) == myhash[x % MOD].end()) {
        myhash[x % MOD].push_back(x);
    }
}

void myerase(int x) {
    vector<int>::iterator it = myfind(x);
    if (it != myhash[x % MOD].end()) {
        myhash[x % MOD].erase(it);
    }
}

int main()
{

    int N, op, x;
    fin >> N;
    for (int i = 1; i <= N; ++i) {
        fin >> op >> x;
        if (op == 1) {
            myinsert(x);
        } else if (op == 2) {
            myerase(x);
        } else {
            fout << (myfind(x) != myhash[x % MOD].end()) << '\n';
        }
    }

    return 0;
}