Cod sursa(job #2892142)

Utilizator Andrei-27Arhire Andrei Andrei-27 Data 20 aprilie 2022 22:42:29
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>

using namespace std;
const int MOD = 999983;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
int n, tip, x, stackSize;
vector<int> st, h;

struct hash2 {
    int v{}, n{};
};

vector<hash2> l;

inline bool find(const int &y) {
    int i = h[y - (y / MOD) * MOD];
    while (i != -1 && l[i].v != y) {
        i = l[i].n;
    }
    return i != -1;
}

inline void insert(const int &y) {
    /*
    if (find(y)) {
        return;
    }
    */
    int hash = y - (y / MOD) * MOD;
    int p;
    if (!st.empty()) {
        p = st.back();
        st.pop_back();
    } else {
        p = stackSize++;
    }
    if (p == l.size()) {
        hash2 temp;
        l.emplace_back(temp);
    }
    l[p].v = y;
    l[p].n = h[hash];
    h[hash] = p;
}

inline void erase(const int &y) {
    int hash = y - (y / MOD) * MOD;
    int i = h[hash];
    if (i == -1) {
        return;
    }
    if (l[i].v == y) {
        st.emplace_back(i);
        h[hash] = l[i].n;
    } else {
        while (l[i].n != -1 && l[l[i].n].v != y) {
            i = l[i].n;
        }
        if (l[i].n != -1) {
            st.emplace_back(l[i].n);
            l[i].n = l[l[i].n].n;
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    in.tie();
    out.tie();
    h.resize(MOD, -1);
    in >> n;
    for (; n; --n) {
        in >> tip >> x;
        if (tip == 1) {
            insert(x);
        }
        if (tip == 2) {
            erase(x);
        }
        if (tip == 3) {
            out << find(x) << '\n';
        }
    }
    return 0;
}