Cod sursa(job #1523374)

Utilizator theodor.moroianuTheodor Moroianu theodor.moroianu Data 12 noiembrie 2015 17:51:07
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
using namespace std;
const int nmax = 1000000;
const int p = 666023;
int nr[nmax];
int urm[nmax];
int lst[p];
int cont = 0, l;

bool cauta(int x);
void adauga(int x);
void sterge(int x);

int main(){

    ifstream in("hashuri.in");
    int n, q;
    ofstream out("hashuri.out");
    short c;
    in >> n;
    while (n--){
        in >> c >> q;
        if (c == 1)
            adauga(q);
        if (c == 2)
            sterge(q);
        if (c == 3 && cauta(q))
            out << "1\n";
        if (c == 3 && !cauta(q))
            out << "0\n";
    }
    in.close();
    out.close();
    return 0;
}
void sterge(int x){
    if (!cauta(x))
        return;
    l = lst[x % p];
    while (nr[l] != x)
        l = urm[l];
    nr[l] = -1;
}

void adauga(int x){
    if (cauta(x))
        return;
    cont++;
    nr[cont] = x;
    urm[cont] = lst[x % p];
    lst[x % p] = cont;
}

bool cauta(int x){
    l = lst[x % p];
    while (l != 0){
        if (nr[l] == x)
            return true;
        l = urm[l];
    }
    return false;
}