-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.cpp
56 lines (52 loc) · 1.54 KB
/
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
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
class BinaryIndexedTree {
private:
int n;
vector<int> c;
public:
BinaryIndexedTree(int n) {
this->n = n;
c.resize(n + 1, -1);
}
void update(int x, int v) {
while (x <= n) {
c[x] = max(c[x], v);
x += x & -x;
}
}
int query(int x) {
int mx = -1;
while (x > 0) {
mx = max(mx, c[x]);
x -= x & -x;
}
return mx;
}
};
class Solution {
public:
vector<int> maximumSumQueries(vector<int>& nums1, vector<int>& nums2, vector<vector<int>>& queries) {
vector<pair<int, int>> nums;
int n = nums1.size(), m = queries.size();
for (int i = 0; i < n; ++i) {
nums.emplace_back(-nums1[i], nums2[i]);
}
sort(nums.begin(), nums.end());
sort(nums2.begin(), nums2.end());
vector<int> idx(m);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&](int i, int j) { return queries[j][0] < queries[i][0]; });
vector<int> ans(m);
int j = 0;
BinaryIndexedTree tree(n);
for (int i : idx) {
int x = queries[i][0], y = queries[i][1];
for (; j < n && -nums[j].first >= x; ++j) {
int k = nums2.end() - lower_bound(nums2.begin(), nums2.end(), nums[j].second);
tree.update(k, -nums[j].first + nums[j].second);
}
int k = nums2.end() - lower_bound(nums2.begin(), nums2.end(), y);
ans[i] = tree.query(k);
}
return ans;
}
};