#include <bits/stdc++.h> //Michał Kuśmirek using namespace std; //XIII LO Szczecin #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) using ll = long long; using pii = pair<int, int>; template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- const int MOD = int(1e9) + 7; int add(int a, int b) { if(a + b >= MOD) return a + b - MOD; return a + b; } struct SegmentTree { //add in point, sum on segment vector<int> T; int sz = 1; SegmentTree(int n) { while(sz < n) sz *= 2; T.resize(2 * sz); } void insert(int p, int val) { p += sz; T[p] = add(T[p], val); while(p /= 2) T[p] = add(T[p], val); } int sum(int l, int r) { if(l > r) return 0; l += sz, r += sz; int ret = T[l]; if(l != r) ret = add(ret, T[r]); while(l / 2 != r / 2) { if(l % 2 == 0) ret = add(ret, T[l + 1]); if(r % 2 == 1) ret = add(ret, T[r - 1]); l /= 2, r /= 2; } return ret; } }; vector<int> scale(vector<int> a) { int n = size(a); vector<pair<int, int>> vec(n); REP(i, n) vec[i] = pair(a[i], i); sort(vec.begin(), vec.end()); int k = 0; REP(i, n) { if(i > 0 and vec[i].first > vec[i - 1].first) ++k; a[vec[i].second] = k; } return a; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n), pref(n), overflow(n); REP(i, n) { cin >> a[i]; pref[i] = a[i]; if(i > 0) { overflow[i] = overflow[i - 1]; pref[i] += pref[i - 1]; if(pref[i] >= MOD) { ++overflow[i]; pref[i] -= MOD; } } } debug(pref); debug(overflow); vector<int> position = scale(pref); debug(position); vector<SegmentTree> tree(2, SegmentTree(n)); vector<int> dp(n); REP(i, n) { int p = position[i]; int par = pref[i] % 2; dp[i] = add(dp[i], tree[par].sum(0, p)); dp[i] = add(dp[i], tree[par ^ 1].sum(p + 1, n - 1)); if(par == 0) dp[i] = add(dp[i], 1); tree[par].insert(p, dp[i]); } debug(dp); cout << dp.back() << '\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 | #include <bits/stdc++.h> //Michał Kuśmirek using namespace std; //XIII LO Szczecin #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) using ll = long long; using pii = pair<int, int>; template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- const int MOD = int(1e9) + 7; int add(int a, int b) { if(a + b >= MOD) return a + b - MOD; return a + b; } struct SegmentTree { //add in point, sum on segment vector<int> T; int sz = 1; SegmentTree(int n) { while(sz < n) sz *= 2; T.resize(2 * sz); } void insert(int p, int val) { p += sz; T[p] = add(T[p], val); while(p /= 2) T[p] = add(T[p], val); } int sum(int l, int r) { if(l > r) return 0; l += sz, r += sz; int ret = T[l]; if(l != r) ret = add(ret, T[r]); while(l / 2 != r / 2) { if(l % 2 == 0) ret = add(ret, T[l + 1]); if(r % 2 == 1) ret = add(ret, T[r - 1]); l /= 2, r /= 2; } return ret; } }; vector<int> scale(vector<int> a) { int n = size(a); vector<pair<int, int>> vec(n); REP(i, n) vec[i] = pair(a[i], i); sort(vec.begin(), vec.end()); int k = 0; REP(i, n) { if(i > 0 and vec[i].first > vec[i - 1].first) ++k; a[vec[i].second] = k; } return a; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n), pref(n), overflow(n); REP(i, n) { cin >> a[i]; pref[i] = a[i]; if(i > 0) { overflow[i] = overflow[i - 1]; pref[i] += pref[i - 1]; if(pref[i] >= MOD) { ++overflow[i]; pref[i] -= MOD; } } } debug(pref); debug(overflow); vector<int> position = scale(pref); debug(position); vector<SegmentTree> tree(2, SegmentTree(n)); vector<int> dp(n); REP(i, n) { int p = position[i]; int par = pref[i] % 2; dp[i] = add(dp[i], tree[par].sum(0, p)); dp[i] = add(dp[i], tree[par ^ 1].sum(p + 1, n - 1)); if(par == 0) dp[i] = add(dp[i], 1); tree[par].insert(p, dp[i]); } debug(dp); cout << dp.back() << '\n'; } |