This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub fffelix-huang/CP-stuff
#define PROBLEM "https://judge.yosupo.jp/problem/set_xor_min" #include <iostream> #include "../../../library/data-structure/binary-trie.hpp" using namespace std; using namespace felix; int main() { ios::sync_with_stdio(false); cin.tie(0); binary_trie<int> b; int q; cin >> q; while(q--) { int type, x; cin >> type >> x; if(type == 0) { if(!b.contains(x)) { b.insert(x); } } else if(type == 1) { if(b.contains(x)) { b.erase(x); } } else { cout << b.get_xor_min(x) << "\n"; } } return 0; }
#line 1 "test/data-structure/binary-trie/yosupo-Set-Xor-Min.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/set_xor_min" #include <iostream> #line 2 "library/data-structure/binary-trie.hpp" #include <vector> #include <array> #include <limits> namespace felix { template<class T> struct binary_trie { public: binary_trie() { new_node(); } void clear() { sz = 0; trie.clear(); new_node(); } void insert(T x) { for(int i = BITS - 1, p = 0; i >= 0; i--) { int y = x >> i & 1; if(trie[p].go[y] == 0) { trie[p].go[y] = new_node(); } p = trie[p].go[y]; trie[p].cnt++; } sz++; } void erase(T x) { for(int i = BITS - 1, p = 0; i >= 0; i--) { p = trie[p].go[x >> i & 1]; trie[p].cnt--; } sz--; } bool contains(T x) { for(int i = BITS - 1, p = 0; i >= 0; i--) { p = trie[p].go[x >> i & 1]; if(trie[p].cnt == 0) { return false; } } return true; } T get_min() const { return get_xor_min(0); } T get_max() const { return get_xor_max(0); } T get_xor_min(T x) const { if(empty()) { return std::numeric_limits<T>::max(); } T ans = 0; for(int i = BITS - 1, p = 0; i >= 0; i--) { int y = x >> i & 1; int z = trie[p].go[y]; if(z > 0 && trie[z].cnt > 0) { p = z; } else { ans |= T(1) << i; p = trie[p].go[y ^ 1]; } } return ans; } T get_xor_max(T x) const { if(empty()) { return std::numeric_limits<T>::min(); } T ans = 0; for(int i = BITS - 1, p = 0; i >= 0; i--) { int y = x >> i & 1; int z = trie[p].go[y ^ 1]; if(z > 0 && trie[z].cnt > 0) { ans |= T(1) << i; p = z; } else { p = trie[p].go[y]; } } return ans; } int size() const { return sz; } bool empty() const { return sz == 0; } private: static constexpr int BITS = sizeof(T) * 8; struct node_t { std::array<int, 2> go = {}; int cnt = 0; }; int sz = 0; std::vector<node_t> trie; int new_node() { trie.emplace_back(); return (int) trie.size() - 1; } }; } // namespace felix #line 5 "test/data-structure/binary-trie/yosupo-Set-Xor-Min.test.cpp" using namespace std; using namespace felix; int main() { ios::sync_with_stdio(false); cin.tie(0); binary_trie<int> b; int q; cin >> q; while(q--) { int type, x; cin >> type >> x; if(type == 0) { if(!b.contains(x)) { b.insert(x); } } else if(type == 1) { if(b.contains(x)) { b.erase(x); } } else { cout << b.get_xor_min(x) << "\n"; } } return 0; }