Cod sursa(job #2198078)

Utilizator ioana9753Ioana Pravai ioana9753 Data 23 aprilie 2018 16:23:20
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>

using namespace std;

const int R = 666019;
const int N = 100001;
const int M = 200001;

ifstream cin("hashuri.in");
ofstream cout("hashuri.out");

int lst[N], val[2 * M], urm[2 * M], nr, n;

bool cauta(int x)
{
    int r = x % R, p;
    p = lst[r];
    while(p != 0)
    {
        if(val[p] == x)
        {
            return true;
        }
        p = urm[p];
    }
    return false;
}

void adauga(int x)
{
    if(cauta(x))
    {
        return;
    }
    int r = x % R, p;
    val[++nr] = x;
    urm[nr] = lst[r];
    lst[r] = nr;
}

void sterge(int x)
{
    int r = x % R, p;
    p = lst[r];
    if(val[p] == x)
    {
        lst[r] = urm[p];
        return;
    }
    while((urm[p] != 0) && (val[urm[p]] != x))
    {
        p = urm[p];
    }
    if(urm[p] != 0)
    {
        urm[p] = urm[urm[p]];
    }
}

int main()
{
    int op, x, i;
    cin >> n;
    for(i = 0; i < n; i++)
    {
        cin >> op >> x;
        if(op == 1)
        {
            adauga(x);
        }
        if(op == 2)
        {

            sterge(x);

        }
        if(op == 3)
        {
            if(cauta(x))
                cout << "1\n";
            else
                cout << "0\n";
        }
    }
    return 0;
}