#include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; typedef long long ll; const ll MOD = 1000000007; vector< map<ll, ll> > M(2); vector<ll> T0, T1; ll l, Size; void Mapuj(vector< vector<ll> >& a) { for (int i = 0; i < 2; ++i) { sort(a[i].begin(), a[i].end()); ll akt = 0; for (int j = 0; j < a[i].size(); ++j) { if (j == 0 || a[i][j] != a[i][j - 1]) { M[i][a[i][j]] = akt; ++akt; } } } } void Stworz(vector<ll>& T, int n) { int p = 1; while (p < n) p *= 2; l = p; Size = 2 * p - 1; for (int i = 0; i <= Size; ++i) T.push_back(0); } void Dodaj(vector<ll>& T, int a, ll x) { a += l; while (a != 0) { T[a] = (T[a] + x) % MOD; a /= 2; } } ll Sum(vector<ll>& T, int a, int b) { a += l; b += l; ll s = T[a]; if (a != b) s = (s + T[b]) % MOD; while (a / 2 != b / 2) { if (a % 2 == 0) s = (s + T[a + 1]) % MOD; if (b % 2 == 1) s = (s + T[b - 1]) % MOD; a /= 2; b /= 2; } return s; } int Szukaj1(vector<ll>& a, ll x) { int l = 0, p = a.size() - 1, s, r = -1; while (l <= p) { s = (l + p) / 2; if (a[s] >= x) { r = s; p = s - 1; } else l = s + 1; } return r; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<ll> a(n); ll pref = 0; vector< vector<ll> > resz(2); for (int i = 0; i < n; ++i) { cin >> a[i]; pref = (pref + a[i]) % MOD; resz[pref % 2].push_back(pref); } Mapuj(resz); Stworz(T0, 300000); Stworz(T1, 300000); vector<ll> dp(n); pref = 0; for (int i = 0; i < n; ++i) { pref = (pref + a[i]) % MOD; if (pref % 2 == 0) { int gdzie = Szukaj1(resz[1], pref + 1); dp[i] = (Sum(T0, 0, M[0][pref]) + 1) % MOD; if (gdzie != -1) dp[i] = (dp[i] + Sum(T1, M[1][resz[1][gdzie]], Size - l)) % MOD; Dodaj(T0, M[0][pref], dp[i]); } else { int gdzie = Szukaj1(resz[0], pref + 1); dp[i] = Sum(T1, 0, M[1][pref]); if (gdzie != -1) dp[i] = (dp[i] + Sum(T0, M[0][resz[0][gdzie]], Size - l)) % MOD; Dodaj(T1, M[1][pref], dp[i]); } } cout << dp[n - 1]; 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; typedef long long ll; const ll MOD = 1000000007; vector< map<ll, ll> > M(2); vector<ll> T0, T1; ll l, Size; void Mapuj(vector< vector<ll> >& a) { for (int i = 0; i < 2; ++i) { sort(a[i].begin(), a[i].end()); ll akt = 0; for (int j = 0; j < a[i].size(); ++j) { if (j == 0 || a[i][j] != a[i][j - 1]) { M[i][a[i][j]] = akt; ++akt; } } } } void Stworz(vector<ll>& T, int n) { int p = 1; while (p < n) p *= 2; l = p; Size = 2 * p - 1; for (int i = 0; i <= Size; ++i) T.push_back(0); } void Dodaj(vector<ll>& T, int a, ll x) { a += l; while (a != 0) { T[a] = (T[a] + x) % MOD; a /= 2; } } ll Sum(vector<ll>& T, int a, int b) { a += l; b += l; ll s = T[a]; if (a != b) s = (s + T[b]) % MOD; while (a / 2 != b / 2) { if (a % 2 == 0) s = (s + T[a + 1]) % MOD; if (b % 2 == 1) s = (s + T[b - 1]) % MOD; a /= 2; b /= 2; } return s; } int Szukaj1(vector<ll>& a, ll x) { int l = 0, p = a.size() - 1, s, r = -1; while (l <= p) { s = (l + p) / 2; if (a[s] >= x) { r = s; p = s - 1; } else l = s + 1; } return r; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<ll> a(n); ll pref = 0; vector< vector<ll> > resz(2); for (int i = 0; i < n; ++i) { cin >> a[i]; pref = (pref + a[i]) % MOD; resz[pref % 2].push_back(pref); } Mapuj(resz); Stworz(T0, 300000); Stworz(T1, 300000); vector<ll> dp(n); pref = 0; for (int i = 0; i < n; ++i) { pref = (pref + a[i]) % MOD; if (pref % 2 == 0) { int gdzie = Szukaj1(resz[1], pref + 1); dp[i] = (Sum(T0, 0, M[0][pref]) + 1) % MOD; if (gdzie != -1) dp[i] = (dp[i] + Sum(T1, M[1][resz[1][gdzie]], Size - l)) % MOD; Dodaj(T0, M[0][pref], dp[i]); } else { int gdzie = Szukaj1(resz[0], pref + 1); dp[i] = Sum(T1, 0, M[1][pref]); if (gdzie != -1) dp[i] = (dp[i] + Sum(T0, M[0][resz[0][gdzie]], Size - l)) % MOD; Dodaj(T1, M[1][pref], dp[i]); } } cout << dp[n - 1]; return 0; } |