Cod sursa(job #3269883)

Utilizator SfichiAndreiSfichi Andrei SfichiAndrei Data 21 ianuarie 2025 13:25:37
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("hashuri.in");
ofstream fout ("hashuri.out");
const int MOD=666013,NMAX=1000006;
int start[NMAX],val[NMAX],urm[NMAX];
int nr;
bool exista(int x)
{
    int c=x%MOD,j;
    for(j=start[c];j!=0;j=urm[j])
    {
        if(val[j]==x)
        {
            return true;
        }
    }
    return false;
}
void adauga(int x)
{
    int c=x%MOD;
    if(exista(x))
    {
        return;
    }
    val[++nr]=x;
    urm[nr]=start[c];
    start[c]=nr;
}
void sterge(int x)
{
    int c=x%MOD,p=start[c];
    while(p!=0 && val[p]!=x)
    {
        p=urm[p];
    }
    if(p!=0)
    {
        val[p]=val[start[c]];
        start[c]=urm[start[c]];
    }
}
int main()
{
    int N;
    fin>>N;
    for(int i=1;i<=N;i++)
    {
        int tip,x;
        fin>>tip>>x;
        if(tip==1)
        {
            adauga(x);
        }
        if(tip==2)
        {
            sterge(x);
        }
        if(tip==3)
        {
            if(exista(x))
            {
                fout<<1<<'\n';
            }
            else
            {
                fout<<0<<'\n';
            }
        }
    }
    return 0;
}