#if defined(EMBE_DEBUG) && !defined(NDEBUG) #include "embe-debug.hpp" #else #define LOG_INDENT(...) do {} while (false) #define LOG(...) do {} while (false) #define DUMP(...) do {} while (false) #endif #include <algorithm> #include <array> #include <iostream> #include <vector> using namespace std; namespace { constexpr auto mod = 1'000'000'007; struct Node { int low = 0; int high = 0; array<int, 2> sum = {0, 0}; }; struct State { int extra = 0; int n; vector<int> data; vector<Node> tree; void reserve(int x) { x -= extra; if (x < 0) x += mod; data.emplace_back(x); } void update_sum(int k) { tree[k].sum[0] = (tree[2 * k].sum[0] + tree[2 * k + 1].sum[0]) % mod; tree[k].sum[1] = (tree[2 * k].sum[1] + tree[2 * k + 1].sum[1]) % mod; } void reset() { extra = 0; sort(data.begin(), data.end()); data.erase(unique(data.begin(), data.end()), data.end()); n = 1; while (n <= int(data.size())) n *= 2; tree.resize(2 * n); for (int i = 0; i < int(data.size()); ++i) { tree[n + i].low = tree[n + i].high = data[i]; } for (int i = data.size(); i < n; ++i) { tree[n + i].low = tree[n + i].high = mod; } for (int i = n - 1; i > 0; --i) { tree[i].low = tree[2 * i].low; tree[i].high = tree[2 * i + 1].high; update_sum(i); } } void add(int x, int v) { x -= extra; if (x < 0) x += mod; int i = lower_bound(data.begin(), data.end(), x) - data.begin(); int k = n + i; tree[k].sum[x % 2] += v; tree[k].sum[x % 2] %= mod; while (k > 1) { k /= 2; update_sum(k); } } void rotate(int dx) { extra += dx; extra %= mod; } int sum_even() const { int res = 0; res += sum(mod - extra, mod, (extra + 1) % 2); res += sum(0, mod - extra, extra % 2); res %= mod; return res; } int sum(int a, int b, int m, int k=1) const { if (k >= 2 * n) return 0; if (a > tree[k].high || b <= tree[k].low) return 0; if (a <= tree[k].low && b > tree[k].high) return tree[k].sum[m]; int res = 0; res += sum(a, b, m, 2 * k); res += sum(a, b, m, 2 * k + 1); return res % mod; } }; } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> data(n); for (int& x: data) cin >> x; int sum = 0; for (int x: data) { sum += x; sum %= mod; } State state; state.reserve(data[0]); for (int i = 1; i < n; ++i) { state.reserve(0); state.rotate(data[i]); } state.reset(); state.add(data[0], 1); for (int i = 1; i < n; ++i) { state.add(0, state.sum_even()); state.rotate(data[i]); } cout << state.sum_even() << endl; 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 | #if defined(EMBE_DEBUG) && !defined(NDEBUG) #include "embe-debug.hpp" #else #define LOG_INDENT(...) do {} while (false) #define LOG(...) do {} while (false) #define DUMP(...) do {} while (false) #endif #include <algorithm> #include <array> #include <iostream> #include <vector> using namespace std; namespace { constexpr auto mod = 1'000'000'007; struct Node { int low = 0; int high = 0; array<int, 2> sum = {0, 0}; }; struct State { int extra = 0; int n; vector<int> data; vector<Node> tree; void reserve(int x) { x -= extra; if (x < 0) x += mod; data.emplace_back(x); } void update_sum(int k) { tree[k].sum[0] = (tree[2 * k].sum[0] + tree[2 * k + 1].sum[0]) % mod; tree[k].sum[1] = (tree[2 * k].sum[1] + tree[2 * k + 1].sum[1]) % mod; } void reset() { extra = 0; sort(data.begin(), data.end()); data.erase(unique(data.begin(), data.end()), data.end()); n = 1; while (n <= int(data.size())) n *= 2; tree.resize(2 * n); for (int i = 0; i < int(data.size()); ++i) { tree[n + i].low = tree[n + i].high = data[i]; } for (int i = data.size(); i < n; ++i) { tree[n + i].low = tree[n + i].high = mod; } for (int i = n - 1; i > 0; --i) { tree[i].low = tree[2 * i].low; tree[i].high = tree[2 * i + 1].high; update_sum(i); } } void add(int x, int v) { x -= extra; if (x < 0) x += mod; int i = lower_bound(data.begin(), data.end(), x) - data.begin(); int k = n + i; tree[k].sum[x % 2] += v; tree[k].sum[x % 2] %= mod; while (k > 1) { k /= 2; update_sum(k); } } void rotate(int dx) { extra += dx; extra %= mod; } int sum_even() const { int res = 0; res += sum(mod - extra, mod, (extra + 1) % 2); res += sum(0, mod - extra, extra % 2); res %= mod; return res; } int sum(int a, int b, int m, int k=1) const { if (k >= 2 * n) return 0; if (a > tree[k].high || b <= tree[k].low) return 0; if (a <= tree[k].low && b > tree[k].high) return tree[k].sum[m]; int res = 0; res += sum(a, b, m, 2 * k); res += sum(a, b, m, 2 * k + 1); return res % mod; } }; } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> data(n); for (int& x: data) cin >> x; int sum = 0; for (int x: data) { sum += x; sum %= mod; } State state; state.reserve(data[0]); for (int i = 1; i < n; ++i) { state.reserve(0); state.rotate(data[i]); } state.reset(); state.add(data[0], 1); for (int i = 1; i < n; ++i) { state.add(0, state.sum_even()); state.rotate(data[i]); } cout << state.sum_even() << endl; return 0; } |