#include <bits/stdc++.h>
#define rs resize
#define sz size()
const int M=1000000007;
using namespace std;
struct st {
int s;
vector <int> t;
st() {s=0;}
st(int n) {
s=1; while (s<n) s<<=1;
t.rs(s<<1);
}
void upd(int p, int a) {
p+=s;
t[p]=a;
while (p>1) {
p>>=1;
t[p]=t[p<<1]+t[(p<<1)^1];
if (t[p]>=M) t[p]-=M;
}
}
int qry(int p, int q) {
if (p>q) return 0;
p+=s;
q+=s;
int w=t[p];
if (p!=q) {w+=t[q]; if (w>=M) w-=M;}
while ((p>>1)^(q>>1)) {
if ((p&1)^1) {w+=t[p+1]; if (w>=M) w-=M;}
if (q&1) {w+=t[q-1]; if (w>=M) w-=M;}
p>>=1;
q>>=1;
}
return w;
}
};
int fin(const vector <int> &t, int a) {
int s=1,w=-1;
while (s<(int)t.sz) s<<=1;
for (int i=s; i>0; i>>=1) {
if (w+i<(int)t.sz && t[w+i]<a) w+=i;
}
return w+1;
}
vector <int> t;
int main() {
int n;
scanf("%d",&n);
t.rs(n+1,0);
for (int i=0; i<n; i++) scanf("%d",&t[i]);
for (int i=n-1; i>=0; i--) {
t[i]+=t[i+1];
if (t[i]>=M) t[i]-=M;
}
vector <int> s=t;
sort(s.begin(),s.end());
vector <int> p(n+1);
{
vector <int> h(n+1);
iota(h.begin(),h.end(),0);
sort(h.begin(),h.end(),[](const int &a, const int &b){
return t[a] < t[b];
});
for (int i=0; i<=n; i++) p[h[i]]=i;
}
vector <st> r(2,st(n+1));
vector <int> d(n+1,0);
d[0]=1; r[t[0]&1].upd(p[0],d[0]);
for (int i=1; i<=n; i++) {
int h=fin(s,t[i]);
if (t[i]&1) {
d[i]=r[0].qry(0,h-1)+r[1].qry(h,n);
} else {
d[i]=r[1].qry(0,h-1)+r[0].qry(h,n);
}
if (d[i]>=M) d[i]-=M;
r[t[i]&1].upd(p[i],d[i]);
}
printf("%d\n",d[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 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 | #include <bits/stdc++.h> #define rs resize #define sz size() const int M=1000000007; using namespace std; struct st { int s; vector <int> t; st() {s=0;} st(int n) { s=1; while (s<n) s<<=1; t.rs(s<<1); } void upd(int p, int a) { p+=s; t[p]=a; while (p>1) { p>>=1; t[p]=t[p<<1]+t[(p<<1)^1]; if (t[p]>=M) t[p]-=M; } } int qry(int p, int q) { if (p>q) return 0; p+=s; q+=s; int w=t[p]; if (p!=q) {w+=t[q]; if (w>=M) w-=M;} while ((p>>1)^(q>>1)) { if ((p&1)^1) {w+=t[p+1]; if (w>=M) w-=M;} if (q&1) {w+=t[q-1]; if (w>=M) w-=M;} p>>=1; q>>=1; } return w; } }; int fin(const vector <int> &t, int a) { int s=1,w=-1; while (s<(int)t.sz) s<<=1; for (int i=s; i>0; i>>=1) { if (w+i<(int)t.sz && t[w+i]<a) w+=i; } return w+1; } vector <int> t; int main() { int n; scanf("%d",&n); t.rs(n+1,0); for (int i=0; i<n; i++) scanf("%d",&t[i]); for (int i=n-1; i>=0; i--) { t[i]+=t[i+1]; if (t[i]>=M) t[i]-=M; } vector <int> s=t; sort(s.begin(),s.end()); vector <int> p(n+1); { vector <int> h(n+1); iota(h.begin(),h.end(),0); sort(h.begin(),h.end(),[](const int &a, const int &b){ return t[a] < t[b]; }); for (int i=0; i<=n; i++) p[h[i]]=i; } vector <st> r(2,st(n+1)); vector <int> d(n+1,0); d[0]=1; r[t[0]&1].upd(p[0],d[0]); for (int i=1; i<=n; i++) { int h=fin(s,t[i]); if (t[i]&1) { d[i]=r[0].qry(0,h-1)+r[1].qry(h,n); } else { d[i]=r[1].qry(0,h-1)+r[0].qry(h,n); } if (d[i]>=M) d[i]-=M; r[t[i]&1].upd(p[i],d[i]); } printf("%d\n",d[n]); return 0; } |
English