-
Notifications
You must be signed in to change notification settings - Fork 47
/
L6MaxXorQueriesCpp
73 lines (70 loc) · 1.79 KB
/
L6MaxXorQueriesCpp
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
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
struct Node {
Node *links[2];
bool containsKey(int ind) {
return (links[ind] != NULL);
}
Node* get(int ind) {
return links[ind];
}
void put(int ind, Node* node) {
links[ind] = node;
}
};
class Trie {
private: Node* root;
public:
Trie() {
root = new Node();
}
public:
void insert(int num) {
Node* node = root;
// cout << num << endl;
for(int i = 31;i>=0;i--) {
int bit = (num >> i) & 1;
if(!node->containsKey(bit)) {
node->put(bit, new Node());
}
node = node->get(bit);
}
}
public:
int findMax(int num) {
Node* node = root;
int maxNum = 0;
for(int i = 31;i>=0;i--) {
int bit = (num >> i) & 1;
if(node->containsKey(!bit)) {
maxNum = maxNum | (1<<i);
node = node->get(!bit);
}
else {
node = node->get(bit);
}
}
return maxNum;
}
};
vector<int> maxXorQueries(vector<int> &arr, vector<vector<int>> &queries){
vector<int> ans(queries.size(), 0);
vector<pair<int, pair<int,int>>> offlineQueries;
sort(arr.begin(), arr.end());
int index = 0;
for(auto &it: queries) {
offlineQueries.push_back({it[1],{it[0], index++}});
}
sort(offlineQueries.begin(), offlineQueries.end());
int i = 0;
int n = arr.size();
Trie trie;
for(auto &it : offlineQueries) {
while(i < n && arr[i] <= it.first) {
trie.insert(arr[i]);
i++;
}
if(i!=0) ans[it.second.second] = trie.findMax(it.second.first);
else ans[it.second.second] = -1;
}
return ans;
}