Cod sursa(job #2698477)

Utilizator iancupoppPopp Iancu Alexandru iancupopp Data 22 ianuarie 2021 11:35:16
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>

using namespace std;

const int N = 1e6 + 1;
const int MOD = 666019;

int val[N], urm[N], lst[MOD], nr = 0;

void adauga(int x) {
    int c = x % MOD;
    val[++nr] = x;
    urm[nr] = lst[c];
    lst[c] = nr;
}

bool cauta(int x) {
    int c = x % MOD, p = lst[c];
    while (p != 0) {
        if (val[p] == x)
            return true;
        p = urm[p];
    }
    return false;
}

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

int main() {
    ifstream in("hashuri.in");
    ofstream out("hashuri.out");
    
    int n, op, x;
    in >> n;
    while (n--) {
        in >> op >> x;
        if (op == 1)
            adauga(x);
        else if (op == 2)
            sterge(x);
        else
            out << cauta(x) << '\n';
    }
    
    in.close();
    out.close();
}