Cod sursa(job #1477555)

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

using namespace std;

template <const size_t MAX_SIZE>
class LinearProbing
{
private:

    static const int NIL = -1;
    static const int DELETED = -2;

    int table[MAX_SIZE];

    inline bool check(const size_t pos, const int key) const
    {
        return table[pos] != NIL && table[pos] != key;
    }

    int supposedPosition(const int key) const
    {
        size_t pos = 0;

        while (check((key + (pos >> 1) + ((pos * pos) >> 1)) % MAX_SIZE, key) == true)
            pos++;

        return (key + (pos >> 1) + ((pos * pos) >> 1)) % MAX_SIZE;
    }

public:

    LinearProbing()
    {
        for (int i = 0; i < MAX_SIZE; ++i)
            table[i] = NIL;
    }

    void insert(const int key)
    {
        table[supposedPosition(key)] = key;
    }

    bool find(const int key)
    {
        return table[supposedPosition(key)] == key;
    }

    void erase(const int key)
    {
        int pos = supposedPosition(key);

        if (table[pos] == key)
            table[pos] = DELETED;
    }
};

const int M = 1 << 20;

LinearProbing<M> L;

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

    ios_base::sync_with_stdio( false );

    int N;
    in >> N;

    while ( N-- )
    {
        int tip, x;

        in >> tip >> x;

        switch(tip)
        {
            case 1: L.insert(x); break;
            case 2: L.erase(x); break;
            case 3: out << L.find(x) << "\n"; break;

            default: cerr << "Error\n";
        }
    }

    return 0;
}