-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
mark-elements-on-array-by-performing-queries.cpp
41 lines (40 loc) · 1.35 KB
/
mark-elements-on-array-by-performing-queries.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
// Time: O(q + nlogn)
// Space: O(n)
// hash table, heap
class Solution {
public:
vector<long long> unmarkedSumArray(vector<int>& nums, vector<vector<int>>& queries) {
long long total = accumulate(cbegin(nums), cend(nums), 0ll);
vector<bool> lookup(size(nums));
vector<pair<int, int>> arr(size(nums));
for (int i = 0; i < size(nums); ++i) {
arr[i] = {nums[i], i};
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> min_heap(cbegin(arr), cend(arr));
vector<long long> result(size(queries));
for (int i = 0; i < size(queries); ++i) {
const int idx = queries[i][0];
const int k = queries[i][1];
if (!lookup[idx]) {
lookup[idx] = true;
total -= nums[idx];
}
for (int _ = 0; _ < k; ++_) {
while (!empty(min_heap)) {
const auto [x, i] = min_heap.top(); min_heap.pop();
if (lookup[i]) {
continue;
}
lookup[i] = true;
total -= x;
break;
}
if (empty(min_heap)) {
break;
}
}
result[i] = total;
}
return result;
}
};