Cod sursa(job #2071306)

Utilizator mlc_oficialBoris Barca mlc_oficial Data 20 noiembrie 2017 16:23:20
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.07 kb
#include <fstream>
#include <iostream>

using namespace std;

class IntegralTrie {
  public:
    explicit IntegralTrie() { }
    
    void Insert(int number) {
        Node* current = root;
        for (int sh = 32; sh >= 0; sh -= kBits) {
            const int shift = max(sh - kBits, 0);
            if (current->son[(number >> shift) & kMask] == nullptr) {
                current->son[(number >> shift) & kMask] = new Node();
            }

            current = current->son[(number >> shift) & kMask];
        }
        current->cnt = true;
    }

    void Erase(int number) {
        Node* current = root;
        for (int sh = 32; sh >= 0; sh -= kBits) {
            const int shift = max(sh - kBits, 0);
            if (current->son[(number >> shift) & kMask] == nullptr) {
                return;
            }

            current = current->son[(number >> shift) & kMask];
        }
        current->cnt = false;
    }

    bool Find(int number) {
        Node* current = root;
        for (int sh = 32; sh >= 0; sh -= kBits) {
            const int shift = max(sh - kBits, 0);
            if (current->son[(number >> shift) & kMask] == nullptr) {
                return false;
            }

            current = current->son[(number >> shift) & kMask];
        }
        return current->cnt;
    }

  private:
    static constexpr int kBits = 3;
    static constexpr int kMask = (1 << kBits) - 1;
    
    struct Node {
        Node* son[1 << kBits];
        bool cnt;

        explicit Node() : cnt(0) {
            for (int i = 0; i < (1 << kBits); i += 1) {
                son[i] = nullptr;
            }
        }
    };
    
    Node* root = new Node();
};

int main() {
#ifdef INFOARENA
    ifstream cin("hashuri.in");
    ofstream cout("hashuri.out");
#endif

    IntegralTrie trie;
    int num_queries; cin >> num_queries;
    while (num_queries--) {
        int op_type, el; cin >> op_type >> el;
        if (op_type == 1) {
            trie.Insert(el);
        } else if (op_type == 2) {
            trie.Erase(el);
        } else {
            cout << trie.Find(el) << '\n';
        }
    }
}