Cod sursa(job #678554)

Utilizator ion_calimanUAIC Ion Caliman ion_caliman Data 11 februarie 2012 22:09:39
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
using namespace std;

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

#define M 1010101

int A[M], N, B[M];

int H(int x, int p)
{
    return ((x%M)*p)%M;
}

void insereaza(int x)
{
    int p=1;
    while (A[H(x,p)] && A[H(x,p)]!=x) p++;
    A[H(x,p)] = x;
    B[H(x,p)] = 0;
}

void sterge(int x)
{
    int p=1;
    while ((A[H(x,p)] || B[H(x,p)]) && A[H(x,p)]!=x) p++;
    A[H(x,p)] = 0;
    B[H(x,p)] = 1;
}

bool cauta(int x)
{
    int p=1;
    while ((A[H(x,p)] || B[H(x,p)]) && A[H(x,p)]!=x) p++;
    return A[H(x,p)] == x;
}

int main()
{
    int c,x;

    f >> N;
    for (;N--;){
        f >> c >> x;
        if (c==1) insereaza(x); else
        if (c==2) sterge(x); else
        if (c==3) g << cauta(x) << '\n';
    }

    return 0;
}