-
Notifications
You must be signed in to change notification settings - Fork 481
/
1395.js
32 lines (25 loc) · 889 Bytes
/
1395.js
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
var numTeams = function(rating) {
if (rating.length < 3) return 0;
let update = function(tr, x, v) {
for (let i = x; i <= N; i += i & -i) tr[i] += v;
}
let getPrefixSum = function(tr, x) {
let res = 0;
for (let i = x; i > 0; i -= i & -i) res += tr[i];
return res;
}
let getSuffixSum = function(tr, i) {
return getPrefixSum(tr, N) - getPrefixSum(tr, i - 1);
}
let N = 100000;
let leftTree = new Array(N + 1).fill(0), rightTree = new Array(N + 1).fill(0);
for (let r of rating) update(rightTree, r, 1);
let res = 0;
for (let r of 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;
};