Cod sursa(job #1518598)

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

using namespace std;

const int MAX_SIZE = 1 << 21;
const int EMPTY = -1;
const int ERASED = -2;

const int MASK = MAX_SIZE - 1;

int table[MAX_SIZE];

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

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

    table[p] = key;
}

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

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

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

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

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

    return (table[p] == key);
}

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

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

    int N;
    in >> N;

    while (N--)
    {
        int t, x;
        in >> t >> x;

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

    return 0;
}