forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
31 lines (31 loc) · 961 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
31
class Solution {
public:
vector<int> countServers(int n, vector<vector<int>>& logs, int x, vector<int>& queries) {
sort(logs.begin(), logs.end(), [](const auto& a, const auto& b) {
return a[1] < b[1];
});
int m = queries.size();
vector<pair<int, int>> qs(m);
for (int i = 0; i < m; ++i) {
qs[i] = {queries[i], i};
}
sort(qs.begin(), qs.end());
unordered_map<int, int> cnt;
vector<int> ans(m);
int j = 0, k = 0;
for (auto& [r, i] : qs) {
int l = r - x;
while (k < logs.size() && logs[k][1] <= r) {
++cnt[logs[k++][0]];
}
while (j < logs.size() && logs[j][1] < l) {
if (--cnt[logs[j][0]] == 0) {
cnt.erase(logs[j][0]);
}
++j;
}
ans[i] = n - cnt.size();
}
return ans;
}
};