forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries-on-a-permutation-with-key.cpp
52 lines (45 loc) · 1.07 KB
/
queries-on-a-permutation-with-key.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
// Time: O(nlogn)
// Space: O(n)
class BIT { // Fenwick Tree, 1-indexed
public:
BIT(int n) : bit_(n) {
}
void add(int i, int val) {
for (; i < bit_.size(); i += lower_bit(i)) {
bit_[i] += val;
}
}
int sum(int i) {
int sum = 0;
for (; i > 0; i -= lower_bit(i)) {
sum += bit_[i];
}
return sum;
}
inline int lower_bit(int i) {
return i & -i;
}
private:
vector<int> bit_;
};
class Solution {
public:
vector<int> processQueries(vector<int>& queries, int m) {
BIT bit(2 * m + 1);
unordered_map<int, int> lookup;
for (int i = 1; i <= m; ++i) {
lookup[i] = m + i;
bit.add(m + i, 1);
}
vector<int> result;
int curr = m;
for (const auto& q : queries) {
auto i = lookup[q]; lookup.erase(q);
result.emplace_back(bit.sum(i - 1));
bit.add(i, -1);
lookup[q] = curr;
bit.add(curr--, 1);
}
return result;
}
};