-
Notifications
You must be signed in to change notification settings - Fork 481
/
1409.cpp
40 lines (36 loc) · 926 Bytes
/
1409.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
class Solution {
public:
vector<int> processQueries(vector<int>& queries, int m) {
int n = queries.size();
N = m + n + 1;
int pos[m + 1];
int tree[N];
memset(tree, 0, sizeof tree);
for (int i = 1; i <= m; i++) {
pos[i] = n + i;
update(tree, i + n, 1);
}
vector<int> res;
for (int i : queries) {
res.emplace_back(prefixSum(tree, pos[i] - 1));
update(tree, pos[i], -1);
update(tree, n, 1);
pos[i] = n--;
}
return res;
}
private:
int N;
void update(int tr[], int x, int v) {
for (int i = x; i < N; i += i & -i) {
tr[i] += v;
}
}
int prefixSum(int tr[], int x) {
int res = 0;
for (int i = x; i; i -= i & -i) {
res += tr[i];
}
return res;
}
};