Cod sursa(job #1537196)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 26 noiembrie 2015 23:58:40
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>

using namespace std;

const int MAX_SIZE = 1 << 21;
const int MASK = MAX_SIZE - 1;

const int EMPTY = -1;
const int ERASED = -2;

int table[MAX_SIZE];

void insert(const int key)
{
    int p = (91 * key) & MASK;

    while (table[p] != key && table[p] != EMPTY && table[p] != ERASED)
        p = (p + 1) & MASK;

    table[p] = key;
}

bool find(const int key)
{
    int p = (91 * key) & MASK;

    while (table[p] != key && table[p] != EMPTY)
        p = (p + 1) & MASK;

    return table[p] == key;
}

void erase(const int key)
{
    int p = (91 * key) & MASK;

    while (table[p] != key && table[p] != EMPTY)
        p = (p + 1) & MASK;

    if (table[p] == key)
        table[p] = ERASED;
}

int main()
{
    ifstream in("hashuri.in");
    ofstream out("hashuri.out");

    for (int i = 0; i < MAX_SIZE; ++i)
        table[i] = EMPTY;

    int N, X, tip;

    in >> N;

    while (N--)
    {
        in >> tip >> X;

        if (tip == 1)
            insert(X);
        else if (tip == 2)
            erase(X);
        else
            out << find(X) << "\n";
    }

    return 0;
}