#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; const int M = 1000000007; const int MM = 2*M; const int BT = 8; struct SparseImmutablePrefixSum { struct Milestone{ int idx; ll sum; bool operator<(const Milestone& ms) const { return idx < ms.idx; } bool operator==(const Milestone& ms) const { return idx == ms.idx; } }; vector<Milestone>MS; void range(int a, int b, ll val) { MS.push_back(Milestone{.idx=a, .sum=val}); MS.push_back(Milestone{.idx=b+1, .sum=-val}); } void build() { MS.push_back(Milestone{.idx=0, .sum=0}); sort(MS.begin(), MS.end()); for(int i=1,first_of_kind=0;i<(int)MS.size();i++) if (MS[i].idx != MS[i-1].idx) { MS[i].sum += MS[first_of_kind].sum; first_of_kind = i; } else MS[first_of_kind].sum += MS[i].sum; MS.erase(unique(MS.begin(), MS.end()), MS.end()); } ll get(int i) { return prev(upper_bound(MS.begin(), MS.end(), Milestone{.idx=i}))->sum; } }; struct BlackBox{ SparseImmutablePrefixSum even, odd; SparseImmutablePrefixSum& get_ps(ll val) { return val%2==0 ? even : odd; } void add_number(ll dp_val, int x) { //x = [0, MM) if (x<M) { get_ps(x).range(0, M-x-1, dp_val); get_ps(x+1).range(M-x, MM-x-1, dp_val); get_ps(x).range(MM-x, MM-1, dp_val); } else { get_ps(x+1).range(0, MM-x-1, dp_val); get_ps(x).range(MM-x, MM-x+M-1, dp_val); get_ps(x+1).range(MM-x+M, MM-1, dp_val); } } void build() { even.build(); odd.build(); } ll get(int x) { return get_ps(x).get(x); } }; struct Block{ int right; int size; BlackBox *bb; }; BlackBox* make_black_box(const vector<int>& seq, const vector<ll>& dp, int from, int to) { BlackBox *bb = new BlackBox; long long acc = 0; for (int i=to; i >= from; i--) { acc = (acc + seq[i]) % MM; bb->add_number(dp[i-1], (int)acc); } bb->build(); return bb; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int>Seq(n+1); for (int i=1;i<=n;i++) cin >> Seq[i]; vector<ll>Pref(n+1); for(int i=1;i<=n;i++) Pref[i] = Pref[i-1] + Seq[i]; vector<ll>dp(n+1); dp[0] = 1; vector<Block>B; int last_block_end = 0; for (int i=1;i<=n;i++) { //get dp value ll acc = 0; for (int j=i;j>last_block_end;j--) { acc = (acc + Seq[j]) % MM; if ((acc%M)%2 == 0) dp[i] += dp[j-1]; } // cerr << "B = " << B.size() << endl; if (!B.empty()) for (int bi=B.size()-1;bi>=0;bi--) { dp[i] += B[bi].bb->get(acc); if(bi>0){ acc += Pref[B[bi].right] - Pref[B[bi-1].right]; acc %= MM; } } // cerr << "XX" << endl; dp[i] %= M; //update structure if (i-last_block_end < BT) continue; int bs = BT; while (!B.empty() && B.back().size==bs) { bs *= 2; delete B.back().bb; B.pop_back(); } B.push_back(Block{.right=i, .size=bs, .bb=make_black_box(Seq, dp, i-bs+1, i)}); last_block_end = i; } cout << dp[n] << "\n"; for (Block b : B) delete b.bb; 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; const int M = 1000000007; const int MM = 2*M; const int BT = 8; struct SparseImmutablePrefixSum { struct Milestone{ int idx; ll sum; bool operator<(const Milestone& ms) const { return idx < ms.idx; } bool operator==(const Milestone& ms) const { return idx == ms.idx; } }; vector<Milestone>MS; void range(int a, int b, ll val) { MS.push_back(Milestone{.idx=a, .sum=val}); MS.push_back(Milestone{.idx=b+1, .sum=-val}); } void build() { MS.push_back(Milestone{.idx=0, .sum=0}); sort(MS.begin(), MS.end()); for(int i=1,first_of_kind=0;i<(int)MS.size();i++) if (MS[i].idx != MS[i-1].idx) { MS[i].sum += MS[first_of_kind].sum; first_of_kind = i; } else MS[first_of_kind].sum += MS[i].sum; MS.erase(unique(MS.begin(), MS.end()), MS.end()); } ll get(int i) { return prev(upper_bound(MS.begin(), MS.end(), Milestone{.idx=i}))->sum; } }; struct BlackBox{ SparseImmutablePrefixSum even, odd; SparseImmutablePrefixSum& get_ps(ll val) { return val%2==0 ? even : odd; } void add_number(ll dp_val, int x) { //x = [0, MM) if (x<M) { get_ps(x).range(0, M-x-1, dp_val); get_ps(x+1).range(M-x, MM-x-1, dp_val); get_ps(x).range(MM-x, MM-1, dp_val); } else { get_ps(x+1).range(0, MM-x-1, dp_val); get_ps(x).range(MM-x, MM-x+M-1, dp_val); get_ps(x+1).range(MM-x+M, MM-1, dp_val); } } void build() { even.build(); odd.build(); } ll get(int x) { return get_ps(x).get(x); } }; struct Block{ int right; int size; BlackBox *bb; }; BlackBox* make_black_box(const vector<int>& seq, const vector<ll>& dp, int from, int to) { BlackBox *bb = new BlackBox; long long acc = 0; for (int i=to; i >= from; i--) { acc = (acc + seq[i]) % MM; bb->add_number(dp[i-1], (int)acc); } bb->build(); return bb; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int>Seq(n+1); for (int i=1;i<=n;i++) cin >> Seq[i]; vector<ll>Pref(n+1); for(int i=1;i<=n;i++) Pref[i] = Pref[i-1] + Seq[i]; vector<ll>dp(n+1); dp[0] = 1; vector<Block>B; int last_block_end = 0; for (int i=1;i<=n;i++) { //get dp value ll acc = 0; for (int j=i;j>last_block_end;j--) { acc = (acc + Seq[j]) % MM; if ((acc%M)%2 == 0) dp[i] += dp[j-1]; } // cerr << "B = " << B.size() << endl; if (!B.empty()) for (int bi=B.size()-1;bi>=0;bi--) { dp[i] += B[bi].bb->get(acc); if(bi>0){ acc += Pref[B[bi].right] - Pref[B[bi-1].right]; acc %= MM; } } // cerr << "XX" << endl; dp[i] %= M; //update structure if (i-last_block_end < BT) continue; int bs = BT; while (!B.empty() && B.back().size==bs) { bs *= 2; delete B.back().bb; B.pop_back(); } B.push_back(Block{.right=i, .size=bs, .bb=make_black_box(Seq, dp, i-bs+1, i)}); last_block_end = i; } cout << dp[n] << "\n"; for (Block b : B) delete b.bb; return 0; } |