Cod sursa(job #2586270)

Utilizator rapidu36Victor Manz rapidu36 Data 20 martie 2020 11:54:37
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>

using namespace std;

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

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

int val[N], urm[N], lst[M], nr;

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)
{
    if (apartine(x))
    {
        return;
    }
    int c = x % M;
    val[++nr] = x;
    urm[nr] = lst[c];
    lst[c] = nr;
}

void sterge(int x)
{
    int c = x % M;
    int 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, tip, x;
    in >> n;
    for (int i = 0; i < n; i++)
    {
        in >> tip >> x;
        if (tip == 1)
        {
            adauga(x);
        }
        if (tip == 2)
        {
            sterge(x);
        }
        if (tip == 3)
        {
            out << apartine(x) << "\n";
        }
    }
    in.close();
    out.close();
    return 0;
}