Cod sursa(job #1772455)

Utilizator OFY4Ahmed Hamza Aydin OFY4 Data 6 octombrie 2016 19:17:32
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>

#define prm 101

using namespace std;

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

const int mod = 1 << 22;
int  h[mod], n, tp, mamaliga;

void inc(int &poz)
{
    if(++poz == mod)
         poz = 0;
}

int bul(int x)
{
    int poz = (1LL * x * prm) % mod;

    for(; h[poz] != x && h[poz] != 0; inc(poz));

    if(h[poz] == x)
        return poz;
    else
        return -1;
}

void ekle(int x)
{
    if(bul(x) != -1)
        return;
    int poz = (1LL * x * prm) % mod;

    for(; h[poz] != x && h[poz] > 0; inc(poz));

    h[poz] = x;
}

void sil(int x)
{
    int poz = bul(x);
    if(poz != -1)
        h[poz] = -1;
}

int main()
{
    in >> n;
    for(int i = 1; i <= n; ++i)
    {
        in >> tp >> mamaliga;

        if(tp == 1)
            ekle(mamaliga);
        else if(tp == 2)
            sil(mamaliga);
        else if(bul(mamaliga) == -1)
            out << 0 << "\n";
        else out << 1 << "\n";
    }
}