Cod sursa(job #1443555)

Utilizator cristinamateiCristina Matei cristinamatei Data 28 mai 2015 09:36:32
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int M = 666019;
int n;
int val[1000001], urm[1000001], lst[M], nr = 0;

void adauga( long long int x )
{
    int r = x % M;
    nr++;
    val[nr] = x;
    urm[nr] = lst[r];
    lst[r] = nr;
}

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

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

int main()
{
    long long int x;
    int p;
    in >> n;
    for ( int i = 1; i <= n; i++ )
    {
        in >> p >> x;
        if ( p == 1 )
        {
            if ( cauta(x) == false )
                adauga(x);
        }
        if ( p == 2 )
        {
            sterge(x);
        }
        if ( p == 3 )
        {
            if ( cauta(x) == true )
                out << 1 <<'\n';
            else out << 0 <<'\n';
        }
    }
    return 0;
}