#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1e9+7; const int MAX_N = 300007; int n, curIter; ll t[MAX_N]; ll pref[MAX_N], prefToSort[MAX_N]; unordered_map <ll, int> prefScale; const int powlee = 1024 * 512 - 1; ll evenTree[1024 * 1024 + 666]; ll oddTree[1024 * 1024 + 666]; void insertEvenTree(int x, ll val) { x += powlee; while (x >= 1) { evenTree[x] += val; evenTree[x] %= MOD; x /= 2; } } void insertOddTree(int x, ll val) { x += powlee; while (x >= 1) { oddTree[x] += val; oddTree[x] %= MOD; x /= 2; } } ll getEvenTree(int b, int e) { ll resVal = 0; b += powlee; b--; e += powlee; e++; while (b + 1 < e) { if(b % 2 == 0) { resVal += evenTree[b + 1]; } if(e % 2 == 1) { resVal += evenTree[e - 1]; } resVal %= MOD; b /= 2; e /= 2; } return resVal; } ll getOddTree(int b, int e) { ll resVal = 0; b += powlee; b--; e += powlee; e++; while (b + 1 < e) { if(b % 2 == 0) { resVal += oddTree[b + 1]; } if(e % 2 == 1) { resVal += oddTree[e - 1]; } resVal %= MOD; b /= 2; e /= 2; } return resVal; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; t[0] = -1; for (int i = 1; i <= n; i++) { cin >> t[i]; pref[i] = pref[i-1] + t[i]; pref[i] %= MOD; prefToSort[i] = pref[i]; } sort(prefToSort + 1, prefToSort + n + 1); prefScale[-1] = 0; curIter = 1; prefToSort[0] = -1; // guard for (int i = 1; i <= n; i++) { if (prefToSort[i] != prefToSort[i-1]) { prefScale[prefToSort[i]] = curIter; curIter++; } } prefScale[MOD] = curIter; insertEvenTree(0, 1); for (int i = 1; i < n; i++) { int scale = prefScale[pref[i]]; ll resVal = 0; if (pref[i] % 2 == 0) { resVal += getEvenTree(0, scale); resVal += getOddTree(scale, curIter); resVal %= MOD; //cout << pref[i] << " isEven: " << resVal << "\n"; insertEvenTree(scale, resVal); } else { resVal += getOddTree(0, scale); resVal += getEvenTree(scale, curIter); resVal %= MOD; //cout << pref[i] << " isOdd: " << resVal << "\n"; insertOddTree(scale, resVal); } } int scale = prefScale[pref[n]]; ll resVal = 0; if (pref[n] % 2 == 0) { resVal += getEvenTree(0, scale); resVal += getOddTree(scale, curIter); resVal %= MOD; //cout << pref[n] << " isEven: " << resVal << "\n"; } else { resVal += getOddTree(0, scale); resVal += getEvenTree(scale, curIter); resVal %= MOD; //cout << pref[n] << " isOdd: " << resVal << "\n"; } cout << resVal << "\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 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 132 133 134 135 136 | #include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1e9+7; const int MAX_N = 300007; int n, curIter; ll t[MAX_N]; ll pref[MAX_N], prefToSort[MAX_N]; unordered_map <ll, int> prefScale; const int powlee = 1024 * 512 - 1; ll evenTree[1024 * 1024 + 666]; ll oddTree[1024 * 1024 + 666]; void insertEvenTree(int x, ll val) { x += powlee; while (x >= 1) { evenTree[x] += val; evenTree[x] %= MOD; x /= 2; } } void insertOddTree(int x, ll val) { x += powlee; while (x >= 1) { oddTree[x] += val; oddTree[x] %= MOD; x /= 2; } } ll getEvenTree(int b, int e) { ll resVal = 0; b += powlee; b--; e += powlee; e++; while (b + 1 < e) { if(b % 2 == 0) { resVal += evenTree[b + 1]; } if(e % 2 == 1) { resVal += evenTree[e - 1]; } resVal %= MOD; b /= 2; e /= 2; } return resVal; } ll getOddTree(int b, int e) { ll resVal = 0; b += powlee; b--; e += powlee; e++; while (b + 1 < e) { if(b % 2 == 0) { resVal += oddTree[b + 1]; } if(e % 2 == 1) { resVal += oddTree[e - 1]; } resVal %= MOD; b /= 2; e /= 2; } return resVal; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; t[0] = -1; for (int i = 1; i <= n; i++) { cin >> t[i]; pref[i] = pref[i-1] + t[i]; pref[i] %= MOD; prefToSort[i] = pref[i]; } sort(prefToSort + 1, prefToSort + n + 1); prefScale[-1] = 0; curIter = 1; prefToSort[0] = -1; // guard for (int i = 1; i <= n; i++) { if (prefToSort[i] != prefToSort[i-1]) { prefScale[prefToSort[i]] = curIter; curIter++; } } prefScale[MOD] = curIter; insertEvenTree(0, 1); for (int i = 1; i < n; i++) { int scale = prefScale[pref[i]]; ll resVal = 0; if (pref[i] % 2 == 0) { resVal += getEvenTree(0, scale); resVal += getOddTree(scale, curIter); resVal %= MOD; //cout << pref[i] << " isEven: " << resVal << "\n"; insertEvenTree(scale, resVal); } else { resVal += getOddTree(0, scale); resVal += getEvenTree(scale, curIter); resVal %= MOD; //cout << pref[i] << " isOdd: " << resVal << "\n"; insertOddTree(scale, resVal); } } int scale = prefScale[pref[n]]; ll resVal = 0; if (pref[n] % 2 == 0) { resVal += getEvenTree(0, scale); resVal += getOddTree(scale, curIter); resVal %= MOD; //cout << pref[n] << " isEven: " << resVal << "\n"; } else { resVal += getOddTree(0, scale); resVal += getEvenTree(scale, curIter); resVal %= MOD; //cout << pref[n] << " isOdd: " << resVal << "\n"; } cout << resVal << "\n"; } |