#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10; const long long mod = 1e9 + 7; int n; int tab[N]; long long pref[N]; long long d[2][N]; // 0*mod + p, 0*mod + np, 1*mod + p, 1*mod + np bool cmp(int a, int b) { return pref[a] < pref[b]; } void update(long long *fen, int x, long long ile) { if (x > n + 3) { return; } // cout << "halo " << x << " " << x + (x & (-x)) << " " << fen[x] << " " << ile << endl; fen[x] = (fen[x] + ile) % mod; update(fen, x + (x & (-x)), ile); } long long sum(long long *fen, int x) { // cout << "siema " << x << " " << fen[x] << endl; if (x == 0) { return 0; } return (fen[x] + sum(fen, x - (x & (-x)))) % mod; } long long get_sum(long long *fen, int a, int b) { // cout << "do widzenia" << endl; return (sum(fen, b) - sum(fen, a - 1) + mod) % mod; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &tab[i]); } for (int i = 1; i <= n; i++) { pref[i] = (pref[i - 1] + tab[i]) % mod; } vector<int> order; for (int i = 0; i <= n; i++) { order.push_back(i); } sort(order.begin(), order.end(), cmp); int last = -1; int curr = -1; for (auto v : order) { if (pref[v] != last) { last = pref[v]; curr++; if (curr % 2 != pref[v] % 2) { curr++; } } pref[v] = curr; } update(d[0], 1, 1); long long res; for (int i = 1; i <= n; i++) { int par = pref[i] % 2; // printf("get_sum(%d, %d) -> %d", 1, (pref[i] + 2) / 2, par); res = (get_sum(d[par], 1, (pref[i] + 2) / 2) + get_sum(d[par ^ 1], (pref[i] + 2) / 2 + (pref[i] & 1), n + 3)) % mod; update(d[par], (pref[i] + 2) / 2, res); // res += get_sum(d[half ^ 1][par], (rest + 2) / 2 + 1, n) + get_sum(d[half ^ 1][par ^ 1], 0, ) } printf("%lld\n", res); // for (int i = 0; i <= n; i++) { // printf("%lld ", pref[i]); // } // printf("\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 | #include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10; const long long mod = 1e9 + 7; int n; int tab[N]; long long pref[N]; long long d[2][N]; // 0*mod + p, 0*mod + np, 1*mod + p, 1*mod + np bool cmp(int a, int b) { return pref[a] < pref[b]; } void update(long long *fen, int x, long long ile) { if (x > n + 3) { return; } // cout << "halo " << x << " " << x + (x & (-x)) << " " << fen[x] << " " << ile << endl; fen[x] = (fen[x] + ile) % mod; update(fen, x + (x & (-x)), ile); } long long sum(long long *fen, int x) { // cout << "siema " << x << " " << fen[x] << endl; if (x == 0) { return 0; } return (fen[x] + sum(fen, x - (x & (-x)))) % mod; } long long get_sum(long long *fen, int a, int b) { // cout << "do widzenia" << endl; return (sum(fen, b) - sum(fen, a - 1) + mod) % mod; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &tab[i]); } for (int i = 1; i <= n; i++) { pref[i] = (pref[i - 1] + tab[i]) % mod; } vector<int> order; for (int i = 0; i <= n; i++) { order.push_back(i); } sort(order.begin(), order.end(), cmp); int last = -1; int curr = -1; for (auto v : order) { if (pref[v] != last) { last = pref[v]; curr++; if (curr % 2 != pref[v] % 2) { curr++; } } pref[v] = curr; } update(d[0], 1, 1); long long res; for (int i = 1; i <= n; i++) { int par = pref[i] % 2; // printf("get_sum(%d, %d) -> %d", 1, (pref[i] + 2) / 2, par); res = (get_sum(d[par], 1, (pref[i] + 2) / 2) + get_sum(d[par ^ 1], (pref[i] + 2) / 2 + (pref[i] & 1), n + 3)) % mod; update(d[par], (pref[i] + 2) / 2, res); // res += get_sum(d[half ^ 1][par], (rest + 2) / 2 + 1, n) + get_sum(d[half ^ 1][par ^ 1], 0, ) } printf("%lld\n", res); // for (int i = 0; i <= n; i++) { // printf("%lld ", pref[i]); // } // printf("\n"); } |