0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 18:52:02 +00:00

H1002. 【模板】字典树 2

https://hydro.ac/record/615807f297ec9cde15d819a2
This commit is contained in:
Baoshuo Ren 2021-10-02 15:19:28 +08:00 committed by Baoshuo Ren
parent 40ae7f0030
commit 4c70b1085c
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -2,54 +2,37 @@
using namespace std; using namespace std;
int n, a[1000005], ans; int n, a[1000005], trie[10000005][2], p = 1, ans;
struct node { void insert(int u, int x, int b) {
int val; if (!x) return;
node* next[2]; if (!trie[u][(x >> b) & 1]) trie[u][(x >> b) & 1] = ++p;
insert(trie[u][(x >> b) & 1], x - (((x >> b) & 1) << b), b - 1);
~node() {
for (auto i : next) {
delete i;
}
}
};
void insert(node* root, int x, int b) {
if (!x) {
root->val++;
return;
}
if (root->next[(x >> b) & 1] == nullptr) root->next[(x >> b) & 1] = new node();
insert(root->next[(x >> b) & 1], x - (((x >> b) & 1) << b), b - 1);
} }
int search(node* root, int x) { int search(int u, int x) {
int res = 0; int res = 0, t = u;
node* p = root;
for (int i = 30; i >= 0; i--) { for (int i = 30; i >= 0; i--) {
if (p == nullptr) break; if (!t) break;
if (p->next[!((x >> i) & 1)] != nullptr) { if (trie[t][!((x >> i) & 1)]) {
res += 1 << i; res += 1 << i;
p = p->next[!((x >> i) & 1)]; t = trie[t][!((x >> i) & 1)];
} else { } else {
p = p->next[(x >> i) & 1]; t = trie[t][(x >> i) & 1];
} }
} }
return res; return res;
} }
int main() { int main() {
node* root = new node();
scanf("%d", &n); scanf("%d", &n);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
scanf("%d", a + i); scanf("%d", a + i);
insert(root, a[i], 30); insert(1, a[i], 30);
} }
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
ans = max(ans, search(root, a[i])); ans = max(ans, search(1, a[i]));
} }
printf("%d\n", ans); printf("%d\n", ans);
delete root;
return 0; return 0;
} }