#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } const ll M = 1'000'000'007; template<typename IntType> struct BinaryIndexedTreeSum { int n; vector<IntType> elems; BinaryIndexedTreeSum(int n_){ n = n_ + 1; elems.assign(n, 0); } void add(int idx, IntType v){ idx++; while(idx < n){ elems[idx] += v; if (elems[idx] >= M) elems[idx] %= M; idx += (idx & (-idx)); } } IntType get(int idx) { return sum(idx, idx + 1); } // prefix sum of A[0], A[1], ..., A[idx - 1] IntType __prefix(int idx){ IntType res = 0; while(idx > 0){ res += elems[idx]; if (res >= M) res %= M; idx ^= (idx & (-idx)); } return res; } IntType sum(int l, int r){ return (__prefix(r) - __prefix(l) + M) % M; } }; using BitSum = BinaryIndexedTreeSum<int>; int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vi V(n); cin >> V; vi P(n + 1); P[0] = 0; FOR(i,0,n) P[i + 1] = (P[i] + V[i]) % M; vi E, O; FOR(i,0,n+1) { if(P[i] & 1) O.PB(P[i]); else E.PB(P[i]); } sort(ALL(O)); sort(ALL(E)); O.erase(unique(ALL(O)), O.end()); E.erase(unique(ALL(E)), E.end()); BitSum bso((int) O.size()), bse((int) E.size()); bse.add(0, 1); int ans = 0; FOR(i,1,n+1){ int x = P[i]; if (x % 2 == 0){ int xe = lower_bound(ALL(E), x) - E.begin(); auto ye = lower_bound(ALL(O), x); ans = bse.sum(0, xe + 1); if (ye != O.end()) ans += bso.sum(ye - O.begin(), O.size()); if (ans >= M) ans %= M; bse.add(xe, ans); } else { int xo = lower_bound(ALL(O), x) - O.begin(); auto yo = lower_bound(ALL(E), x); ans = bso.sum(0, xo + 1); if (yo != E.end()) ans += bse.sum(yo - E.begin(), E.size()); if (ans >= M) ans %= M; bso.add(xo, ans); } } cout << ans << endl; }
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 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } const ll M = 1'000'000'007; template<typename IntType> struct BinaryIndexedTreeSum { int n; vector<IntType> elems; BinaryIndexedTreeSum(int n_){ n = n_ + 1; elems.assign(n, 0); } void add(int idx, IntType v){ idx++; while(idx < n){ elems[idx] += v; if (elems[idx] >= M) elems[idx] %= M; idx += (idx & (-idx)); } } IntType get(int idx) { return sum(idx, idx + 1); } // prefix sum of A[0], A[1], ..., A[idx - 1] IntType __prefix(int idx){ IntType res = 0; while(idx > 0){ res += elems[idx]; if (res >= M) res %= M; idx ^= (idx & (-idx)); } return res; } IntType sum(int l, int r){ return (__prefix(r) - __prefix(l) + M) % M; } }; using BitSum = BinaryIndexedTreeSum<int>; int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vi V(n); cin >> V; vi P(n + 1); P[0] = 0; FOR(i,0,n) P[i + 1] = (P[i] + V[i]) % M; vi E, O; FOR(i,0,n+1) { if(P[i] & 1) O.PB(P[i]); else E.PB(P[i]); } sort(ALL(O)); sort(ALL(E)); O.erase(unique(ALL(O)), O.end()); E.erase(unique(ALL(E)), E.end()); BitSum bso((int) O.size()), bse((int) E.size()); bse.add(0, 1); int ans = 0; FOR(i,1,n+1){ int x = P[i]; if (x % 2 == 0){ int xe = lower_bound(ALL(E), x) - E.begin(); auto ye = lower_bound(ALL(O), x); ans = bse.sum(0, xe + 1); if (ye != O.end()) ans += bso.sum(ye - O.begin(), O.size()); if (ans >= M) ans %= M; bse.add(xe, ans); } else { int xo = lower_bound(ALL(O), x) - O.begin(); auto yo = lower_bound(ALL(E), x); ans = bso.sum(0, xo + 1); if (yo != E.end()) ans += bse.sum(yo - E.begin(), E.size()); if (ans >= M) ans %= M; bso.add(xo, ans); } } cout << ans << endl; } |