Cod sursa(job #2846257)

Utilizator cdenisCovei Denis cdenis Data 9 februarie 2022 00:34:18
Problema Hashuri Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.66 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int MAX=1e6+5;
const int P=666013;
int n,op,x;
struct nod
{
    int info;
    nod *urm;
}*H[P];

void adauga(int x)
{
    bool apare=0;
    int xmod=x%P;
    nod *p=H[xmod];
    while(p && !apare)
    {
        if(p->info==x)
            apare=1;
        p=p->urm;
    }
    if(!apare)
//        if(H[xmod])
        {
            nod *u=new nod;
            u->info=x;
            u->urm=H[xmod];
            H[xmod]=u;
//        }
//        else
//        {
//            H[xmod]=new nod;
//            H[xmod]->info=x;
//            H[xmod]->urm=NULL;
        }
}

void sterge(int x)
{
    int xmod=x%P;
    nod *p=H[xmod];
    if(p && p->info==x)
        if(p->urm)
        {
            nod *u=p;
            p=p->urm;
            delete u;
        }
        else
        {
            p->info=-1;
            p->urm=NULL;
        }
    nod *ant=p;
    if(p)
        p=p->urm;
    while(p)
    {
        if(p->info==x)
        {
            ant->urm=p->urm;
            delete p;
            return;
        }
        p=p->urm;
    }
}

bool cauta(int x)
{
    int xmod=x%P;
    nod *p=H[xmod];
    while(p)
    {
        if(p->info==x)
            return 1;
        p=p->urm;
    }
    return 0;
}

int main()
{
    fin >> n;
    while(n--)
    {
        fin >> op >> x;
        switch(op)
        {
            case 1 : adauga(x); break;
            case 2 : sterge(x); break;
            case 3 : fout << cauta(x) << '\n'; break;
        }
    }
    return 0;
}