//Author: Piotr Zielinski #include<bits/stdc++.h> using namespace std; template<class C> C reversed(C c) {reverse(c.begin(),c.end()); return c;} #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<ll, bool> pll; const int M = 1 << 20; const ll MOD = 1e9 + 7; struct PlusTree { vector<ll> data; PlusTree() { data.resize(2 * M); } void update(int x, ll v) { x += M; data[x] = v % MOD; while(x != 1) { x >>= 1; data[x] = (data[2 * x] + data[2 * x + 1]) % MOD; } } ll query(int a, int b) { a += M; b += M; ll res = data[a]; if(a != b) res += data[b]; res %= MOD; while(a / 2 != b / 2) { if((a & 1) ^ 1) res += data[a + 1]; if(b & 1) res += data[b - 1]; res %= MOD; a >>= 1; b >>= 1; } return res; } }; struct Mop { ll rem; bool odd; int id; Mop() : rem(0), odd(false), id(0) {} bool operator<(const Mop& other) const { if(rem == other.rem) return id < other.id; return rem < other.rem; } }; int32_t main(){ ios::sync_with_stdio(false); int n; cin >> n; vector<ll> a(n); vector<Mop> sums(n + 1); for(int i = 0;i < n;++i) { cin >> a[i]; sums[i + 1].rem = sums[i].rem + a[i]; sums[i + 1].odd = sums[i].odd; sums[i + 1].id = i + 1; if(sums[i + 1].rem >= MOD) { sums[i + 1].rem -= MOD; sums[i + 1].odd = !sums[i + 1].odd; } } vector<Mop> sorted_sums = sums; sort(all(sorted_sums)); vector<int> pos(n + 1); for(int i = 0;i <= n;++i) { pos[sorted_sums[i].id] = i; } PlusTree treeEven, treeOdd; ll res1, res2; treeEven.update(0, 1); // debug(pos); for(int i = 0;i < n;++i) { if(sums[i + 1].rem & 1) { res1 = treeOdd.query(0, pos[sums[i + 1].id] - 1); res2 = treeEven.query(pos[sums[i + 1].id] + 1, n + 1); treeOdd.update(pos[sums[i + 1].id], res1 + res2); // debug("ODD", res1, res2); // res1 = treeOdd.query(0, pos[sums[i + 1].id] - 1); // res2 = treeEven.query(pos[sums[i + 1].id] + 1, n + 1); // treeEven.update(pos[sums[i + 1].id], res1 + res2); // debug(res1, res2); } else { res1 = treeEven.query(0, pos[sums[i + 1].id] - 1); res2 = treeOdd.query(pos[sums[i + 1].id] + 1, n + 1); treeEven.update(pos[sums[i + 1].id], res1 + res2); // debug("EVEN", res1, res2); // res1 = treeOdd.query(0, pos[sums[i + 1].id] - 1); // res2 = treeEven.query(pos[sums[i + 1].id] + 1, n + 1); // treeOdd.update(pos[sums[i + 1].id], res1 + res2); // debug(res1, res2); } } if(sums[n].rem & 1) cout << treeOdd.query(pos[sums[n].id], pos[sums[n].id]) << "\n"; else cout << treeEven.query(pos[sums[n].id], pos[sums[n].id]) << "\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 | //Author: Piotr Zielinski #include<bits/stdc++.h> using namespace std; template<class C> C reversed(C c) {reverse(c.begin(),c.end()); return c;} #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<ll, bool> pll; const int M = 1 << 20; const ll MOD = 1e9 + 7; struct PlusTree { vector<ll> data; PlusTree() { data.resize(2 * M); } void update(int x, ll v) { x += M; data[x] = v % MOD; while(x != 1) { x >>= 1; data[x] = (data[2 * x] + data[2 * x + 1]) % MOD; } } ll query(int a, int b) { a += M; b += M; ll res = data[a]; if(a != b) res += data[b]; res %= MOD; while(a / 2 != b / 2) { if((a & 1) ^ 1) res += data[a + 1]; if(b & 1) res += data[b - 1]; res %= MOD; a >>= 1; b >>= 1; } return res; } }; struct Mop { ll rem; bool odd; int id; Mop() : rem(0), odd(false), id(0) {} bool operator<(const Mop& other) const { if(rem == other.rem) return id < other.id; return rem < other.rem; } }; int32_t main(){ ios::sync_with_stdio(false); int n; cin >> n; vector<ll> a(n); vector<Mop> sums(n + 1); for(int i = 0;i < n;++i) { cin >> a[i]; sums[i + 1].rem = sums[i].rem + a[i]; sums[i + 1].odd = sums[i].odd; sums[i + 1].id = i + 1; if(sums[i + 1].rem >= MOD) { sums[i + 1].rem -= MOD; sums[i + 1].odd = !sums[i + 1].odd; } } vector<Mop> sorted_sums = sums; sort(all(sorted_sums)); vector<int> pos(n + 1); for(int i = 0;i <= n;++i) { pos[sorted_sums[i].id] = i; } PlusTree treeEven, treeOdd; ll res1, res2; treeEven.update(0, 1); // debug(pos); for(int i = 0;i < n;++i) { if(sums[i + 1].rem & 1) { res1 = treeOdd.query(0, pos[sums[i + 1].id] - 1); res2 = treeEven.query(pos[sums[i + 1].id] + 1, n + 1); treeOdd.update(pos[sums[i + 1].id], res1 + res2); // debug("ODD", res1, res2); // res1 = treeOdd.query(0, pos[sums[i + 1].id] - 1); // res2 = treeEven.query(pos[sums[i + 1].id] + 1, n + 1); // treeEven.update(pos[sums[i + 1].id], res1 + res2); // debug(res1, res2); } else { res1 = treeEven.query(0, pos[sums[i + 1].id] - 1); res2 = treeOdd.query(pos[sums[i + 1].id] + 1, n + 1); treeEven.update(pos[sums[i + 1].id], res1 + res2); // debug("EVEN", res1, res2); // res1 = treeOdd.query(0, pos[sums[i + 1].id] - 1); // res2 = treeEven.query(pos[sums[i + 1].id] + 1, n + 1); // treeOdd.update(pos[sums[i + 1].id], res1 + res2); // debug(res1, res2); } } if(sums[n].rem & 1) cout << treeOdd.query(pos[sums[n].id], pos[sums[n].id]) << "\n"; else cout << treeEven.query(pos[sums[n].id], pos[sums[n].id]) << "\n"; return 0; } |