Cod sursa(job #1184827)

Utilizator IonSebastianIon Sebastian IonSebastian Data 14 mai 2014 11:20:10
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>

using namespace std;

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

const int MAX_N = 1000000, K = 1758900;

int lst[K], urm[MAX_N+1], val[MAX_N+1];

int nr;

bool check(int x)
{
    int p = lst[x%K];
    while(p != 0 && val[p] != x)
    {
        p = urm[p];
    }
    return p != 0;
}

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

void add(int x)
{
    if(!check(x))
    {
        int place = x%K;
        val[++nr] = x;
        urm[nr] = lst[place];
        lst[place] = nr;
    }
}

int main()
{
    int n, i, x;
    char type;

    in >> n;

    for(i = 0; i < n; i++)
    {
        in >> type >> x;
        if(type == '1')
            add(x);
        else if(type == '2')
                del(x);
        else out << check(x) << "\n";
    }
    return 0;
}