#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;
#define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b))
const int MOD = 1e9+7;
struct SegmentTree {
int M;
vector<int> t;
SegmentTree(int n) {
M = 1;
while (M <= n) {
M *= 2;
}
t = vector<int>(2*M, 0);
}
void add(int x, int val) {
x += M;
t[x] += val;
t[x] %= MOD;
while (x > 1) {
x /= 2;
t[x] = t[2*x] + t[2*x+1];
t[x] %= MOD;
}
}
int sum(int x, int y) {
if (x > y) return 0;
x += M;
y += M;
int res = t[x];
if (x != y) res += t[y];
while (x/2 != y/2) {
if (x%2 == 0) {
res += t[x+1];
res %= MOD;
}
if (y%2 == 1) {
res += t[y-1];
res %= MOD;
}
x /= 2;
y /= 2;
}
return res;
}
};
int main() {
int n;
scanf("%d", &n);
vector<int> a(n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
set<int> rests = {0};
int pref = 0;
for (int i = 0; i < n; ++i) {
pref += a[i];
pref %= MOD;
rests.insert(pref);
}
map<int, int> ord;
int cur_ord = 0;
for (auto it = rests.begin(); it != rests.end(); ++it) {
ord[*it] = cur_ord;
++cur_ord;
}
auto evens = SegmentTree(rests.size()+10);
auto odds = SegmentTree(rests.size()+10);
evens.add(0, 1); // empty segment
int r = 0;
for (int i = 1; i <= n; ++i) {
r = (r + a[i-1]) % MOD;
int here = 0;
int idx = ord[r];
if (r % 2 == 0) {
here = evens.sum(0, idx);
here += odds.sum(idx+1, odds.M-1);
here %= MOD;
evens.add(idx, here);
} else {
here = odds.sum(0, idx);
here += evens.sum(idx+1, odds.M-1);
here %= MOD;
odds.add(idx, here);
}
if (i == n) {
printf("%d\n", here);
}
}
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 | #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) const int MOD = 1e9+7; struct SegmentTree { int M; vector<int> t; SegmentTree(int n) { M = 1; while (M <= n) { M *= 2; } t = vector<int>(2*M, 0); } void add(int x, int val) { x += M; t[x] += val; t[x] %= MOD; while (x > 1) { x /= 2; t[x] = t[2*x] + t[2*x+1]; t[x] %= MOD; } } int sum(int x, int y) { if (x > y) return 0; x += M; y += M; int res = t[x]; if (x != y) res += t[y]; while (x/2 != y/2) { if (x%2 == 0) { res += t[x+1]; res %= MOD; } if (y%2 == 1) { res += t[y-1]; res %= MOD; } x /= 2; y /= 2; } return res; } }; int main() { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } set<int> rests = {0}; int pref = 0; for (int i = 0; i < n; ++i) { pref += a[i]; pref %= MOD; rests.insert(pref); } map<int, int> ord; int cur_ord = 0; for (auto it = rests.begin(); it != rests.end(); ++it) { ord[*it] = cur_ord; ++cur_ord; } auto evens = SegmentTree(rests.size()+10); auto odds = SegmentTree(rests.size()+10); evens.add(0, 1); // empty segment int r = 0; for (int i = 1; i <= n; ++i) { r = (r + a[i-1]) % MOD; int here = 0; int idx = ord[r]; if (r % 2 == 0) { here = evens.sum(0, idx); here += odds.sum(idx+1, odds.M-1); here %= MOD; evens.add(idx, here); } else { here = odds.sum(0, idx); here += evens.sum(idx+1, odds.M-1); here %= MOD; odds.add(idx, here); } if (i == n) { printf("%d\n", here); } } return 0; } |
English