Cod sursa(job #1875039)

Utilizator tudormaximTudor Maxim tudormaxim Data 10 februarie 2017 18:00:25
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>
using namespace std;

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

const int mod = 666013;

class Hash {
private:
    vector <int> V[mod + 1];

public:
    inline int Exist(int val) {
        int key = val & mod;
        int i = 0;
        while (i < (int) V[key].size() && V[key][i] != val) {
            i++;
        }
        if (i < V[key].size()) {
            return i;
        }
        return -1;
    }

    inline void Insert(int val) {
        int key = val & mod;
        if (Exist(val) == -1) {
            V[key].push_back(val);
        }
    }

    inline void Erase(int val) {
        int key = val & mod;
        int pos = Exist(val);
        if (pos != -1) {
            V[key][pos] = V[key][V[key].size() - 1];
            V[key].pop_back();
        }
    }
} H;

int main() {
    ios_base :: sync_with_stdio (false);
    int n, op, val;
    fin >> n;
    while (n--) {
        fin >> op >> val;
        if (op == 1) {
            H.Insert(val);
        } else if (op == 2) {
            H.Erase(val);
        } else {
            if (H.Exist(val) == -1) {
                fout << "0\n";
            } else {
                fout << "1\n";
            }
        }
    }
    fin.close();
    fout.close();
    return 0;
}