Cod sursa(job #2588829)

Utilizator maramihaliMara Mihali maramihali Data 25 martie 2020 15:23:29
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>

using namespace std;

const int N = 1000001;
const int M = 666019;

int val[N], urm[N], lst[M], nr, op, a, n;

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

bool apartine(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(apartine(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()
{
    in >> n;
    for(int i = 1; i <= n; i++)
    {
        in >> op >> a;
        if(op == 1)
        {
            adauga(a);
        }
        if(op == 2)
        {
            sterge(a);
        }
        if(op == 3)
        {
            int ok = apartine(a);
            if(ok == 1)
            {
                out << "1\n";
            }
            else
                out << "0\n";
        }
    }
    return 0;
}