Cod sursa(job #1479146)

Utilizator retrogradLucian Bicsi retrograd Data 30 august 2015 17:15:46
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <cstdio>

using namespace std;

#define gc getchar
#define isdigit(c) (c>='0'&&c<='9')
char c;
void Read(int &a) {
    for(c=gc(); !isdigit(c); c=gc());
    for(a=0; isdigit(c); a=a*10+c-'0', c=gc());
}


#define P 4098329
#define B 37

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() {
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);

    int m, t, a;

    Read(m);
    while(m--) {
        Read(t); Read(a);
        if(t == 1) H.insert(a);
        if(t == 2) H.erase(a);
        if(t == 3) printf("%d\n", H.find(a));
    }

    return 0;
}