-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path1707. Maximum XOR With an Element From Array.cpp
64 lines (62 loc) · 1.56 KB
/
1707. Maximum XOR With an Element From Array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
/*
1. 将 queries 排序,从小到大处理,每处理一个 query,将 <= m_i 的 num 放入 trie 数中
2. 剩下的步骤就是求最大异或对了
*/
const int N = 32 * 1e5 + 10;
int trie[N][2], tot = 1;
// O(1e5)
void init_trie() {
memset(trie, 0, sizeof trie);
tot = 1;
}
// O(32)
void insert(int x) {
int p = 1;
for (int i = 31; i >= 0; i--) {
int j = x >> i & 1;
if (trie[p][j] == 0) trie[p][j] = ++tot;
p = trie[p][j];
}
}
// O(32)
int max_xor(int x) {
int p = 1, q = 1;
int ans = 0;
for (int i = 31; i >= 0; i--) {
int j = x >> i & 1;
p = trie[p][j];
if (trie[q][!j]) {
q = trie[q][!j];
ans |= 1 << i;
} else {
q = trie[q][j];
}
}
return ans;
}
class Solution {
public:
vector<int> maximizeXor(vector<int>& nums, vector<vector<int>>& queries) {
int n = nums.size(), m = queries.size();
init_trie();
sort(nums.begin(), nums.end());
for (int i = 0; i < m; i++) {
queries[i].push_back(i);
}
sort(queries.begin(), queries.end(), [&queries](auto& a, auto& b) {
return a[1] < b[1];
});
vector<int> ans(m, -1);
for (int i = 0, j = 0; i < m; i++) {
while (j < n && nums[j] <= queries[i][1]) {
insert(nums[j]);
j++;
}
if (j > 0)
ans[queries[i][2]] = max_xor(queries[i][0]);
}
return ans;
}
};