#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; a <= i; i--) #define cat(x) cerr << #x << " = " << x << '\n'; using ll = long long; using namespace std; const int N = 300300; const int P = 1e9 + 7; void add(int &a, int b) { a += b; if (a >= P) a -= P; } int n, m, a[N], b[N], dp[N]; struct fenwick { int f[N]; void update(int x, int y) { for (x++; x < N; x += x & -x) add(f[x], y); } int query(int x) { int res = 0; for (x++; x > 0; x -= x & -x) add(res, f[x]); return res; } int range_sum(int l, int r) { int res = query(r); add(res, P - query(l - 1)); return res; } } f[2]; int id(int x) { return lower_bound(b, b + m + 1, x) - b; } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n; rep(i, 1, n) { cin >> a[i]; add(a[i], a[i - 1]); b[i] = a[i]; } sort(b, b + n + 1); m = unique(b, b + n + 1) - b - 1; f[0].update(id(0), 1); rep(i, 1, n) { int x = id(a[i]); int p = a[i] % 2; add(dp[i], f[p].range_sum(0, x)); add(dp[i], f[!p].range_sum(x + 1, m)); f[p].update(x, dp[i]); } cout << dp[n] << '\n'; 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 | #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; a <= i; i--) #define cat(x) cerr << #x << " = " << x << '\n'; using ll = long long; using namespace std; const int N = 300300; const int P = 1e9 + 7; void add(int &a, int b) { a += b; if (a >= P) a -= P; } int n, m, a[N], b[N], dp[N]; struct fenwick { int f[N]; void update(int x, int y) { for (x++; x < N; x += x & -x) add(f[x], y); } int query(int x) { int res = 0; for (x++; x > 0; x -= x & -x) add(res, f[x]); return res; } int range_sum(int l, int r) { int res = query(r); add(res, P - query(l - 1)); return res; } } f[2]; int id(int x) { return lower_bound(b, b + m + 1, x) - b; } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n; rep(i, 1, n) { cin >> a[i]; add(a[i], a[i - 1]); b[i] = a[i]; } sort(b, b + n + 1); m = unique(b, b + n + 1) - b - 1; f[0].update(id(0), 1); rep(i, 1, n) { int x = id(a[i]); int p = a[i] % 2; add(dp[i], f[p].range_sum(0, x)); add(dp[i], f[!p].range_sum(x + 1, m)); f[p].update(x, dp[i]); } cout << dp[n] << '\n'; return 0; } |