Cod sursa(job #2646308)

Utilizator ArthurelVilceanu Razvan-Arthur Arthurel Data 31 august 2020 18:25:13
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 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 part(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 add(int x)
{
    int c = x % M;
    if (part(x))
    {
        return;
    }
    val[++nr] = x;
    urm[nr] = lst[c];
    lst[c] = nr;
}

void del(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, tip, val;
    in >> n;
    for (int i = 0; i < n; i++)
    {
        in >> tip >> val;

        if (tip == 1)
        {
            add(val);
        }

        else if (tip == 2)
        {
            del(val);
        }

        else
        {
            out << part(val) << '\n';
        }

    }

    in.close();
    out.close();
    return 0;
}