forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
30 lines (30 loc) · 957 Bytes
/
Solution.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
class Solution {
public:
vector<int> minInterval(vector<vector<int>>& intervals, vector<int>& queries) {
int n = intervals.size(), m = queries.size();
sort(intervals.begin(), intervals.end());
using pii = pair<int, int>;
vector<pii> qs;
for (int i = 0; i < m; ++i) {
qs.emplace_back(queries[i], i);
}
sort(qs.begin(), qs.end());
vector<int> ans(m, -1);
priority_queue<pii, vector<pii>, greater<pii>> pq;
int i = 0;
for (auto& [x, j] : qs) {
while (i < n && intervals[i][0] <= x) {
int a = intervals[i][0], b = intervals[i][1];
pq.emplace(b - a + 1, b);
++i;
}
while (!pq.empty() && pq.top().second < x) {
pq.pop();
}
if (!pq.empty()) {
ans[j] = pq.top().first;
}
}
return ans;
}
};