Cod sursa(job #2586267)

Utilizator mihai2003LLL LLL mihai2003 Data 20 martie 2020 11:51:22
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>

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

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

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

bool apartine(int x){
    int c=x%M;
    int p=lst[c];
    while(p!=0){
        if(val[p]==x)
            return 1;
        p=urm[p];
    }
    return 0;
}

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 t;
    in>>t;
    while(t){
        t--;
        int op,x;
        in>>op>>x;
        if(op==1)
            adauga(x);
        else
            if(op==3)
                out<<apartine(x)<<'\n';
            else
                sterge(x);
    }
    return 0;
}