0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2025-01-11 18:51:59 +00:00

U109895 [HDU4825]Xor Sum

R58660572
This commit is contained in:
Baoshuo Ren 2021-09-27 20:54:31 +08:00 committed by Baoshuo Ren
parent c92a7218b9
commit fb12fb3896
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -0,0 +1,52 @@
#include <bits/stdc++.h>
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;
}