#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SZ(a) ((int)(a).size()) #define ALL(a) (a).begin(), (a).end() using ll = long long; using vi = vector<int>; using vvi = vector<vi>; constexpr int kInf = 1000 * 1000 * 1000 + 7; constexpr int kMod = 1000 * 1000 * 1000 + 7; constexpr long long kInfLL = 1e18; void Add(int& x, int y) { x += y; if (x >= kMod) x -= kMod; } struct SegTree { int n; vector<int> t; SegTree() {} SegTree(int n): n(n), t(4 * n) {} void Update(int pos, int val) { Update(1, 0, n - 1, pos, val); } void Update(int v, int tl, int tr, int pos, int val) { if (tl == tr) { Add(t[v], val); return; } int tm = (tl + tr) / 2; if (pos <= tm) { Update(2 * v, tl, tm, pos, val); } else { Update(2 * v + 1, tm + 1, tr, pos, val); } t[v] = t[2 * v]; Add(t[v], t[2 * v + 1]); } int Sum(int l, int r) { return Sum(1, 0, n - 1, l, r); } int Sum(int v, int tl, int tr, int l, int r) { if (l > tr || r < tl) return 0; if (l <= tl && r >= tr) return t[v]; int tm = (tl + tr) / 2; int result = 0; Add(result, Sum(2 * v, tl, tm, l, r)); Add(result, Sum(2 * v + 1, tm + 1, tr, l, r)); return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vi a(n + 1); for (int i = 1; i <= n; ++i) { cin >> a[i]; Add(a[i], a[i - 1]); } vi c = a; sort(ALL(c)); c.erase(unique(ALL(c)), c.end()); SegTree t[2]; t[0] = t[1] = SegTree(SZ(c)); t[0].Update(0, 1); int result = 0; for (int i = 1; i <= n; ++i) { int val = lower_bound(ALL(c), a[i]) - c.begin(); result = 0; Add(result, t[a[i] % 2].Sum(0, val)); Add(result, t[1 - a[i] % 2].Sum(val, SZ(c) - 1)); t[a[i] % 2].Update(val, result); } cout << result << '\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 | #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SZ(a) ((int)(a).size()) #define ALL(a) (a).begin(), (a).end() using ll = long long; using vi = vector<int>; using vvi = vector<vi>; constexpr int kInf = 1000 * 1000 * 1000 + 7; constexpr int kMod = 1000 * 1000 * 1000 + 7; constexpr long long kInfLL = 1e18; void Add(int& x, int y) { x += y; if (x >= kMod) x -= kMod; } struct SegTree { int n; vector<int> t; SegTree() {} SegTree(int n): n(n), t(4 * n) {} void Update(int pos, int val) { Update(1, 0, n - 1, pos, val); } void Update(int v, int tl, int tr, int pos, int val) { if (tl == tr) { Add(t[v], val); return; } int tm = (tl + tr) / 2; if (pos <= tm) { Update(2 * v, tl, tm, pos, val); } else { Update(2 * v + 1, tm + 1, tr, pos, val); } t[v] = t[2 * v]; Add(t[v], t[2 * v + 1]); } int Sum(int l, int r) { return Sum(1, 0, n - 1, l, r); } int Sum(int v, int tl, int tr, int l, int r) { if (l > tr || r < tl) return 0; if (l <= tl && r >= tr) return t[v]; int tm = (tl + tr) / 2; int result = 0; Add(result, Sum(2 * v, tl, tm, l, r)); Add(result, Sum(2 * v + 1, tm + 1, tr, l, r)); return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vi a(n + 1); for (int i = 1; i <= n; ++i) { cin >> a[i]; Add(a[i], a[i - 1]); } vi c = a; sort(ALL(c)); c.erase(unique(ALL(c)), c.end()); SegTree t[2]; t[0] = t[1] = SegTree(SZ(c)); t[0].Update(0, 1); int result = 0; for (int i = 1; i <= n; ++i) { int val = lower_bound(ALL(c), a[i]) - c.begin(); result = 0; Add(result, t[a[i] % 2].Sum(0, val)); Add(result, t[1 - a[i] % 2].Sum(val, SZ(c) - 1)); t[a[i] % 2].Update(val, result); } cout << result << '\n'; } |