Cod sursa(job #2527855)

Utilizator Rares31100Popa Rares Rares31100 Data 20 ianuarie 2020 22:00:32
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>

using namespace std;

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

typedef struct lista* nod;
struct lista
{
    int val;
    nod urm;
};

int n;
nod a[1000007];

void adauga(int x)
{
    int key=x%1000007;

    if(a[key]==NULL)
    {
        a[key]=new lista;
        a[key]->val=x;
    }
    else
    {
        nod p=a[key];

        while(p->urm)
            p=p->urm;

        nod add=new lista;
        add->val=x;
        p->urm=add;
    }
}

void sterge(int x)
{
    int key=x%1000007;

    if(a[key]==NULL)
        return;
    else if(a[key]->val==x)
        a[key]=a[key]->urm;
    else
    {
        nod p=a[key];
        while(p->urm && p->urm->val!=x)
            p=p->urm;

        if(p->urm)
            p->urm=p->urm->urm;
    }
}

bool gaseste(int x)
{
    int key=x%1000007;

    if(a[key]==NULL)
        return 0;
    else
    {
        nod p=a[key];
        while(p)
        {
            if(p->val==x)
                return 1;

            p=p->urm;
        }
    }

    return 0;
}

int main()
{
    in>>n;

    for(int c,x,k=1;k<=n;k++)
    {
        in>>c>>x;

        switch(c)
        {
            case 1:adauga(x);break;
            case 2:sterge(x);break;
            case 3:out<<gaseste(x)<<'\n';break;
        }
    }

    return 0;
}