Cod sursa(job #803994)

Utilizator robert_dDragan Robert robert_d Data 28 octombrie 2012 17:57:19
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define MOD 100000
vector<int> ht[MOD];

inline vector<int>::iterator find_ht(int x) {
    int h = x % MOD;
    vector<int>::iterator iter;
    for (iter = ht[h].begin(); iter != ht[h].end(); ++iter)
        if (*iter == x) return iter;
    return ht[h].end();
}

inline void insert_ht(int x) {
    int h = x % MOD;
    if (find_ht(x) == ht[h].end())
        ht[h].push_back(x);
}

inline void delete_ht(int x) {
    int h = x % MOD;
    vector<int>::iterator iter = find_ht(x);
    if (iter != ht[h].end()) {
        *iter = ht[h].back();  // move the last element in the place of this one
        ht[h].pop_back();   // pop the last element
    }
}

int main() {
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    int n, op, val;
    scanf("%d", &n);
    for (int i=0; i<n; ++i) {
        scanf("%d %d", &op, &val);
        if (op == 1) {
            insert_ht(val);
        } else if (op == 2) {
            delete_ht(val);
        } else {
            printf("%d\n", find_ht(val) != ht[val%MOD].end());
        }
    }
    return 0;
}