Cod sursa(job #2596966)

Utilizator Robert.BrindeaBrindea Robert Robert.Brindea Data 10 aprilie 2020 20:49:03
Problema Hashuri Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int MOD = 666013;
int n;

class lista
{
public:
    int val;
    lista *next;
    lista(int x)
    {
        next = nullptr;
        val = x;
    }
    lista* inserare(int x)
    {
        lista *p = new lista(x);
        p->next = this;
        return p;
    }
    lista* stergere(lista *p, int x)
    {
        if(p->val == x)
        {
            return this->next;
        }
        lista* aux = p->next;
        p->next = stergere(p->next, x);
        if(p->next == nullptr)
            delete aux;
    }
    bool valid(int x)
    {
        lista *p = this;
        while(p != nullptr)
        {
            if(p->val == x)
                return 1;
            p = p->next;
        }
        return 0;
    }
};

lista *liste[MOD];

int main()
{
    fin >> n;
    for(int i = 0; i < n; i++)
    {
        int op, x;
        fin >> op >> x;
            if(op == 1)
            {
                if(liste[x%MOD] == nullptr)
                    liste[x%MOD] = new lista(x);
                else
                    liste[x%MOD] = liste[x%MOD]->inserare(x);
            }
            if(op == 2)
            {
                if(liste[x%MOD] != nullptr)
                    liste[x%MOD] = liste[x%MOD]->stergere(liste[x%MOD], x);
            }
            if(op == 3)
            {
                if(liste[x%MOD] == nullptr)
                    fout << "0\n";
                else
                    fout << liste[x%MOD]->valid(x) << "\n";
            }
    }
    return 0;
}