#include <bits/stdc++.h>
using namespace std;
const int M = 1'000'000'007;
const int B = 1 << 19;
int n, t[300005];
int dp[300005];
int pref[300005];
map<int, int> numer;
int N;
int tree[2][2 * B + 5];
void dodaj(int pos, int x, int tr) {
pos += B;
while (pos > 0) {
tree[tr][pos] += x;
if (tree[tr][pos] >= M)
tree[tr][pos] -= M;
pos >>= 1;
}
}
int zlicz(int a, int b, int tr) {
int akt = B + b;
int wynik = 0;
while (akt > 1) {
if (akt % 2 == 0) wynik += tree[tr][akt + 1];
if (wynik >= M) wynik -= M;
akt >>= 1;
}
akt = B + a;
while (akt > 1) {
if (akt % 2 == 1) wynik += tree[tr][akt - 1];
if (wynik >= M) wynik -= M;
akt >>= 1;
}
wynik = tree[tr][1] - wynik;
if (wynik < 0) wynik += M;
return wynik;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin >> n;
numer[-1] = 1;
numer[0] = 1;
for (int i = 1; i <= n; i++) {
cin >> t[i];
pref[i] = pref[i - 1] + t[i];
if (pref[i] >= M) pref[i] -= M;
if (pref[i] == 0) {
numer[0] = 1;
} else {
numer[M - pref[i]] = 1;
}
}
for (auto &i : numer) {
i.second = ++N;
}
dodaj(2, 1, 0);
for (int i = 1; i <= n; i++) {
int x = numer[(M - pref[i]) % M];
if (pref[i] == 0) {
dp[i] += zlicz(1, N, 0);
} else {
dp[i] += zlicz(1, x - 1, pref[i] % 2);
dp[i] += zlicz(x, N, 1 - (pref[i] % 2));
}
if (dp[i] >= M) dp[i] -= M;
dodaj(x, dp[i], ((M - pref[i]) % M) % 2);
}
cout << dp[n] << "\n";
}
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #include <bits/stdc++.h> using namespace std; const int M = 1'000'000'007; const int B = 1 << 19; int n, t[300005]; int dp[300005]; int pref[300005]; map<int, int> numer; int N; int tree[2][2 * B + 5]; void dodaj(int pos, int x, int tr) { pos += B; while (pos > 0) { tree[tr][pos] += x; if (tree[tr][pos] >= M) tree[tr][pos] -= M; pos >>= 1; } } int zlicz(int a, int b, int tr) { int akt = B + b; int wynik = 0; while (akt > 1) { if (akt % 2 == 0) wynik += tree[tr][akt + 1]; if (wynik >= M) wynik -= M; akt >>= 1; } akt = B + a; while (akt > 1) { if (akt % 2 == 1) wynik += tree[tr][akt - 1]; if (wynik >= M) wynik -= M; akt >>= 1; } wynik = tree[tr][1] - wynik; if (wynik < 0) wynik += M; return wynik; } int32_t main() { ios_base::sync_with_stdio(0); cin >> n; numer[-1] = 1; numer[0] = 1; for (int i = 1; i <= n; i++) { cin >> t[i]; pref[i] = pref[i - 1] + t[i]; if (pref[i] >= M) pref[i] -= M; if (pref[i] == 0) { numer[0] = 1; } else { numer[M - pref[i]] = 1; } } for (auto &i : numer) { i.second = ++N; } dodaj(2, 1, 0); for (int i = 1; i <= n; i++) { int x = numer[(M - pref[i]) % M]; if (pref[i] == 0) { dp[i] += zlicz(1, N, 0); } else { dp[i] += zlicz(1, x - 1, pref[i] % 2); dp[i] += zlicz(x, N, 1 - (pref[i] % 2)); } if (dp[i] >= M) dp[i] -= M; dodaj(x, dp[i], ((M - pref[i]) % M) % 2); } cout << dp[n] << "\n"; } |
English