#include <bits/stdc++.h> #define fi first #define sc second #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int,int> pii; const int MM = 300013; const int P = 1000000007; struct NN { int sum[2]; NN * L, * R; NN() {sum[0] = 0, sum[1] = 0, L = nullptr, R = nullptr;} } Node[MM * 30]; int dp[MM]; int IND = 0; inline NN * getNew() { return Node + IND++; } inline int add(const int &a, const int &b) { return a + b >= P ? a + b - P : a + b; } void Update(NN * N, int p, int k, const int &ind, const int &v) { if (p == k) { N->sum[p&1] = add(N->sum[p&1], v); return; } int mid = (p+k) >> 1; if (ind <= mid) { if (N->L == nullptr) N->L = getNew(); Update(N->L,p,mid,ind,v); } else { if (N->R == nullptr) N->R = getNew(); Update(N->R,mid+1,k,ind,v); } N->sum[0] = 0; N->sum[1] = 0; if (N->L != nullptr) N->sum[0] = add(N->sum[0], N->L->sum[0]), N->sum[1] = add(N->sum[1], N->L->sum[1]); if (N->R != nullptr) N->sum[0] = add(N->sum[0], N->R->sum[0]), N->sum[1] = add(N->sum[1], N->R->sum[1]); } int Query(NN * N, int p, int k, int l, int r, int par) { if(p > r || k < l) return 0; if(l<=p&&k<=r) return N->sum[par]; int mid = (p+k)>>1; int res = 0; if (N->L != nullptr) res = add(res, Query(N->L,p,mid,l,r,par)); if (N->R != nullptr) res = add(res, Query(N->R,mid+1,k,l,r,par)); return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); NN * root = getNew(); int n; cin>>n; Update(root,0,P-1,0,1); dp[0] = 1; forn(i,1,n) cin>>dp[i]; int sum = 0; forn(i,1,n) { sum = add(sum, dp[i]); dp[i] = 0; dp[i] = add(dp[i], Query(root, 0, P-1, 0, sum, sum&1)); if (sum != P-1) dp[i] = add(dp[i], Query(root, 0, P-1 , sum+1, P-1, (sum&1)^1)); Update(root,0,P-1,sum,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 70 71 72 73 74 75 76 77 78 | #include <bits/stdc++.h> #define fi first #define sc second #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int,int> pii; const int MM = 300013; const int P = 1000000007; struct NN { int sum[2]; NN * L, * R; NN() {sum[0] = 0, sum[1] = 0, L = nullptr, R = nullptr;} } Node[MM * 30]; int dp[MM]; int IND = 0; inline NN * getNew() { return Node + IND++; } inline int add(const int &a, const int &b) { return a + b >= P ? a + b - P : a + b; } void Update(NN * N, int p, int k, const int &ind, const int &v) { if (p == k) { N->sum[p&1] = add(N->sum[p&1], v); return; } int mid = (p+k) >> 1; if (ind <= mid) { if (N->L == nullptr) N->L = getNew(); Update(N->L,p,mid,ind,v); } else { if (N->R == nullptr) N->R = getNew(); Update(N->R,mid+1,k,ind,v); } N->sum[0] = 0; N->sum[1] = 0; if (N->L != nullptr) N->sum[0] = add(N->sum[0], N->L->sum[0]), N->sum[1] = add(N->sum[1], N->L->sum[1]); if (N->R != nullptr) N->sum[0] = add(N->sum[0], N->R->sum[0]), N->sum[1] = add(N->sum[1], N->R->sum[1]); } int Query(NN * N, int p, int k, int l, int r, int par) { if(p > r || k < l) return 0; if(l<=p&&k<=r) return N->sum[par]; int mid = (p+k)>>1; int res = 0; if (N->L != nullptr) res = add(res, Query(N->L,p,mid,l,r,par)); if (N->R != nullptr) res = add(res, Query(N->R,mid+1,k,l,r,par)); return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); NN * root = getNew(); int n; cin>>n; Update(root,0,P-1,0,1); dp[0] = 1; forn(i,1,n) cin>>dp[i]; int sum = 0; forn(i,1,n) { sum = add(sum, dp[i]); dp[i] = 0; dp[i] = add(dp[i], Query(root, 0, P-1, 0, sum, sum&1)); if (sum != P-1) dp[i] = add(dp[i], Query(root, 0, P-1 , sum+1, P-1, (sum&1)^1)); Update(root,0,P-1,sum,dp[i]); } cout<<dp[n]<<"\n"; return 0; } |