Cod sursa(job #2154957)

Utilizator amaliarebAmalia Rebegea amaliareb Data 7 martie 2018 14:12:13
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
const int mod = 666013, MaxN = 1000005;
int n, urm[MaxN], val[MaxN], start[mod], ind, fr[MaxN];

void Del(int x) {
    int poz = start[x % mod];
    if (val[poz] == x) {
        //--fr[poz];
        start[x % mod] = urm[poz];
        return;
    }
    while (urm[poz] && val[urm[poz]] != x) poz = urm[poz];
    //if (val[urm[poz]] == x) --fr[urm[poz]];
    urm[poz] = urm[urm[poz]];
}

int Check(int x) {
    int poz = start[x % mod];
    while (poz && val[poz] != x) poz = urm[poz];
    return poz;
}

void Add(int x) {
    if (Check(x)) {
        //++fr[Check(x)];
        return;
    }
    val[++ind] = x;
    urm[ind] = urm[start[x % mod]];
    start[x % mod] = ind;
}

int main()
{
    f >> n;
    for (int i = 1; i <= n; ++i) {
        int op, x;
        f >> op >> x;
        if (op == 1) {
            Add(x);
        }
        else if (op == 2) Del(x);
        else g << (bool)Check(x) << '\n';
    }
    return 0;
}