Cod sursa(job #2619590)

Utilizator AlexVulpoiuAlexandru Vulpoiu AlexVulpoiu Data 28 mai 2020 00:53:40
Problema Hashuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.99 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)
    {
        nod *aux = L.p;
        if(L.p == L.u)
        {
            L.p = L.p->urm;
            L.u = L.p;
        }
        else
            L.p = L.p->urm;
        delete aux;
        return;
    }
    if(L.u != NULL && L.u->val == x)
    {
        nod *aux = L.u;
        L.u = L.u->ant;
        delete aux;
        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 cauta(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()
{
    for(x = 0; x < 666013; x++)
        H[x].p = H[x].u = NULL;
    f >> n;
    while(n)
    {
        f >> op >> x;

        if(op == 1)
            insereaza(H[abs(x) % prim], x);
        else
            if(op == 2)
                sterge(H[abs(x) % prim], x);
            else
                g << cauta(H[abs(x) % prim], x) << '\n';

        n--;
    }

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

    return 0;
}