Cod sursa(job #2154971)

Utilizator amaliarebAmalia Rebegea amaliareb Data 7 martie 2018 14:25:17
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 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;

void Add(int x) {
    val[++ind] = x;
    int poz = val[ind] % mod;
    if (!start[poz]) {
        start[poz] = ind;
        return;
    }
    int y = start[poz];
    while (y && val[y] != x) y = urm[y];
    if (!y) {
        urm[ind] = start[poz];
        start[poz] = ind;
    }
}

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]];
}

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

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 << Check(x) << '\n';
    }
    return 0;
}