#include<bits/stdc++.h> using namespace std; const int M=1e9+7; int A[(int)3e5+5]; long long S[(int)3e5+5]; vector<pair<int,int> > P,NP; long long T[3][(int)1e6+6]; struct seqtree{ int siz3=1; void init(int n){ while(siz3<n)siz3*=2; } int typ; void add(int x,int lx, int rx, int v,long long va){ if(rx-lx==1)T[typ][x]=va; else{ int m=(lx+rx)/2; if(v<m)add(2*x+1,lx,m,v,va); else add(2*x+2,m,rx,v,va); T[typ][x]=T[typ][2*x+1]+T[typ][2*x+2]; } } long long suum(int x, int lx, int rx, int l, int r){ if(l>=rx or r<=lx)return 0; if(lx>=l and rx<=r)return T[typ][x]; int m=(lx+rx)/2; long long C=suum(2*x+1,lx,m,l,r); long long B=suum(2*x+2,m,rx,l,r); C+=B;C=(C+M)%M; return C; } }; pair<int,int> G[(int)3e5+5]; /////typ/m-ce seqtree st1,st2; long long licz1(long long zero){ long long Q=0; //<0,M) w parzystych //szukaj zero int p=-1;int k=P.size(); while(k-p>1){ int m=(p+k)/2; if(P[m].first>=zero)k=m; else p=m; } int low=k; long long w=zero+M;w=(w+2*M)%(2*M); p=-1;k=P.size(); while(k-p>1){ int m=(p+k)/2; if(P[m].first<w)p=m; else k=m; } int high=p; if(high>=low)Q+=st1.suum(0,0,st1.siz3,low,high+1); else{ if(low!=P.size() and P[low].first<zero+M)Q+=st1.suum(0,0,st1.siz3,low,st1.siz3); if(high!=-1 and (zero+M>=2*M or P[high].first>=zero))Q+=st1.suum(0,0,st1.siz3,0,high+1); } //<M,2*M) w nieparzystych zero+=M;zero=(zero+2*M)%(2*M); p=-1;k=NP.size(); while(k-p>1){ int m=(p+k)/2; if(NP[m].first>=zero)k=m; else p=m; } low=k; w=zero+M;w=(w+2*M)%(2*M); p=-1;k=NP.size(); while(k-p>1){ int m=(p+k)/2; if(NP[m].first<w)p=m; else k=m; } high=p; if(high>=low)Q+=st2.suum(0,0,st2.siz3,low,high+1); else{ if(low!=NP.size() and NP[low].first<zero+M)Q+=st2.suum(0,0,st2.siz3,low,st2.siz3);; if(high!=-1 and (zero+M>=2*M or NP[high].first>=zero))Q+=st2.suum(0,0,st2.siz3,0,high+1); } return Q; } long long licz2(long long zero){ long long Q=0; int p=-1;int k=NP.size(); while(k-p>1){ int m=(p+k)/2; if(NP[m].first>=zero)k=m; else p=m; } int low=k; long long w=zero+M;w=(w+2*M)%(2*M); p=-1;k=NP.size(); while(k-p>1){ int m=(p+k)/2; if(NP[m].first<w)p=m; else k=m; } int high=p; if(high>=low)Q+=st2.suum(0,0,st2.siz3,low,high+1); else{ if(low!=NP.size() and NP[low].first<zero+M)Q+=st2.suum(0,0,st2.siz3,low,st2.siz3); if(high!=-1 and (zero+M>=2*M or NP[high].first>=zero))Q+=st2.suum(0,0,st2.siz3,0,high+1); } zero+=M;zero=(zero+2*M)%(2*M); p=-1;k=P.size(); while(k-p>1){ int m=(p+k)/2; if(P[m].first>=zero)k=m; else p=m; } low=k; w=zero+M;w=(w+2*M)%(2*M); p=-1;k=P.size(); while(k-p>1){ int m=(p+k)/2; if(P[m].first<w)p=m; else k=m; } high=p; if(high>=low)Q+=st1.suum(0,0,st1.siz3,low,high+1); else{ if(low!=P.size() and P[low].first<zero+M)Q+=st1.suum(0,0,st1.siz3,low,st1.siz3); if(high!=-1 and (zero+M>=2*M or P[high].first>=zero))Q+=st1.suum(0,0,st1.siz3,0,high+1); } return Q; } int main(){ ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); int n;cin>>n; P.push_back({0,0}); for(int i=1;i<=n;i++){ cin>>A[i]; S[i]=S[i-1]; S[i]+=A[i]; S[i]=(S[i]+2*M)%(2*M); if(S[i]%2==0)P.push_back({S[i],i}); else NP.push_back({S[i],i}); } sort(P.begin(),P.end()); for(int i=0;i<P.size();i++){ G[P[i].second]={1,i}; } sort(NP.begin(),NP.end()); for(int i=0;i<NP.size();i++){ G[NP[i].second]={2,i}; } st1.typ=1;st2.typ=2; st1.init(P.size());st2.init(NP.size()); if(G[n].first==1){ st1.add(0,0,st1.siz3,G[n].second,1); } else st2.add(0,0,st2.siz3,G[n].second,1); for(int i=n;i>0;i--){ long long Q=0; if(S[i-1]%2==0){ Q=licz1(S[i-1]);Q=(Q+M)%M; st1.add(0,0,st1.siz3,G[i-1].second,Q); } else{ Q=licz2(S[i-1]); Q=(Q+M)%M; st2.add(0,0,st2.siz3,G[i-1].second,Q); } if(i==1)cout<<Q; } 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 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | #include<bits/stdc++.h> using namespace std; const int M=1e9+7; int A[(int)3e5+5]; long long S[(int)3e5+5]; vector<pair<int,int> > P,NP; long long T[3][(int)1e6+6]; struct seqtree{ int siz3=1; void init(int n){ while(siz3<n)siz3*=2; } int typ; void add(int x,int lx, int rx, int v,long long va){ if(rx-lx==1)T[typ][x]=va; else{ int m=(lx+rx)/2; if(v<m)add(2*x+1,lx,m,v,va); else add(2*x+2,m,rx,v,va); T[typ][x]=T[typ][2*x+1]+T[typ][2*x+2]; } } long long suum(int x, int lx, int rx, int l, int r){ if(l>=rx or r<=lx)return 0; if(lx>=l and rx<=r)return T[typ][x]; int m=(lx+rx)/2; long long C=suum(2*x+1,lx,m,l,r); long long B=suum(2*x+2,m,rx,l,r); C+=B;C=(C+M)%M; return C; } }; pair<int,int> G[(int)3e5+5]; /////typ/m-ce seqtree st1,st2; long long licz1(long long zero){ long long Q=0; //<0,M) w parzystych //szukaj zero int p=-1;int k=P.size(); while(k-p>1){ int m=(p+k)/2; if(P[m].first>=zero)k=m; else p=m; } int low=k; long long w=zero+M;w=(w+2*M)%(2*M); p=-1;k=P.size(); while(k-p>1){ int m=(p+k)/2; if(P[m].first<w)p=m; else k=m; } int high=p; if(high>=low)Q+=st1.suum(0,0,st1.siz3,low,high+1); else{ if(low!=P.size() and P[low].first<zero+M)Q+=st1.suum(0,0,st1.siz3,low,st1.siz3); if(high!=-1 and (zero+M>=2*M or P[high].first>=zero))Q+=st1.suum(0,0,st1.siz3,0,high+1); } //<M,2*M) w nieparzystych zero+=M;zero=(zero+2*M)%(2*M); p=-1;k=NP.size(); while(k-p>1){ int m=(p+k)/2; if(NP[m].first>=zero)k=m; else p=m; } low=k; w=zero+M;w=(w+2*M)%(2*M); p=-1;k=NP.size(); while(k-p>1){ int m=(p+k)/2; if(NP[m].first<w)p=m; else k=m; } high=p; if(high>=low)Q+=st2.suum(0,0,st2.siz3,low,high+1); else{ if(low!=NP.size() and NP[low].first<zero+M)Q+=st2.suum(0,0,st2.siz3,low,st2.siz3);; if(high!=-1 and (zero+M>=2*M or NP[high].first>=zero))Q+=st2.suum(0,0,st2.siz3,0,high+1); } return Q; } long long licz2(long long zero){ long long Q=0; int p=-1;int k=NP.size(); while(k-p>1){ int m=(p+k)/2; if(NP[m].first>=zero)k=m; else p=m; } int low=k; long long w=zero+M;w=(w+2*M)%(2*M); p=-1;k=NP.size(); while(k-p>1){ int m=(p+k)/2; if(NP[m].first<w)p=m; else k=m; } int high=p; if(high>=low)Q+=st2.suum(0,0,st2.siz3,low,high+1); else{ if(low!=NP.size() and NP[low].first<zero+M)Q+=st2.suum(0,0,st2.siz3,low,st2.siz3); if(high!=-1 and (zero+M>=2*M or NP[high].first>=zero))Q+=st2.suum(0,0,st2.siz3,0,high+1); } zero+=M;zero=(zero+2*M)%(2*M); p=-1;k=P.size(); while(k-p>1){ int m=(p+k)/2; if(P[m].first>=zero)k=m; else p=m; } low=k; w=zero+M;w=(w+2*M)%(2*M); p=-1;k=P.size(); while(k-p>1){ int m=(p+k)/2; if(P[m].first<w)p=m; else k=m; } high=p; if(high>=low)Q+=st1.suum(0,0,st1.siz3,low,high+1); else{ if(low!=P.size() and P[low].first<zero+M)Q+=st1.suum(0,0,st1.siz3,low,st1.siz3); if(high!=-1 and (zero+M>=2*M or P[high].first>=zero))Q+=st1.suum(0,0,st1.siz3,0,high+1); } return Q; } int main(){ ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); int n;cin>>n; P.push_back({0,0}); for(int i=1;i<=n;i++){ cin>>A[i]; S[i]=S[i-1]; S[i]+=A[i]; S[i]=(S[i]+2*M)%(2*M); if(S[i]%2==0)P.push_back({S[i],i}); else NP.push_back({S[i],i}); } sort(P.begin(),P.end()); for(int i=0;i<P.size();i++){ G[P[i].second]={1,i}; } sort(NP.begin(),NP.end()); for(int i=0;i<NP.size();i++){ G[NP[i].second]={2,i}; } st1.typ=1;st2.typ=2; st1.init(P.size());st2.init(NP.size()); if(G[n].first==1){ st1.add(0,0,st1.siz3,G[n].second,1); } else st2.add(0,0,st2.siz3,G[n].second,1); for(int i=n;i>0;i--){ long long Q=0; if(S[i-1]%2==0){ Q=licz1(S[i-1]);Q=(Q+M)%M; st1.add(0,0,st1.siz3,G[i-1].second,Q); } else{ Q=licz2(S[i-1]); Q=(Q+M)%M; st2.add(0,0,st2.siz3,G[i-1].second,Q); } if(i==1)cout<<Q; } return 0; } |