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:
parent
c92a7218b9
commit
fb12fb3896
52
Luogu/problem/U109895/U109895.cpp
Normal file
52
Luogu/problem/U109895/U109895.cpp
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user