From fb12fb3896fce63a2e09db0616cee76d61d3cca6 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Mon, 27 Sep 2021 20:54:31 +0800 Subject: [PATCH] U109895 [HDU4825]Xor Sum R58660572 --- Luogu/problem/U109895/U109895.cpp | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Luogu/problem/U109895/U109895.cpp diff --git a/Luogu/problem/U109895/U109895.cpp b/Luogu/problem/U109895/U109895.cpp new file mode 100644 index 00000000..0f5b8450 --- /dev/null +++ b/Luogu/problem/U109895/U109895.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +int t, n, m; +long long a; + +struct node { + long long val; + node* next[2]; + + ~node() { + for (auto& i : next) { + delete i; + } + } +}; + +void insert(node* root, long long x) { + for (int i = 31; i >= 0; i--) { + if (root->next[(x >> i) & 1] == nullptr) root->next[(x >> i) & 1] = new node(); + root = root->next[(x >> i) & 1]; + } + root->val = x; +} + +long long query(node* root, long long x) { + for (int i = 31; i >= 0; i--) { + if (root == nullptr) break; + if (root->next[!((x >> i) & 1)] != nullptr) { + root = root->next[!((x >> i) & 1)]; + } else { + root = root->next[(x >> i) & 1]; + } + } + return root->val; +} + +int main() { + node* root = new node(); + scanf("%d%d", &n, &m); + for (int i = 0; i < n; i++) { + scanf("%lld", &a); + insert(root, a); + } + for (int i = 0; i < m; i++) { + scanf("%lld", &a); + printf("%lld\n", query(root, a)); + } + delete root; + return 0; +}