Cod sursa(job #2457332)

Utilizator alexdumitrescuDumitrescu George Alex alexdumitrescu Data 17 septembrie 2019 13:24:23
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <fstream>
#define mod 666013
using namespace std;
ifstream fin ("hashuri.in");
ofstream fout ("hashuri.out");

struct nod
{
    int val;
    nod *urm, *prev;
};
nod *p[mod];
nod *cauta(int x)
{
    int l=x%mod;
    nod *q=p[l];
    while(q)
    {
        if(q->val==x)
            return q;
        q=q->urm;
    }
    return NULL;
}
void adauga(int x)
{
    int l=x%mod;
    nod *q=new nod;
    q->urm=p[l]; q->prev=NULL; q->val=x;
    if(p[l]!=NULL)
        p[l]->prev=q;
    p[l]=q;
}
void sterge(nod *q)
{
    int l=q->val%mod;
    if(q==p[l])
        p[l]=p[l]->urm;
    else
    {
        q->prev->urm=q->urm;
        if(q->urm!=NULL)
            q->urm->prev=q->prev;
    }
    delete q;
}
int n, i, t, x;
int main()
{
    fin >> n;
    for(i=1;i<=n;i++)
    {
        fin >> t >> x;
        nod *q;
        q=cauta(x);
        if(q==NULL)
        {
            if(t==1)
                adauga(x);
            else if(t==3)
                fout << "0\n";
        }
        else
        {
            if(t==2)
                sterge(q);
            else if(t==3)
                fout << "1\n";
        }
    }
    return 0;
}