#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template<typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template<typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 3e5 + 7; const int S = 500; const int M = 1e9 + 7; int n; LL A[N], dp[N]; struct block { LL offset; LL dp_sum[2]; vector<pair<LL, int>> v; }; inline LL addM(LL a, LL b) { return (a + b < M ? a + b : a + b - M); } inline LL subM(LL a, LL b) { return (a - b >= 0 ? a - b : a - b + M); } inline LL get_val(block *b, int i) { return b->offset + b->v[i].first; } pair<block*, block*> split(block *b, LL x) { int id = sz(b->v) - 1; while (id >= 0 && get_val(b, id) < x) id--; block *r = new block(); r->offset = b->offset; r->dp_sum[0] = r->dp_sum[1] = 0; for (int i = id + 1; i < sz(b->v); i++) { r->v.push_back(b->v[i]); bool bv = b->v[i].first % 2; r->dp_sum[bv] = addM(r->dp_sum[bv], dp[b->v[i].second]); b->dp_sum[bv] = subM(b->dp_sum[bv], dp[b->v[i].second]); } b->v.resize(id + 1); return {b, r}; } void merge(block *l, block *r) { for (int i = 0; i < sz(r->v); i++) { l->v.push_back({get_val(r, i) - l->offset, r->v[i].second}); bool b = (get_val(r, i) - l->offset) % 2; l->dp_sum[b] = addM(l->dp_sum[b], dp[l->v.back().second]); } delete r; } void recalc(vector<block*> &new_blocks, vector<block*> &blocks, int x, int i, int j) { auto [l, r] = split(blocks[j], M - x); r->offset += x; new_blocks.push_back(r); for (int k = j + 1; k < sz(blocks); k++) { while (k < sz(blocks) && sz(new_blocks.back()->v) + sz(blocks[k]->v) <= S) { blocks[k]->offset += x; merge(new_blocks.back(), blocks[k]); k++; } if (k < sz(blocks)) { blocks[k]->offset += x; new_blocks.push_back(blocks[k]); } } if (sz(new_blocks.back()->v) + 1 <= S) { new_blocks.back()->v.push_back({x - new_blocks.back()->offset, i - 1}); bool b = (x - new_blocks.back()->offset) % 2; new_blocks.back()->dp_sum[b] = addM(new_blocks.back()->dp_sum[b], dp[i - 1]); } else { block *bx = new block(); bx->offset = 0; bx->dp_sum[0] = bx->dp_sum[1] = 0; bx->v.push_back({x, i - 1}); bx->dp_sum[x % 2] = dp[i - 1]; new_blocks.push_back(bx); } for (int k = 0; k < j; k++) { while (k < j && sz(new_blocks.back()->v) + sz(blocks[k]->v) <= S) { blocks[k]->offset += x - M; merge(new_blocks.back(), blocks[k]); k++; } if (k < j) { blocks[k]->offset += x - M; new_blocks.push_back(blocks[k]); } } l->offset += x - M; if (sz(new_blocks.back()->v) + sz(l->v) <= S) merge(new_blocks.back(), l); else new_blocks.push_back(l); } int main() { _upgrade cin >> n; for (int i = 1; i <= n; i++) cin >> A[i]; vector<block*> blocks; dp[0] = 1; for (int i = 1; i <= n; i++) { LL x = A[i]; if (i == 1) { block *bx = new block(); bx->offset = 0; bx->dp_sum[0] = bx->dp_sum[1] = 0; bx->v.push_back({x, i - 1}); bx->dp_sum[x % 2] = dp[i - 1]; blocks.push_back(bx); for (auto &b: blocks) dp[i] = addM(dp[i], b->dp_sum[(bool)(b->offset % 2)]); continue; } vector<block*> new_blocks; for (int j = sz(blocks) - 1; j >= -1; j--) { if (j == -1) { recalc(new_blocks, blocks, x, i, 0); break; } LL mx = get_val(blocks[j], 0); if (M - x <= mx) { recalc(new_blocks, blocks, x, i, j); break; } } swap(blocks, new_blocks); for (auto &b: blocks) dp[i] = addM(dp[i], b->dp_sum[(bool)(b->offset % 2)]); } cout << dp[n] << "\n"; 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template<typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template<typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 3e5 + 7; const int S = 500; const int M = 1e9 + 7; int n; LL A[N], dp[N]; struct block { LL offset; LL dp_sum[2]; vector<pair<LL, int>> v; }; inline LL addM(LL a, LL b) { return (a + b < M ? a + b : a + b - M); } inline LL subM(LL a, LL b) { return (a - b >= 0 ? a - b : a - b + M); } inline LL get_val(block *b, int i) { return b->offset + b->v[i].first; } pair<block*, block*> split(block *b, LL x) { int id = sz(b->v) - 1; while (id >= 0 && get_val(b, id) < x) id--; block *r = new block(); r->offset = b->offset; r->dp_sum[0] = r->dp_sum[1] = 0; for (int i = id + 1; i < sz(b->v); i++) { r->v.push_back(b->v[i]); bool bv = b->v[i].first % 2; r->dp_sum[bv] = addM(r->dp_sum[bv], dp[b->v[i].second]); b->dp_sum[bv] = subM(b->dp_sum[bv], dp[b->v[i].second]); } b->v.resize(id + 1); return {b, r}; } void merge(block *l, block *r) { for (int i = 0; i < sz(r->v); i++) { l->v.push_back({get_val(r, i) - l->offset, r->v[i].second}); bool b = (get_val(r, i) - l->offset) % 2; l->dp_sum[b] = addM(l->dp_sum[b], dp[l->v.back().second]); } delete r; } void recalc(vector<block*> &new_blocks, vector<block*> &blocks, int x, int i, int j) { auto [l, r] = split(blocks[j], M - x); r->offset += x; new_blocks.push_back(r); for (int k = j + 1; k < sz(blocks); k++) { while (k < sz(blocks) && sz(new_blocks.back()->v) + sz(blocks[k]->v) <= S) { blocks[k]->offset += x; merge(new_blocks.back(), blocks[k]); k++; } if (k < sz(blocks)) { blocks[k]->offset += x; new_blocks.push_back(blocks[k]); } } if (sz(new_blocks.back()->v) + 1 <= S) { new_blocks.back()->v.push_back({x - new_blocks.back()->offset, i - 1}); bool b = (x - new_blocks.back()->offset) % 2; new_blocks.back()->dp_sum[b] = addM(new_blocks.back()->dp_sum[b], dp[i - 1]); } else { block *bx = new block(); bx->offset = 0; bx->dp_sum[0] = bx->dp_sum[1] = 0; bx->v.push_back({x, i - 1}); bx->dp_sum[x % 2] = dp[i - 1]; new_blocks.push_back(bx); } for (int k = 0; k < j; k++) { while (k < j && sz(new_blocks.back()->v) + sz(blocks[k]->v) <= S) { blocks[k]->offset += x - M; merge(new_blocks.back(), blocks[k]); k++; } if (k < j) { blocks[k]->offset += x - M; new_blocks.push_back(blocks[k]); } } l->offset += x - M; if (sz(new_blocks.back()->v) + sz(l->v) <= S) merge(new_blocks.back(), l); else new_blocks.push_back(l); } int main() { _upgrade cin >> n; for (int i = 1; i <= n; i++) cin >> A[i]; vector<block*> blocks; dp[0] = 1; for (int i = 1; i <= n; i++) { LL x = A[i]; if (i == 1) { block *bx = new block(); bx->offset = 0; bx->dp_sum[0] = bx->dp_sum[1] = 0; bx->v.push_back({x, i - 1}); bx->dp_sum[x % 2] = dp[i - 1]; blocks.push_back(bx); for (auto &b: blocks) dp[i] = addM(dp[i], b->dp_sum[(bool)(b->offset % 2)]); continue; } vector<block*> new_blocks; for (int j = sz(blocks) - 1; j >= -1; j--) { if (j == -1) { recalc(new_blocks, blocks, x, i, 0); break; } LL mx = get_val(blocks[j], 0); if (M - x <= mx) { recalc(new_blocks, blocks, x, i, j); break; } } swap(blocks, new_blocks); for (auto &b: blocks) dp[i] = addM(dp[i], b->dp_sum[(bool)(b->offset % 2)]); } cout << dp[n] << "\n"; return 0; } |