Cod sursa(job #797551)

Utilizator mihai995mihai995 mihai995 Data 14 octombrie 2012 12:48:42
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
#include <vector>
using namespace std;

const int K = 1046527, inf = 0x3f3f3f3f;

struct HashTable{
    int hash[K];

    inline int h(int k, int i){
        return (k + i * (i + 1)) % K;
    }

    inline bool can_write(int x, int i){
        int q = h(x, i);

        return !hash[q] || hash[q] == inf || hash[q] == x;
    }

    inline bool move_on(int x, int i){
        int q = h(x, i);

        return hash[q] && hash[q] != x;
    }

    bool find(int x){
        int i = 0;

        while (move_on(x, i))
            i++;

        return hash[h(x, i)] == x;
    }

    void insert(int x){
        int i = 0;

        while (!can_write(x, i))
            i++;

        hash[h(x, i)] = x;
    }

    void erase(int x){
        int i = 0;

        while (move_on(x, i))
            i++;

        if (hash[h(x, i)] == x)
            hash[h(x, i)] = inf;
    }
};

HashTable H;

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

int main(){
    int t, q, x;

    in >> t;

    while (t--){
        in >> q >> x;

        if (q == 1)
            H.insert(x);

        if (q == 2)
            H.erase(x);

        if (q == 3)
            out << H.find(x) << "\n";
    }

    return 0;
}