Cod sursa(job #1479139)

Utilizator retrogradLucian Bicsi retrograd Data 30 august 2015 17:07:55
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>

using namespace std;


#define P 3801298
#define B 28

struct HashMap {
    int V[P];
    void insert(int val) {
        int pos = val % P;
        while(V[pos]) {
            if(V[pos] == val) return;
            pos = pos * B % P;
        }
        V[pos] = val;
    }

    void erase(int val) {
        int pos = val % P;
        while(V[pos] != val) {
            if(V[pos] == 0) return;
            pos = pos * B % P;
        }
        V[pos] = -1;
    }

    bool find(int val) {
        int pos = val % P;
        while(V[pos]) {
            if(V[pos] == val) return 1;
            pos = pos * B % P;
        }
        return 0;
    }
};
HashMap H;

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

    int m, t, a;

    fin>>m;
    while(m--) {
        fin>>t>>a;
        if(t == 1) H.insert(a);
        if(t == 2) H.erase(a);
        if(t == 3) fout<<H.find(a)<<'\n';
    }

    return 0;
}