#include <bits/stdc++.h> #include <iostream> using namespace std; constexpr int M = 1e9+7, N=3e5+10; int magic(int x) { return x & (-x); } class Tree { public: Tree(size_t size) : size(size) { values.assign(size+1, 0); } void update(int x, int val) { while(x <= size) { values[x] += val; values[x] %= M; x += magic(x); } } int collect(int x) { int res = 0; while(x != 0) { res += values[x]; res %= M; x -= magic(x); } return res; } int collect_range(int a, int b) { return (collect(b) - collect(a-1) + M) % M; } int collect_upper(int a) { return collect_range(a, size); } private: size_t size; vector<int> values; }; void remove_duplicates(vector<int> &vec) { sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); } int main() { ios_base::sync_with_stdio(false); int pref=0; int n; cin >> n; vector<int> values, parz, nparz; for(int i=0; i < n; i++) { int a; cin >> a; pref += a; pref %= M; values.push_back(pref); if (pref % 2 == 0) { parz.push_back(pref); } else { nparz.push_back(pref); } } remove_duplicates(parz); remove_duplicates(nparz); Tree nparzTree(nparz.size()), parzTree(parz.size()); for (int i=0; i < n; i++) { int res; if (values[i] % 2 == 0) { res = 1; int idx = distance(parz.begin(), lower_bound(parz.begin(), parz.end(), values[i])) + 1; res += parzTree.collect(idx); res %= M; auto it = lower_bound(nparz.begin(), nparz.end(), values[i]); if (it != nparz.end()) { int idy = distance(nparz.begin(), it) + 1; res += nparzTree.collect_upper(idy); res %= M; } parzTree.update(idx, res); } else { res = 0; int idx = distance(nparz.begin(), lower_bound(nparz.begin(), nparz.end(), values[i])) + 1; res += nparzTree.collect(idx); res %= M; auto it = lower_bound(parz.begin(), parz.end(), values[i]); if (it != parz.end()) { int idy = distance(parz.begin(), it) + 1; res += parzTree.collect_upper(idy); res %= M; } nparzTree.update(idx, res); } if (i == n-1) { cout << res; } } }
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 | #include <bits/stdc++.h> #include <iostream> using namespace std; constexpr int M = 1e9+7, N=3e5+10; int magic(int x) { return x & (-x); } class Tree { public: Tree(size_t size) : size(size) { values.assign(size+1, 0); } void update(int x, int val) { while(x <= size) { values[x] += val; values[x] %= M; x += magic(x); } } int collect(int x) { int res = 0; while(x != 0) { res += values[x]; res %= M; x -= magic(x); } return res; } int collect_range(int a, int b) { return (collect(b) - collect(a-1) + M) % M; } int collect_upper(int a) { return collect_range(a, size); } private: size_t size; vector<int> values; }; void remove_duplicates(vector<int> &vec) { sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); } int main() { ios_base::sync_with_stdio(false); int pref=0; int n; cin >> n; vector<int> values, parz, nparz; for(int i=0; i < n; i++) { int a; cin >> a; pref += a; pref %= M; values.push_back(pref); if (pref % 2 == 0) { parz.push_back(pref); } else { nparz.push_back(pref); } } remove_duplicates(parz); remove_duplicates(nparz); Tree nparzTree(nparz.size()), parzTree(parz.size()); for (int i=0; i < n; i++) { int res; if (values[i] % 2 == 0) { res = 1; int idx = distance(parz.begin(), lower_bound(parz.begin(), parz.end(), values[i])) + 1; res += parzTree.collect(idx); res %= M; auto it = lower_bound(nparz.begin(), nparz.end(), values[i]); if (it != nparz.end()) { int idy = distance(nparz.begin(), it) + 1; res += nparzTree.collect_upper(idy); res %= M; } parzTree.update(idx, res); } else { res = 0; int idx = distance(nparz.begin(), lower_bound(nparz.begin(), nparz.end(), values[i])) + 1; res += nparzTree.collect(idx); res %= M; auto it = lower_bound(parz.begin(), parz.end(), values[i]); if (it != parz.end()) { int idy = distance(parz.begin(), it) + 1; res += parzTree.collect_upper(idy); res %= M; } nparzTree.update(idx, res); } if (i == n-1) { cout << res; } } } |