#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define SIZE(a) ((int)a.size()) const int mod = 1e9+7; struct Tree { static const int size = 1<<20; int shift = 1<<19; vector<int> t; Tree() : t(size) {} void add(int id, int value) { id+=shift; t[id] = (t[id]+value)%mod; id/=2; while(id) { t[id] = (t[2*id] + t[2*id+1])%mod; id/=2; } } ll sum(int left, int right) { if(left > right) { return 0; } left += shift; right += shift; ll res = t[left]; if(left < right) { res += t[right]; } while(left/2 != right/2) { if(left % 2 == 0) { res += t[left+1]; } if(right %2 == 1) { res += t[right-1]; } left/=2; right/=2; } return res; } }; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n); ll sum = 0; vector<int> values; values.push_back(sum); for(int &a_i : a) { cin >> a_i; sum = (sum+a_i)%mod; values.push_back(sum); } sort(values.begin(), values.end()); map<int, int> mapping; int num = 0; int prev_value = -1; for(int value : values) { if(prev_value != value) { mapping[value] = num++; } prev_value = value; } sum = 0; Tree even, odd; even.add(0, 1); ll value = 0; for(int i=0; i < n; i++) { sum = (sum+a[i])%mod; int id = mapping[sum]; if(sum%2 == 1) { value = odd.sum(0, id) + even.sum(id+1, num-1); value%=mod; odd.add(id, value); } else { value = even.sum(0, id) + odd.sum(id+1, num-1); value%=mod; even.add(id, value); } // cout << value << "\n"; } cout << value << "\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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define SIZE(a) ((int)a.size()) const int mod = 1e9+7; struct Tree { static const int size = 1<<20; int shift = 1<<19; vector<int> t; Tree() : t(size) {} void add(int id, int value) { id+=shift; t[id] = (t[id]+value)%mod; id/=2; while(id) { t[id] = (t[2*id] + t[2*id+1])%mod; id/=2; } } ll sum(int left, int right) { if(left > right) { return 0; } left += shift; right += shift; ll res = t[left]; if(left < right) { res += t[right]; } while(left/2 != right/2) { if(left % 2 == 0) { res += t[left+1]; } if(right %2 == 1) { res += t[right-1]; } left/=2; right/=2; } return res; } }; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n); ll sum = 0; vector<int> values; values.push_back(sum); for(int &a_i : a) { cin >> a_i; sum = (sum+a_i)%mod; values.push_back(sum); } sort(values.begin(), values.end()); map<int, int> mapping; int num = 0; int prev_value = -1; for(int value : values) { if(prev_value != value) { mapping[value] = num++; } prev_value = value; } sum = 0; Tree even, odd; even.add(0, 1); ll value = 0; for(int i=0; i < n; i++) { sum = (sum+a[i])%mod; int id = mapping[sum]; if(sum%2 == 1) { value = odd.sum(0, id) + even.sum(id+1, num-1); value%=mod; odd.add(id, value); } else { value = even.sum(0, id) + odd.sum(id+1, num-1); value%=mod; even.add(id, value); } // cout << value << "\n"; } cout << value << "\n"; } |