Cod sursa(job #2160244)

Utilizator jeanFMI - Petcu Ion Cristian jean Data 11 martie 2018 12:13:53
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include<iostream>
#include<algorithm>
#include<vector>
#define MOD 666013
using namespace std;

vector<int> H[MOD];

vector<int>::iterator search(int x) {
    int hash = x % MOD;
    for (vector<int>::iterator it = H[hash].begin(); it != H[hash].end(); it++) {
        if (*it == x) return it;
    }
    return H[hash].end();
}

void insert(int x) {
    int hash = x % MOD;
    if (search(x) == H[hash].end()) {
        H[hash].push_back(x);
    }
}

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

bool exists(int x) {
    int hash = x % MOD;
    return search(x) != H[hash].end();
}

int main() {
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);

    int N, op, x;
    for (cin >> N; N; N--) {
        cin >> op >> x;
        if (op == 1) insert(x);
        else if (op == 2) erase(x);
        else cout << exists(x) << endl;
    } 

    return 0;
}