#include<bits/stdc++.h> #define fi first #define se second using namespace std; const int N=3e5; const int PP=6e5; const int MOD=1e9+7; int tab[N+10]; int p[N+10]; map<int,int> c[2]; int dp[N+10]; int pot; int tree[2][2*PP+10]; int read(int tt,int l,int r) { int ans=0; for(l+=pot-1,r+=pot-1;l<=r;l/=2,r/=2) { if(l%2==1) ans=(ans+tree[tt][l++])%MOD; if(r%2==0) ans=(ans+tree[tt][r--])%MOD; } return ans; } void add(int tt,int g,int val) { for(g+=pot-1;g>0;g/=2) tree[tt][g]=(tree[tt][g]+val)%MOD; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; for(pot=1;pot<n;pot*=2); for(int i=1;i<=n;i++) { cin>>tab[i]; p[i]=(tab[i]+p[i-1])%MOD; c[p[i]%2][p[i]]=0; } c[0][0]=c[1][0]=c[0][MOD]=c[1][MOD]=0; for(int j:{0,1}) { int i=0; for(auto v:c[j]) c[j][v.fi]=++i; } dp[0]=1; add(p[0]%2,c[p[0]%2][p[0]],1); for(int i=1;i<=n;i++) { dp[i]=(read(p[i]%2,1,c[p[i]%2][p[i]]) +read((p[i]+1)%2,c[(p[i]+1)%2].upper_bound(p[i])->se,pot))%MOD; add(p[i]%2,c[p[i]%2][p[i]],dp[i]); //cerr<<i<<" "<<mp[i]<<" "<<dp[i]<<"\n"; } 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 | #include<bits/stdc++.h> #define fi first #define se second using namespace std; const int N=3e5; const int PP=6e5; const int MOD=1e9+7; int tab[N+10]; int p[N+10]; map<int,int> c[2]; int dp[N+10]; int pot; int tree[2][2*PP+10]; int read(int tt,int l,int r) { int ans=0; for(l+=pot-1,r+=pot-1;l<=r;l/=2,r/=2) { if(l%2==1) ans=(ans+tree[tt][l++])%MOD; if(r%2==0) ans=(ans+tree[tt][r--])%MOD; } return ans; } void add(int tt,int g,int val) { for(g+=pot-1;g>0;g/=2) tree[tt][g]=(tree[tt][g]+val)%MOD; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; for(pot=1;pot<n;pot*=2); for(int i=1;i<=n;i++) { cin>>tab[i]; p[i]=(tab[i]+p[i-1])%MOD; c[p[i]%2][p[i]]=0; } c[0][0]=c[1][0]=c[0][MOD]=c[1][MOD]=0; for(int j:{0,1}) { int i=0; for(auto v:c[j]) c[j][v.fi]=++i; } dp[0]=1; add(p[0]%2,c[p[0]%2][p[0]],1); for(int i=1;i<=n;i++) { dp[i]=(read(p[i]%2,1,c[p[i]%2][p[i]]) +read((p[i]+1)%2,c[(p[i]+1)%2].upper_bound(p[i])->se,pot))%MOD; add(p[i]%2,c[p[i]%2][p[i]],dp[i]); //cerr<<i<<" "<<mp[i]<<" "<<dp[i]<<"\n"; } cout<<dp[n]<<"\n"; return 0; } |