#include <iostream> #include <vector> #include <algorithm> typedef long long ll; using namespace std; ll mod = 1000000007; int S = 1; vector<ll> tree[2]; void add(int p, ll val, int nr) { p += S; while (p) { tree[nr][p] = (tree[nr][p] + val)%mod; p>>=1; } } ll sum(int p, int q, int nr) { p += S, q += S+1; ll r = 0; while (p < q) { if (p&1) { r = (r+tree[nr][p])%mod; p++; } if (q&1) { q--; r = (r+tree[nr][q])%mod; } p>>=1, q>>=1; } return r; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<ll> a(n+1), pref(n+1); for (int i = 1; i <= n; i++) { cin >> a[i]; pref[i] = (pref[i-1]+a[i])%mod; } sort(pref.begin(), pref.end()); while (S < n+1) S<<=1; tree[0] = tree[1] = vector<ll>(2*S); ll ans = 0, asum = 0; add(0, 1, 0); for (int i = 1; i <= n; i++) { asum = (asum + a[i])%mod; int x = (lower_bound(pref.begin(), pref.end(), asum) - pref.begin()); ans = (sum(0, x, asum%2) + sum(x+1, n, (asum%2)^1))%mod; add(x, ans, asum%2); } cout << ans; }
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 <iostream> #include <vector> #include <algorithm> typedef long long ll; using namespace std; ll mod = 1000000007; int S = 1; vector<ll> tree[2]; void add(int p, ll val, int nr) { p += S; while (p) { tree[nr][p] = (tree[nr][p] + val)%mod; p>>=1; } } ll sum(int p, int q, int nr) { p += S, q += S+1; ll r = 0; while (p < q) { if (p&1) { r = (r+tree[nr][p])%mod; p++; } if (q&1) { q--; r = (r+tree[nr][q])%mod; } p>>=1, q>>=1; } return r; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<ll> a(n+1), pref(n+1); for (int i = 1; i <= n; i++) { cin >> a[i]; pref[i] = (pref[i-1]+a[i])%mod; } sort(pref.begin(), pref.end()); while (S < n+1) S<<=1; tree[0] = tree[1] = vector<ll>(2*S); ll ans = 0, asum = 0; add(0, 1, 0); for (int i = 1; i <= n; i++) { asum = (asum + a[i])%mod; int x = (lower_bound(pref.begin(), pref.end(), asum) - pref.begin()); ans = (sum(0, x, asum%2) + sum(x+1, n, (asum%2)^1))%mod; add(x, ans, asum%2); } cout << ans; } |