#include<bits/stdc++.h>
using namespace std;
#define d(a)cout<<#a<<" = "<<a<<"\n";
#define f first
#define s second
#define mp make_pair
#define TURBO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define M 1000000007
#define MAXN 300300
#define TREESIZE (1<<20)+10
ll pref[MAXN];
ll a[MAXN];
ll b[MAXN];
map<ll,int>id;
int shift=1;
ll tree[MAXN][2];
void update(int p,ll val,int t){
for (tree[p += shift][t] = val; p > 1; p >>= 1) tree[p>>1][t] = tree[p][t] + tree[p^1][t];
}
ll querry(int l,int r,int t){
ll ans=0;
for(l+=shift,r+=shift+1;l<r;l>>=1,r>>=1){
if(l&1)(ans+=tree[l++][t])%=M;
if(r&1)(ans+=tree[--r][t])%=M;
}
return ans;
}
int main(){
TURBO;
int n;
cin>>n;
while (shift<=n)shift<<=1;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)pref[i]=(pref[i-1]+a[i])%M;
for(int i=0;i<n;i++)b[i]=pref[i+1];
sort(b,b+n);
for(int i=0;i<n;i++)id[b[i]]=i+1;
update(0,1,0);
ll res=0;
for(int i=1;i<=n;i++){
int k=id[pref[i]];
if(pref[i]%2){//nieparzysta
res=(querry(0,k,1)+querry(k+1,n,0))%M;
}
else{//parzysta
res=(querry(0,k,0)+querry(k+1,n,1))%M;
}
update(k,res,pref[i]%2);
}
cout<<res;
}
//4 1000000006 1 5 1000000004
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 | #include<bits/stdc++.h> using namespace std; #define d(a)cout<<#a<<" = "<<a<<"\n"; #define f first #define s second #define mp make_pair #define TURBO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define ll long long #define M 1000000007 #define MAXN 300300 #define TREESIZE (1<<20)+10 ll pref[MAXN]; ll a[MAXN]; ll b[MAXN]; map<ll,int>id; int shift=1; ll tree[MAXN][2]; void update(int p,ll val,int t){ for (tree[p += shift][t] = val; p > 1; p >>= 1) tree[p>>1][t] = tree[p][t] + tree[p^1][t]; } ll querry(int l,int r,int t){ ll ans=0; for(l+=shift,r+=shift+1;l<r;l>>=1,r>>=1){ if(l&1)(ans+=tree[l++][t])%=M; if(r&1)(ans+=tree[--r][t])%=M; } return ans; } int main(){ TURBO; int n; cin>>n; while (shift<=n)shift<<=1; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<=n;i++)pref[i]=(pref[i-1]+a[i])%M; for(int i=0;i<n;i++)b[i]=pref[i+1]; sort(b,b+n); for(int i=0;i<n;i++)id[b[i]]=i+1; update(0,1,0); ll res=0; for(int i=1;i<=n;i++){ int k=id[pref[i]]; if(pref[i]%2){//nieparzysta res=(querry(0,k,1)+querry(k+1,n,0))%M; } else{//parzysta res=(querry(0,k,0)+querry(k+1,n,1))%M; } update(k,res,pref[i]%2); } cout<<res; } //4 1000000006 1 5 1000000004 |
English