0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

HDU-4825 Xor Sum

36744295
This commit is contained in:
Baoshuo Ren 2021-09-14 19:46:50 +08:00 committed by Baoshuo Ren
parent a33ed868b0
commit 279c499ca5
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

56
HDU/4825/4825.cpp Normal file
View File

@ -0,0 +1,56 @@
#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;
}