#include <cstdio> #include <vector> #include <algorithm> const int MOD = 1000000007; struct CountTree { long long int *el, s; CountTree(int size) { el = new long long int[2 * (s = 1 << size)]; for (int x = 0; x < 2 * s; ++x) { el[x] = 0; } } ~CountTree() { delete[]el; } void Set(int p, int v) { for (p += s; p; p >>= 1) { el[p] += v; } } long long int Find(int p, int k) { long long int m = 0; p += s; k += s; while (p < k) { if (p & 1) m += el[p++]; if (!(k & 1)) m += el[k--]; p >>= 1; k >>= 1; } if (p == k) m += el[p]; return m; } }; CountTree tree_even(19); CountTree tree_odd(19); std::vector<int> partials; long long int get(CountTree &tree, int lhs, int rhs) { int a = std::lower_bound(partials.begin(), partials.end(), lhs) - partials.begin(); int b = std::upper_bound(partials.begin(), partials.end(), rhs) - partials.begin() - 1; if (a>b) { return 0; } else { return tree.Find(a, b) % MOD; } } void set(CountTree &tree, int key, int value) { int a = std::lower_bound(partials.begin(), partials.end(), key) - partials.begin(); tree.Set(a, value); } int main() { int N; std::vector<int> values; scanf("%d", &N); for (int i=0; i<N; ++i) { int x; scanf("%d", &x); values.push_back(x); } long long int sum=0; std::vector<int> sums(N, 0); for (int i=N-1; i>=0; i--) { sum += values[i]; sum %= MOD; partials.push_back(sum); sums[i] = sum; } std::sort(partials.begin(), partials.end()); long long int total = 1; for (int i=0; i<N; ++i) { int val = values[i]; long long int result = 0; int cum = sums[i]; int lhs = cum; //0 -> cum int rhs = (cum+MOD-val-1); // MOD-val-1 -> (cum+MOD-val-1)%MOD int lhs2 = cum+MOD-val; //MOD-val int rhs2 = cum+MOD-1; //MOD-1 if (val % 2 == 0) { result += total; } if ((val % 2 == 0) == (cum % 2 == 0)) { result += get(tree_even, lhs, rhs); if (rhs >= MOD) { result += get(tree_odd, 0, rhs % MOD); } result += get(tree_odd, lhs2, rhs2); if (rhs2 >= MOD) { if (lhs2 >= MOD) { result += get(tree_even, lhs2%MOD, rhs2%MOD); } else { result += get(tree_even, 0, rhs2 % MOD); } } } else { result += get(tree_odd, lhs, rhs); if (rhs >= MOD) { result += get(tree_even, 0, rhs % MOD); } result += get(tree_even, lhs2, rhs2); if (rhs2 >= MOD) { if (lhs2 >= MOD) { result += get(tree_odd, lhs2%MOD, rhs2%MOD); } else { result += get(tree_odd, 0, rhs2 % MOD); } } } if (cum % 2 == 0) { set(tree_even, cum, total); } else { set(tree_odd, cum, total); } total = result % MOD; } printf("%lld\n", total); 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 | #include <cstdio> #include <vector> #include <algorithm> const int MOD = 1000000007; struct CountTree { long long int *el, s; CountTree(int size) { el = new long long int[2 * (s = 1 << size)]; for (int x = 0; x < 2 * s; ++x) { el[x] = 0; } } ~CountTree() { delete[]el; } void Set(int p, int v) { for (p += s; p; p >>= 1) { el[p] += v; } } long long int Find(int p, int k) { long long int m = 0; p += s; k += s; while (p < k) { if (p & 1) m += el[p++]; if (!(k & 1)) m += el[k--]; p >>= 1; k >>= 1; } if (p == k) m += el[p]; return m; } }; CountTree tree_even(19); CountTree tree_odd(19); std::vector<int> partials; long long int get(CountTree &tree, int lhs, int rhs) { int a = std::lower_bound(partials.begin(), partials.end(), lhs) - partials.begin(); int b = std::upper_bound(partials.begin(), partials.end(), rhs) - partials.begin() - 1; if (a>b) { return 0; } else { return tree.Find(a, b) % MOD; } } void set(CountTree &tree, int key, int value) { int a = std::lower_bound(partials.begin(), partials.end(), key) - partials.begin(); tree.Set(a, value); } int main() { int N; std::vector<int> values; scanf("%d", &N); for (int i=0; i<N; ++i) { int x; scanf("%d", &x); values.push_back(x); } long long int sum=0; std::vector<int> sums(N, 0); for (int i=N-1; i>=0; i--) { sum += values[i]; sum %= MOD; partials.push_back(sum); sums[i] = sum; } std::sort(partials.begin(), partials.end()); long long int total = 1; for (int i=0; i<N; ++i) { int val = values[i]; long long int result = 0; int cum = sums[i]; int lhs = cum; //0 -> cum int rhs = (cum+MOD-val-1); // MOD-val-1 -> (cum+MOD-val-1)%MOD int lhs2 = cum+MOD-val; //MOD-val int rhs2 = cum+MOD-1; //MOD-1 if (val % 2 == 0) { result += total; } if ((val % 2 == 0) == (cum % 2 == 0)) { result += get(tree_even, lhs, rhs); if (rhs >= MOD) { result += get(tree_odd, 0, rhs % MOD); } result += get(tree_odd, lhs2, rhs2); if (rhs2 >= MOD) { if (lhs2 >= MOD) { result += get(tree_even, lhs2%MOD, rhs2%MOD); } else { result += get(tree_even, 0, rhs2 % MOD); } } } else { result += get(tree_odd, lhs, rhs); if (rhs >= MOD) { result += get(tree_even, 0, rhs % MOD); } result += get(tree_even, lhs2, rhs2); if (rhs2 >= MOD) { if (lhs2 >= MOD) { result += get(tree_odd, lhs2%MOD, rhs2%MOD); } else { result += get(tree_odd, 0, rhs2 % MOD); } } } if (cum % 2 == 0) { set(tree_even, cum, total); } else { set(tree_odd, cum, total); } total = result % MOD; } printf("%lld\n", total); return 0; } |