Cod sursa(job #3232024)

Utilizator SebyIliescuIliescu Sebastian Sorin SebyIliescu Data 28 mai 2024 17:54:59
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.48 kb
#include <iostream>
#include <fstream>
using namespace std;

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

#define MOD 666013;

struct nod
{
    int inf;
    nod* urm;
} * H[666013];

void add(int x)
{
    int ind = x % MOD;
    nod* p = new nod;
    p->inf = x;
    p->urm = H[ind];
    H[ind] = p;
}

void remove(int x)
{
    int ind = x % MOD;
    nod* p = new nod, * u = new nod;
    p = u = H[ind];
    if (H[ind])
    {
        if (H[ind]->inf == x)
            H[ind] = H[ind]->urm;
        else
        {
            while (p && p->inf != x)
            {
                u = p;
                p = p->urm;
            }

            if (p != NULL)
                u->urm = p->urm;
        }
    }
}

void print(int x)
{
    int ind = x % MOD;
    nod* p = H[ind];
    if (H[ind])
    {
        if (H[ind]->inf == x)
        {
            fout << "1\n";
            return;
        }
        else
        {
            while (p && p->inf != x)
                p = p->urm;

            if (p == NULL)
                fout << "0\n";
            else
                fout << "1\n";
            return;
        }
    }

    fout << "0\n";
}

int main()
{
    int n, op, x;
    fin >> n;

    for (int i = 1; i <= n; i++)
    {
        fin >> op >> x;
        if (op == 1)
            add(x);
        if (op == 2)
            remove(x);
        if (op == 3)
            print(x);
    }

    
    return 0;
}