Cod sursa(job #1083184)

Utilizator Theorytheo .c Theory Data 15 ianuarie 2014 18:28:17
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>

using namespace std;

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

class LinearProbing {

    static const unsigned M = 20;
    static const unsigned MASK = (1 << M) - 1;

    private:

    unsigned H[MASK + 1];

    inline bool CheckSpace(int pos, int value) {

        return H[pos] && H[pos] != value;
    }

    inline int FindPosition(int value) {

        unsigned pos = 0;

        while ( CheckSpace( (value + pos) & MASK , value) ) ++pos;

        return (value + pos) & MASK;
    }

    public:

    void insert(int value) {

        H[ FindPosition(value) ] = value;
    }

    void erase(int value) {

        unsigned pos = FindPosition(value);

        if(H[pos] == value) H[pos] = -1;
    }

    bool find(int value) {

        return H[FindPosition(value)] == value;
    }
};


int N; LinearProbing LP;


int main() {


    fin >> N;

    while(N--) {

        int type; int value;

        fin >> type >> value;

        switch(type) {

            case 1: LP.insert(value); break;
            case 2: LP.erase(value); break;
            case 3: fout << LP.find(value) << '\n'; break;
        }
    }
    return 0;
}