Cod sursa(job #1700236)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 9 mai 2016 21:10:39
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.95 kb
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1000000;
const int MAX_LOG = 31;
const int MAX_NODES = MAX_N * MAX_LOG;
const int NIL = -1;
const int BUFFSIZE = 528257;

int T[MAX_NODES + 1][2];

inline char getChar() {
    static char buff[BUFFSIZE];
    static int ptr = BUFFSIZE;
    if (ptr == BUFFSIZE) {
        fread(buff, 1, BUFFSIZE, stdin);
        ptr = 0;
    }
    return buff[ptr++];
}

inline int readInt() {
    int q = 0;
    char c;
    do {
        c = getChar();
    } while (!isdigit(c));
    do {
        q = (q << 1) + (q << 3) + (c - '0');
        c = getChar();
    } while (isdigit(c));
    return q;
}

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

    int N = readInt();
    int allocPtr = 1;

    T[0][0] = T[0][1] = NIL;

    while (N--) {
        int opType = readInt(), x = readInt();
        if (opType == 1) {
            int node = 0;
            for (int i = MAX_LOG - 1; i >= 0; -- i) {
                const bool bit = ((x >> i) & 1);
                if (T[node][bit] == NIL) {
                    T[allocPtr][0] = T[allocPtr][1] = NIL;
                    T[node][bit] = allocPtr++;
                }
                node = T[node][bit];
            }
        } else if (opType == 2) {
            int node = 0;
            int i = MAX_LOG - 1;
            while (i > 0 && node != NIL) {
                node = T[node][((x >> i) & 1)];
                -- i;
            }
            if (i == 0) {
                T[node][x & 1] = NIL;
            }
        } else {
            int node = 0;
            int i = MAX_LOG - 1;
            while (i >= 0 && node != NIL) {
                node = T[node][((x >> i) & 1)];
                -- i;
            }
            putchar('0' + (node != NIL));
            putchar('\n');
        }
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}