-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3534.cpp
More file actions
61 lines (59 loc) · 1.77 KB
/
Copy path3534.cpp
File metadata and controls
61 lines (59 loc) · 1.77 KB
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
class Solution {
int up[100005][18];
public:
int step(int u, int s) {
for (int i = 0; i <= 17; ++i) {
if ((s >> i) & 1) {
u = up[u][i];
}
}
return u;
}
vector<int> pathExistenceQueries(int n, vector<int>& nums, int maxDiff, vector<vector<int>>& queries) {
vector<pair<int, int>> arr;
for (int i = 0; i < n; ++i) {
arr.push_back({nums[i], i});
}
sort(arr.begin(), arr.end());
vector<int> indexMapping(n, -1);
for (int i = 0; i < n; ++i) {
indexMapping[arr[i].second] = i;
}
int right = 0;
for (int left = 0; left < n; ++left) {
while (right + 1 < n && arr[right + 1].first - arr[left].first <= maxDiff) right++;
up[left][0] = right;
}
for (int k = 1; k <= 17; ++k) {
for (int i = 0; i < n; ++i) {
up[i][k] = up[up[i][k - 1]][k - 1];
}
}
vector<int> res;
for (auto& q : queries) {
int x = indexMapping[q[0]];
int y = indexMapping[q[1]];
if (x > y) swap(x, y);
if (x == y) {
res.push_back(0);
continue;
}
int low = 1;
int high = 1e5;
while (low < high) {
int mid = low + (high - low) / 2;
int reach = step(x, mid);
if (arr[reach].first >= arr[y].first) high = mid;
else low = mid + 1;
}
int reach = step(x, low);
if (arr[reach].first >= arr[y].first) {
res.push_back(low);
}
else {
res.push_back(-1);
}
}
return res;
}
};