// Daniel Grzegorzewski // while (clock()<=69*CLOCKS_PER_SEC) #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #pragma GCC optimize("Ofast") // #pragma GCC target("avx,avx2,fma") #define MP make_pair #define PB push_back #define ST first #define ND second using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef long long LL; void init_ios() { ios_base::sync_with_stdio(0); cin.tie(0); } auto random_address = [] { char *p = new char; delete p; return uint64_t(p); }; const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1); mt19937_64 rng(SEED); const int N = 3*(int)1e5 + 3; const int M = 64*N; const int MOD = (int)1e9 + 7; const int MOD2 = 2*MOD; int n, nxt = 1, a[N], res[N]; struct Node { int l, r, ln, rn; PII sum; } node[M]; void crl(Node& rt) { if (rt.ln == 0) { rt.ln = nxt; node[nxt].l = rt.l; node[nxt].r = (1LL*rt.l+rt.r)/2; node[nxt].ln = node[nxt].rn = 0; node[nxt].sum = {0, 0}; ++nxt; } } void crr(Node& rt) { if (rt.rn == 0) { rt.rn = nxt; node[nxt].l = (1LL*rt.l+rt.r)/2+1; node[nxt].r = rt.r; node[nxt].ln = node[nxt].rn = 0; node[nxt].sum = {0, 0}; ++nxt; } } void add(int& x, int y) { LL qwe = 1LL*x+y; if (qwe >= MOD2) qwe -= MOD2; x = qwe; } PII operator+(const PII& x, const PII& y) { LL qwe1 = 1LL*x.ST+y.ST; LL qwe2 = 1LL*x.ND+y.ND; if (qwe1 >= MOD2) qwe1 -= MOD2; if (qwe2 >= MOD2) qwe2 -= MOD2; return {qwe1, qwe2}; } void upd(Node& rt) { rt.sum = {0, 0}; if (rt.ln != 0) rt.sum = rt.sum+node[rt.ln].sum; if (rt.rn != 0) rt.sum = rt.sum+node[rt.rn].sum; } void add(int id, int v, int val) { auto& nd = node[id]; if (nd.l == nd.r) { if (v%2 == 0) add(nd.sum.ST, val); else add(nd.sum.ND, val); return; } int mid = (1LL*nd.l+nd.r)/2; if (v <= mid) { crl(nd); add(nd.ln, v, val); } else { crr(nd); add(nd.rn, v, val); } upd(nd); } bool inters(PII p1, PII p2) { if (p1.ST > p2.ST) swap(p1, p2); return p1.ND >= p2.ST; } PII get(int id, int l, int r) { if (l > r) return {0, 0}; auto& nd = node[id]; if (r < nd.l || l > nd.r) return {0, 0}; if (l <= nd.l && nd.r <= r) return nd.sum; int mid = (1LL*nd.l+nd.r)/2; PII res = {0, 0}; if (inters({l, r}, {nd.l, mid})) { crl(nd); res = res+get(nd.ln, l, r); } if (inters({l, r}, {mid+1, nd.r})) { crr(nd); res = res+get(nd.rn, l, r); } return res; } int main() { init_ios(); cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; node[0].l = node[0].ln = node[0].rn = 0; node[0].r = (1LL<<31)-1; node[0].sum = {0, 0}; int sum = 0; add(0, 0, 1); for (int i = n-1; i >= 0; --i) { add(sum, a[i]); bool pa = false; if (sum%2 == 0) pa = true; if (sum < MOD) { auto sm1 = get(0, 0, sum); auto sm2 = get(0, sum+1, MOD+sum); auto sm3 = get(0, MOD+sum+1, 2*MOD-1); if (pa) { add(res[i], sm1.ST); add(res[i], sm2.ND); add(res[i], sm3.ST); } else { add(res[i], sm1.ND); add(res[i], sm2.ST); add(res[i], sm3.ND); } } else { auto sm1 = get(0, 0, sum-MOD); auto sm2 = get(0, sum-MOD+1, sum); auto sm3 = get(0, sum+1, 2*MOD-1); if (pa) { add(res[i], sm1.ND); add(res[i], sm2.ST); add(res[i], sm3.ND); } else { add(res[i], sm1.ST); add(res[i], sm2.ND); add(res[i], sm3.ST); } } add(0, sum, res[i]); } if (res[0] >= MOD) res[0] -= MOD; cout<<res[0]<<"\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 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 | // Daniel Grzegorzewski // while (clock()<=69*CLOCKS_PER_SEC) #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #pragma GCC optimize("Ofast") // #pragma GCC target("avx,avx2,fma") #define MP make_pair #define PB push_back #define ST first #define ND second using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef long long LL; void init_ios() { ios_base::sync_with_stdio(0); cin.tie(0); } auto random_address = [] { char *p = new char; delete p; return uint64_t(p); }; const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1); mt19937_64 rng(SEED); const int N = 3*(int)1e5 + 3; const int M = 64*N; const int MOD = (int)1e9 + 7; const int MOD2 = 2*MOD; int n, nxt = 1, a[N], res[N]; struct Node { int l, r, ln, rn; PII sum; } node[M]; void crl(Node& rt) { if (rt.ln == 0) { rt.ln = nxt; node[nxt].l = rt.l; node[nxt].r = (1LL*rt.l+rt.r)/2; node[nxt].ln = node[nxt].rn = 0; node[nxt].sum = {0, 0}; ++nxt; } } void crr(Node& rt) { if (rt.rn == 0) { rt.rn = nxt; node[nxt].l = (1LL*rt.l+rt.r)/2+1; node[nxt].r = rt.r; node[nxt].ln = node[nxt].rn = 0; node[nxt].sum = {0, 0}; ++nxt; } } void add(int& x, int y) { LL qwe = 1LL*x+y; if (qwe >= MOD2) qwe -= MOD2; x = qwe; } PII operator+(const PII& x, const PII& y) { LL qwe1 = 1LL*x.ST+y.ST; LL qwe2 = 1LL*x.ND+y.ND; if (qwe1 >= MOD2) qwe1 -= MOD2; if (qwe2 >= MOD2) qwe2 -= MOD2; return {qwe1, qwe2}; } void upd(Node& rt) { rt.sum = {0, 0}; if (rt.ln != 0) rt.sum = rt.sum+node[rt.ln].sum; if (rt.rn != 0) rt.sum = rt.sum+node[rt.rn].sum; } void add(int id, int v, int val) { auto& nd = node[id]; if (nd.l == nd.r) { if (v%2 == 0) add(nd.sum.ST, val); else add(nd.sum.ND, val); return; } int mid = (1LL*nd.l+nd.r)/2; if (v <= mid) { crl(nd); add(nd.ln, v, val); } else { crr(nd); add(nd.rn, v, val); } upd(nd); } bool inters(PII p1, PII p2) { if (p1.ST > p2.ST) swap(p1, p2); return p1.ND >= p2.ST; } PII get(int id, int l, int r) { if (l > r) return {0, 0}; auto& nd = node[id]; if (r < nd.l || l > nd.r) return {0, 0}; if (l <= nd.l && nd.r <= r) return nd.sum; int mid = (1LL*nd.l+nd.r)/2; PII res = {0, 0}; if (inters({l, r}, {nd.l, mid})) { crl(nd); res = res+get(nd.ln, l, r); } if (inters({l, r}, {mid+1, nd.r})) { crr(nd); res = res+get(nd.rn, l, r); } return res; } int main() { init_ios(); cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; node[0].l = node[0].ln = node[0].rn = 0; node[0].r = (1LL<<31)-1; node[0].sum = {0, 0}; int sum = 0; add(0, 0, 1); for (int i = n-1; i >= 0; --i) { add(sum, a[i]); bool pa = false; if (sum%2 == 0) pa = true; if (sum < MOD) { auto sm1 = get(0, 0, sum); auto sm2 = get(0, sum+1, MOD+sum); auto sm3 = get(0, MOD+sum+1, 2*MOD-1); if (pa) { add(res[i], sm1.ST); add(res[i], sm2.ND); add(res[i], sm3.ST); } else { add(res[i], sm1.ND); add(res[i], sm2.ST); add(res[i], sm3.ND); } } else { auto sm1 = get(0, 0, sum-MOD); auto sm2 = get(0, sum-MOD+1, sum); auto sm3 = get(0, sum+1, 2*MOD-1); if (pa) { add(res[i], sm1.ND); add(res[i], sm2.ST); add(res[i], sm3.ND); } else { add(res[i], sm1.ST); add(res[i], sm2.ND); add(res[i], sm3.ST); } } add(0, sum, res[i]); } if (res[0] >= MOD) res[0] -= MOD; cout<<res[0]<<"\n"; } |