#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int query(const vector<int>& tree, int l, int r) {
assert(l <= r);
l += tree.size() / 2;
r += tree.size() / 2;
int result = tree[l];
if (l != r)
result = (result + tree[r]) % MOD;
while (l / 2 != r / 2) {
if (l % 2 == 0) result = (result + tree[l + 1]) % MOD;
if (r % 2 == 1) result = (result + tree[r - 1]) % MOD;
l /= 2;
r /= 2;
}
return result;
}
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
for (int i=0; i<n; ++i)
cin >> a[i];
vector<int> p(n + 1);
p[0] = 0;
for (int i = 0; i < n; ++i)
p[i + 1] = (p[i] + a[i]) % MOD;
vector<int> v = p;
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int T = 1;
while (T < (v.size() + 1))
T *= 2;
vector<int> tree[2];
tree[0].resize(2 * T);
tree[1].resize(2 * T);
vector<int> dp(n + 1);
for (int i = 0; i <= n; ++i) {
int ind = lower_bound(v.begin(), v.end(), p[i]) - v.begin();
if (i == 0) {
dp[i] = 1;
} else {
dp[i] = (
query(tree[p[i] % 2], 0, ind) +
query(tree[(p[i] + 1) % 2], ind + 1, v.size())
) % MOD;
}
int it = T + ind;
while (it > 0) {
tree[p[i] % 2][it] = (tree[p[i] % 2][it] + dp[i]) % MOD;
it /= 2;
}
}
cout << dp[n] << endl;
}
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int query(const vector<int>& tree, int l, int r) { assert(l <= r); l += tree.size() / 2; r += tree.size() / 2; int result = tree[l]; if (l != r) result = (result + tree[r]) % MOD; while (l / 2 != r / 2) { if (l % 2 == 0) result = (result + tree[l + 1]) % MOD; if (r % 2 == 1) result = (result + tree[r - 1]) % MOD; l /= 2; r /= 2; } return result; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> a(n); for (int i=0; i<n; ++i) cin >> a[i]; vector<int> p(n + 1); p[0] = 0; for (int i = 0; i < n; ++i) p[i + 1] = (p[i] + a[i]) % MOD; vector<int> v = p; sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); int T = 1; while (T < (v.size() + 1)) T *= 2; vector<int> tree[2]; tree[0].resize(2 * T); tree[1].resize(2 * T); vector<int> dp(n + 1); for (int i = 0; i <= n; ++i) { int ind = lower_bound(v.begin(), v.end(), p[i]) - v.begin(); if (i == 0) { dp[i] = 1; } else { dp[i] = ( query(tree[p[i] % 2], 0, ind) + query(tree[(p[i] + 1) % 2], ind + 1, v.size()) ) % MOD; } int it = T + ind; while (it > 0) { tree[p[i] % 2][it] = (tree[p[i] % 2][it] + dp[i]) % MOD; it /= 2; } } cout << dp[n] << endl; } |
English