-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2940.cpp
More file actions
55 lines (45 loc) · 1.48 KB
/
Copy path2940.cpp
File metadata and controls
55 lines (45 loc) · 1.48 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
class Solution {
static int search(int height, const vector<pair<int, int>> &st) {
int low = 0, high = size(st) - 1;
int ans = -1;
while (low <= high) {
const int mid = low + (high - low) / 2;
if (st[mid].first > height) {
ans = max(ans, mid);
low = mid + 1;
} else {
high = mid - 1;
}
}
return ans;
}
public:
vector<int> leftmostBuildingQueries(const vector<int> &heights, const vector<vector<int>> &queries) {
const int n = size(heights), m = size(queries);
vector<vector<pair<int, int>>> nqueries(n);
vector<int> res(m, -1);
for (int i = 0; i < m; i++) {
int a = queries[i][0];
int b = queries[i][1];
if (a > b) swap(a, b);
if (heights[b] > heights[a] || a == b)
res[i] = b;
else
nqueries[b].emplace_back(heights[a], i);
}
vector<pair<int, int>> st;
for (int i = n - 1; i >= 0; i--) {
for (const auto &[a, b] : nqueries[i]) {
const auto pos = search(a, st);
if (pos >= 0 && pos < size(st)) {
res[b] = st[pos].second;
}
}
while (!st.empty() && st.back().first <= heights[i]) {
st.pop_back();
}
st.emplace_back(heights[i], i);
}
return res;
}
};