forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
43 lines (39 loc) · 877 Bytes
/
Solution.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
class BinaryIndexedTree {
private:
int n;
vector<int> c;
public:
BinaryIndexedTree(int n)
: n(n)
, c(n + 1, 0) {}
void update(int x, int v) {
for (; x <= n; x += x & -x) {
c[x] += v;
}
}
int query(int x) {
int s = 0;
for (; x > 0; x -= x & -x) {
s += c[x];
}
return s;
}
};
class Solution {
public:
int subarraysWithMoreZerosThanOnes(vector<int>& nums) {
int n = nums.size();
int base = n + 1;
BinaryIndexedTree tree(n + base);
tree.update(base, 1);
const int mod = 1e9 + 7;
int ans = 0, s = 0;
for (int x : nums) {
s += (x == 0) ? -1 : 1;
ans += tree.query(s - 1 + base);
ans %= mod;
tree.update(s + base, 1);
}
return ans;
}
};