0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-10 03:38:47 +00:00
OI-codes/HDU/4825/4825.cpp
2021-09-14 19:46:50 +08:00

57 lines
1.2 KiB
C++

#include <bits/stdc++.h>
using namespace std;
int t, n, m, a;
struct node {
int val;
node* next[2];
~node() {
for (auto& i : next) {
delete i;
}
}
};
void insert(node* root, int x) {
for (int i = 30; 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;
}
int query(node* root, int x) {
for (int i = 30; 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() {
std::ios::sync_with_stdio(false);
cin >> t;
for (int i = 1; i <= t; i++) {
cout << "Case #" << i << ":" << endl;
node* root = new node();
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a;
insert(root, a);
}
for (int i = 0; i < m; i++) {
cin >> a;
cout << query(root, a) << endl;
}
delete root;
}
return 0;
}