Cod sursa(job #2627277)

Utilizator lucamLuca Mazilescu lucam Data 10 iunie 2020 12:19:35
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <cstdio>

const int N = 1000001, MOD = 666019;
int val[N], lst[N], nxt[MOD], current;

bool exists(int x) {
    int c = x % MOD;
    for (int p = lst[c]; p != 0; p = nxt[p]) {
        if (val[p] == x)
            return true;
    }
    return false;
}

void insert(int x) {
    if (exists(x))
        return;
    int c = x % MOD;
    val[++current] = x;
    nxt[current] = lst[c];
    lst[c] = current;
}

void erase(int x) {
    int c = x % MOD, p = lst[c];
    while (p != 0 && val[p] != x)
        p = nxt[p];
    if (!p)
        return;
    val[p] = val[lst[c]];
    lst[c] = nxt[lst[c]];
}

int main() {
    FILE *inf = freopen("hashuri.in", "r", stdin);
    FILE *outf = freopen("hashuri.out", "w", stdout);

    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
        int op, x;
        scanf("%d%d", &op, &x);
        if (op == 1)
            insert(x);
        else if (op == 2)
            erase(x);
        else
            puts(exists(x) ? "1" : "0");
    }

    fclose(inf);
    fclose(outf);
    return 0;
}