Cod sursa(job #2594613)

Utilizator Ioan_AnghelIoan Anghel Ioan_Anghel Data 6 aprilie 2020 13:57:59
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream in("hashuri.in");
ofstream out("hashuri.out");

const int N = 1000005, M = 666019;
int val[N], lst[N], urm[N];
int nr;

bool exista(int x)
{
    int c = x % M;
    for(int p = lst[c]; p != 0; p = urm[p]){
        if(val[p] == x){
            return true;
        }
    }
    return false;
}

void adauga(int x)
{
    int c = x % M;
    if(exista(x)){
        return;
    }
    val[++nr] = x;
    urm[nr] = lst[c];
    lst[c] = nr;
}

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

int main()
{
    int n;
    in >> n;
    while(n--){
        int code, x;
        in >> code >> x;
        switch(code)
        {
        case 1:
            adauga(x);
            break;
        case 2:
            sterge(x);
            break;
        case 3:
            if(exista(x)){
                out << 1 << "\n";
            }
            else{
                out << 0 << "\n";
            }
            break;
        }
    }

    return 0;
}