-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path1395.cpp
36 lines (30 loc) · 956 Bytes
/
1395.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
const int N = 1e5;
class Solution {
public:
int numTeams(vector<int>& rating) {
if (rating.size() < 3) return 0;
int leftTree[N + 1] = {};
int rightTree[N + 1] = {};
for (int r : rating) update(rightTree, r, 1);
int res = 0;
for (int r : rating) {
update(rightTree, r, -1);
res += getPrefixSum(leftTree, r - 1) * getSuffixSum(rightTree, r + 1) +
getSuffixSum(leftTree, r + 1) * getPrefixSum(rightTree, r - 1);
update(leftTree, r, 1);
}
return res;
}
private:
void update(int tr[], int x, int v) {
for (int i = x; i <= N; i += i & -i) tr[i] += v;
}
int getPrefixSum(int tr[], int x) {
int res = 0;
for (int i = x; i > 0; i -= i & -i) res += tr[i];
return res;
}
int getSuffixSum(int tr[], int i) {
return getPrefixSum(tr, N) - getPrefixSum(tr, i - 1);
}
};