#include <bits/stdc++.h> using namespace std; const int B = 1 << 19; const int N = 300001; const long long MOD = 1e9 + 7; long long n, a, d[2 * B + 1][2]; long long preft[N]; map<long long, int> pref_to_ind; vector<long long>v; void add(int a, long long k, int p) { a += B; while (a > 1) { d[a][p] += k; d[a][p] %= MOD; a >>= 1; } } long long leq(int a, int p) { a += B; long long sum = d[a][p]; while (a > 1) { if (a % 2) { sum += d[a - 1][p]; sum %= MOD; } a >>= 1; } return sum; } long long gr(int a, int p) { a += B; long long sum = 0; while (a > 1) { if (a % 2 == 0) { sum += d[a + 1][p]; sum %= MOD; } a >>= 1; } return sum; } int main() { ios_base::sync_with_stdio(0); cin >> n; add(0, 1, 0); v.push_back(0); long long pref = 0; long long m = 0; long long res = 0; for (int i = 1; i <= n; i++) { cin >> a; pref += a; pref %= MOD; preft[i] = pref; v.push_back(pref); } sort(v.begin(), v.end()); int ind = 0; pref_to_ind[v[0]] = 0; for (int i = 1; i < v.size(); i++) { if (v[i] != v[i - 1]) { ind++; pref_to_ind[v[i]] = ind; } } for (int i = 1; i <= n; i++) { int p = preft[i] % 2;; int pref_ind = pref_to_ind[preft[i]]; res = (leq(pref_ind, p) + gr(pref_ind, (p + 1) % 2)) % MOD; add(pref_ind, res, p); } cout << res << endl; return 0; }
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 83 84 85 | #include <bits/stdc++.h> using namespace std; const int B = 1 << 19; const int N = 300001; const long long MOD = 1e9 + 7; long long n, a, d[2 * B + 1][2]; long long preft[N]; map<long long, int> pref_to_ind; vector<long long>v; void add(int a, long long k, int p) { a += B; while (a > 1) { d[a][p] += k; d[a][p] %= MOD; a >>= 1; } } long long leq(int a, int p) { a += B; long long sum = d[a][p]; while (a > 1) { if (a % 2) { sum += d[a - 1][p]; sum %= MOD; } a >>= 1; } return sum; } long long gr(int a, int p) { a += B; long long sum = 0; while (a > 1) { if (a % 2 == 0) { sum += d[a + 1][p]; sum %= MOD; } a >>= 1; } return sum; } int main() { ios_base::sync_with_stdio(0); cin >> n; add(0, 1, 0); v.push_back(0); long long pref = 0; long long m = 0; long long res = 0; for (int i = 1; i <= n; i++) { cin >> a; pref += a; pref %= MOD; preft[i] = pref; v.push_back(pref); } sort(v.begin(), v.end()); int ind = 0; pref_to_ind[v[0]] = 0; for (int i = 1; i < v.size(); i++) { if (v[i] != v[i - 1]) { ind++; pref_to_ind[v[i]] = ind; } } for (int i = 1; i <= n; i++) { int p = preft[i] % 2;; int pref_ind = pref_to_ind[preft[i]]; res = (leq(pref_ind, p) + gr(pref_ind, (p + 1) % 2)) % MOD; add(pref_ind, res, p); } cout << res << endl; return 0; } |