#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int Q = ((int)1e9) + 7;
void add(int& x, int y) {
x += y;
if (x >= Q) x -= Q;
}
const int U = 1<<19;
int e[U+U];
int pre(int v) {
v += U;
int ret = e[v];
while(v > 1) {
if (v % 2)
add(ret, e[v-1]);
v /= 2;
}
return ret;
}
void dod(int v, int x) {
v += U;
while(v) {
add(e[v], x);
v /= 2;
}
}
int main() {
int n;
scanf("%d", &n);
vector<int> t(n), s(n+1);
for (int i = 0; i < n; i++) {
scanf("%d", &t[i]);
s[i+1] = s[i];
add(s[i+1], t[i]);
}
vector<int> ss = s;
sort(ss.begin(), ss.end());
dod(0, 1);
int ret, pa = 1, np = 0;
for (int i = 1; i <= n; i++) {
int v = std::lower_bound(ss.begin(), ss.end(), s[i]) - ss.begin();
if (s[i] % 2 == 0) {
ret = np;
add(ret, pre(v));
} else {
ret = pa;
add(ret, Q-pre(v));
}
if (s[i] % 2 == 0) {
dod(v, ret);
add(pa, ret);
} else {
dod(v, Q-ret);
add(np, ret);
}
//printf("%d :: %d\n",i, ret);
}
printf("%d\n",ret);
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 | #include <cstdio> #include <vector> #include <string> #include <algorithm> using namespace std; const int Q = ((int)1e9) + 7; void add(int& x, int y) { x += y; if (x >= Q) x -= Q; } const int U = 1<<19; int e[U+U]; int pre(int v) { v += U; int ret = e[v]; while(v > 1) { if (v % 2) add(ret, e[v-1]); v /= 2; } return ret; } void dod(int v, int x) { v += U; while(v) { add(e[v], x); v /= 2; } } int main() { int n; scanf("%d", &n); vector<int> t(n), s(n+1); for (int i = 0; i < n; i++) { scanf("%d", &t[i]); s[i+1] = s[i]; add(s[i+1], t[i]); } vector<int> ss = s; sort(ss.begin(), ss.end()); dod(0, 1); int ret, pa = 1, np = 0; for (int i = 1; i <= n; i++) { int v = std::lower_bound(ss.begin(), ss.end(), s[i]) - ss.begin(); if (s[i] % 2 == 0) { ret = np; add(ret, pre(v)); } else { ret = pa; add(ret, Q-pre(v)); } if (s[i] % 2 == 0) { dod(v, ret); add(pa, ret); } else { dod(v, Q-ret); add(np, ret); } //printf("%d :: %d\n",i, ret); } printf("%d\n",ret); return 0; } |
English