Cod sursa(job #1478493)

Utilizator retrogradLucian Bicsi retrograd Data 28 august 2015 19:26:21
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <cstdio>
#include <algorithm>

using namespace std;
typedef int var;

struct Trie {
    #define P 1048576

    #define val first
    #define nxt second

    pair<int, int> T[1000005];
    int B[P], nodes = 0;

    int find(int x) {
        for(int y=B[x % P]; y; y = T[y].nxt)
            if(T[y].val == x)
                return y;
        return 0;
    }

    void erase(int x) {
        int y = find(x);
        if(y) T[y].val = 0;
    }

    void insert(int x) {
        int y = find(x);
        if(!y) { T[++nodes] = {x, B[x % P]}; B[x % P] = nodes; }
    }
};
Trie T;

#define isdigit(c) (c>='0'&&c<='9')
struct Scanner {
    #define DIM 100000
    char buff[DIM];
    int poz = DIM - 1;
    bool sign;

    Scanner& operator >> (var &a) {
        #define adv() (++poz == DIM) ? fread(buff, 1, DIM, stdin), poz=0 : 0
        for(sign = 0; !isdigit(buff[poz]) && buff[poz] != '-'; adv());
        if(buff[poz] == '-') sign = 1, adv();
        for(a = 0; isdigit(buff[poz]); a = a * 10 + buff[poz] - '0', adv());
        if(sign) a = -a;

        return *this;
    }
};
Scanner in;

int main() {
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);

    int m, t, a;
    in >> m;
    while(m--) {
        in >> t >> a;
        if(t == 1) T.insert(a);
        if(t == 2) T.erase(a);
        if(t == 3) printf("%d\n", (T.find(a) != 0));
    }

    return 0;
}