Cod sursa(job #2619600)

Utilizator AlexVulpoiuAlexandru Vulpoiu AlexVulpoiu Data 28 mai 2020 01:32:50
Problema Hashuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.97 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

const int prim = 666013;

struct nod
{
    int val;
    nod *urm, *ant;
};

struct lista
{
    nod *p, *u;
};

int n, op, x;
lista H[666015];

void insereaza(lista& L, const int& x)
{
    nod *aux;

    aux = new nod;
    aux->val = x;
    aux->urm = NULL;
    aux->ant = L.u;
    if(L.p == NULL)
    {
        L.p = aux;
        L.u = aux;
        aux = NULL;
    }
    else
    {
        L.u->urm = aux;
        L.u = aux;
    }
    delete aux;
}

void sterge(lista& L, const int& x)
{
    nod *it;

    if(L.p != NULL && L.p->val == x)
    {
        if(L.p == L.u)
        {
            L.p = NULL;
            L.u = L.p;
        }
        else
        {
            L.p = L.p->urm;
            L.p->ant = NULL;
        }
        return;
    }
    if(L.u != NULL && L.u->val == x)
    {
        L.u = L.u->ant;
        L.u->urm = NULL;
        return;
    }
    for(it = L.p; it != NULL; it = it->urm)
        if(it->val == x)
        {
            if(it->urm != NULL)
                (it->urm)->ant = it->ant;
            if(it->ant != NULL)
                (it->ant)->urm = it->urm;
            it->urm = NULL;
            it->ant = NULL;
            delete it;
            break;
        }
}

bool gaseste(lista& L, const int& x)
{
    nod *it;

    for(it = L.p; it != NULL; it = it->urm)
        if(it->val == x)
            return true;

    return false;
}

int main()
{
    f >> n;
    while(n)
    {
        f >> op >> x;

        if(op == 1)
        {
            if(!gaseste(H[x % prim], x))
                insereaza(H[x % prim], x);
        }
        else
            if(op == 2)
                sterge(H[x % prim], x);
            else
                g << gaseste(H[x % prim], x) << '\n';

        n--;
    }

    f.close();
    g.close();

    return 0;
}