Cod sursa(job #2588831)

Utilizator rares9991Matisan Rares-Stefan rares9991 Data 25 martie 2020 15:26:00
Problema Hashuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <fstream>

using namespace std;

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

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

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

bool apartine(int x)
{
    int c=x%M;
    for(int p=lst[c]; p!=0; p=urm[p])
    {
        if(val[p]==c)
        {
            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 ct, t;

int main()
{
    in>>t;
    for(int i=1; i<=t; i++)
    {
        int x;
        in>>ct>>x;
        if(ct==1)
            adauga(x);
        else if(ct==2)
            sterge(x);
        else if(ct==3)
        {
            int ok = apartine(x);
            if(ok == 1)
            {
                out << "1\n";
            }
            else
                out << "0\n";
        }
    }
    return 0;
}