// _| _| // _| _| _| _| _|_| _|_|_| _|_|_| _| _| _|_| _|_| // _|_| _| _| _|_|_|_| _| _| _|_| _|_| _|_|_|_| _|_|_|_| // _| _| _| _| _| _| _| _|_| _| _| _| _| // _| _| _|_|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_| // _| _| // _|_| _| #include <iostream> #include <set> #include <vector> #include <cassert> using namespace std; // #define DEBUG #ifdef DEBUG #define dassert(x) assert(x); #define df(...) printf(__VA_ARGS__) #else #define dassert(x) #define df(...) #endif #define x first #define y second #define mp make_pair #define pb push_back #define ir(x, a, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define sz(x) (ll)x.size() #define foru(i, n) for (ll i = 0; (i) < (n); ++(i)) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; ll read() { ll n = 0; bool b = 0; char c = getchar(); for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-'); for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0'); if (b) return -n; return n; } const int N = 3e5+10, MOD = 1e9+7; int n; ll a[N]; ll stidx[N]; ll st[4*N]; ll query(ll node, ll lo, ll hi, ll qlo, ll qhi) { // if (node == 1) { // df("STARTING-----"); // } // df("%lld %lld %lld %lld %lld\n", node, lo, hi, qlo, qhi); if (qhi < qlo) return 0; if (lo == qlo && hi == qhi) return st[node]; ll mid = (lo + hi)/2; return (query(2*node, lo, mid, qlo, min(qhi, mid)) + query(2*node+1, mid+1, hi, max(qlo, mid+1), qhi)) % MOD; } void update(ll node, ll lo, ll hi, ll q, ll v) { // df("%lld %lld %lld %lld %lld\n", node, lo, hi, q, v); dassert(node < 4*N); if (lo == hi) { st[node] = v; return; } ll mid = (lo+hi)/2; if (q <= mid) { update(2*node, lo, mid, q, v); } else { update(2*node+1, mid+1, hi, q, v); } st[node] = (st[2*node] + st[2*node+1]) % MOD; } // bool cmp(pll a, pll b) { // if (a.x == b.x) { // return a.y < b.y; // } // if ((a.x & 1) == (b.x & 1)) { // return a.x < b.x; // } // return (a.x & 1) < (b.x & 1); // } int main() { df("debug mode\n"); #ifndef DEBUG ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif // int n = 10; // update(1, 0, n-1, 2, 3); // while (1) { // int a = read(), b = read(); // df("%lld\n", query(1, 0, n-1, a, b)); // } n = read(); foru (i, n) a[i] = read(); // set<pll, decltype(&cmp)> s(cmp); set<pll> odds, evens; ll total = 0; foru (i, n) { // df("%d\n", i); total = (total + a[i]) % MOD; // df("inserting (%lld, %lld) [total %lld]\n", (a[i]-total+MOD)%MOD, i, total); int v = (a[i]-total+MOD)%MOD; if (v%2 == 0) { evens.insert({v, i}); } else { odds.insert({v, i}); } } int idx = 0; for (auto& p : evens) { // df("%lld ", (p.x+total)%MOD); stidx[p.y] = idx; ++idx; } int mid = idx; for (auto& p : odds) { // df("%lld ", (p.x+total)%MOD); stidx[p.y] = idx; ++idx; } foru (i, n) { df("%lld: %lld\n", i, stidx[i]); } df("\n"); evens.clear(); odds.clear(); total = 0; ll last = 1; foru (i, n) { // df("\n\ngoing to a[%lld] = %lld\n", i, a[i]); // df("odds:"); // for (auto p : odds) { // df("(%lld, %lld) ", p.x, p.y); // } // df("\nevens:"); // for (auto p : evens) { // df("(%lld, %lld) ", p.x, p.y); // } // df("\n"); total = (total + a[i]) % MOD; // df("total: %lld\n", total); // search for int v = (a[i]-total+MOD) % MOD; if (v%2 == 0) { evens.insert({v, i}); } else { odds.insert({v, i}); } update(1, 0, n-1, stidx[i], last); ll s = 0; if (total & 1) { // df("total odd\n"); // x + total < MOD // x odd auto it = odds.lower_bound({MOD-total, 0}); if (it != odds.begin()) { --it; df("indexing with stidx[%lld] == %lld\n", it->y, stidx[it->y]); s = (s + query(1, 0, n-1, mid, stidx[it->y])) % MOD; } // x + total >= MOD // x even it = evens.lower_bound({MOD-total, 0}); if (it != evens.end()) { s = (s + query(1, 0, n-1, stidx[it->y], mid-1)) % MOD; } } else { // df("total even\n"); // x + total < MOD // x even auto it = evens.lower_bound({MOD-total, 0}); if (it != evens.begin()) { --it; s = (s + query(1, 0, n-1, 0, stidx[it->y])) % MOD; } // x + total >= MOD // x even it = odds.lower_bound({MOD-total, 0}); if (it != odds.end()) { s = (s + query(1, 0, n-1, stidx[it->y], n-1)) % MOD; } } last = s; // printf("%lld\n", last); // s.insert({(a[i]-total+MOD)%MOD, i}); } printf("%lld\n", last); foru (i, n) { df("%lld: %lld\n", i, query(1, 0, n-1, stidx[i], stidx[i])); } 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 203 204 205 | // _| _| // _| _| _| _| _|_| _|_|_| _|_|_| _| _| _|_| _|_| // _|_| _| _| _|_|_|_| _| _| _|_| _|_| _|_|_|_| _|_|_|_| // _| _| _| _| _| _| _| _|_| _| _| _| _| // _| _| _|_|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_| // _| _| // _|_| _| #include <iostream> #include <set> #include <vector> #include <cassert> using namespace std; // #define DEBUG #ifdef DEBUG #define dassert(x) assert(x); #define df(...) printf(__VA_ARGS__) #else #define dassert(x) #define df(...) #endif #define x first #define y second #define mp make_pair #define pb push_back #define ir(x, a, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define sz(x) (ll)x.size() #define foru(i, n) for (ll i = 0; (i) < (n); ++(i)) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; ll read() { ll n = 0; bool b = 0; char c = getchar(); for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-'); for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0'); if (b) return -n; return n; } const int N = 3e5+10, MOD = 1e9+7; int n; ll a[N]; ll stidx[N]; ll st[4*N]; ll query(ll node, ll lo, ll hi, ll qlo, ll qhi) { // if (node == 1) { // df("STARTING-----"); // } // df("%lld %lld %lld %lld %lld\n", node, lo, hi, qlo, qhi); if (qhi < qlo) return 0; if (lo == qlo && hi == qhi) return st[node]; ll mid = (lo + hi)/2; return (query(2*node, lo, mid, qlo, min(qhi, mid)) + query(2*node+1, mid+1, hi, max(qlo, mid+1), qhi)) % MOD; } void update(ll node, ll lo, ll hi, ll q, ll v) { // df("%lld %lld %lld %lld %lld\n", node, lo, hi, q, v); dassert(node < 4*N); if (lo == hi) { st[node] = v; return; } ll mid = (lo+hi)/2; if (q <= mid) { update(2*node, lo, mid, q, v); } else { update(2*node+1, mid+1, hi, q, v); } st[node] = (st[2*node] + st[2*node+1]) % MOD; } // bool cmp(pll a, pll b) { // if (a.x == b.x) { // return a.y < b.y; // } // if ((a.x & 1) == (b.x & 1)) { // return a.x < b.x; // } // return (a.x & 1) < (b.x & 1); // } int main() { df("debug mode\n"); #ifndef DEBUG ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif // int n = 10; // update(1, 0, n-1, 2, 3); // while (1) { // int a = read(), b = read(); // df("%lld\n", query(1, 0, n-1, a, b)); // } n = read(); foru (i, n) a[i] = read(); // set<pll, decltype(&cmp)> s(cmp); set<pll> odds, evens; ll total = 0; foru (i, n) { // df("%d\n", i); total = (total + a[i]) % MOD; // df("inserting (%lld, %lld) [total %lld]\n", (a[i]-total+MOD)%MOD, i, total); int v = (a[i]-total+MOD)%MOD; if (v%2 == 0) { evens.insert({v, i}); } else { odds.insert({v, i}); } } int idx = 0; for (auto& p : evens) { // df("%lld ", (p.x+total)%MOD); stidx[p.y] = idx; ++idx; } int mid = idx; for (auto& p : odds) { // df("%lld ", (p.x+total)%MOD); stidx[p.y] = idx; ++idx; } foru (i, n) { df("%lld: %lld\n", i, stidx[i]); } df("\n"); evens.clear(); odds.clear(); total = 0; ll last = 1; foru (i, n) { // df("\n\ngoing to a[%lld] = %lld\n", i, a[i]); // df("odds:"); // for (auto p : odds) { // df("(%lld, %lld) ", p.x, p.y); // } // df("\nevens:"); // for (auto p : evens) { // df("(%lld, %lld) ", p.x, p.y); // } // df("\n"); total = (total + a[i]) % MOD; // df("total: %lld\n", total); // search for int v = (a[i]-total+MOD) % MOD; if (v%2 == 0) { evens.insert({v, i}); } else { odds.insert({v, i}); } update(1, 0, n-1, stidx[i], last); ll s = 0; if (total & 1) { // df("total odd\n"); // x + total < MOD // x odd auto it = odds.lower_bound({MOD-total, 0}); if (it != odds.begin()) { --it; df("indexing with stidx[%lld] == %lld\n", it->y, stidx[it->y]); s = (s + query(1, 0, n-1, mid, stidx[it->y])) % MOD; } // x + total >= MOD // x even it = evens.lower_bound({MOD-total, 0}); if (it != evens.end()) { s = (s + query(1, 0, n-1, stidx[it->y], mid-1)) % MOD; } } else { // df("total even\n"); // x + total < MOD // x even auto it = evens.lower_bound({MOD-total, 0}); if (it != evens.begin()) { --it; s = (s + query(1, 0, n-1, 0, stidx[it->y])) % MOD; } // x + total >= MOD // x even it = odds.lower_bound({MOD-total, 0}); if (it != odds.end()) { s = (s + query(1, 0, n-1, stidx[it->y], n-1)) % MOD; } } last = s; // printf("%lld\n", last); // s.insert({(a[i]-total+MOD)%MOD, i}); } printf("%lld\n", last); foru (i, n) { df("%lld: %lld\n", i, query(1, 0, n-1, stidx[i], stidx[i])); } return 0; } |