#include <algorithm> #include <iostream> using namespace std; const int MAXN = 300000; const int MOD = 1000000007; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) struct Node { int a, b; Node * L, *R; int v; int s[2]; void init() { v = 0; s[0] = s[1] = 0; L = R = nullptr; } Node(int a, int b): a(a), b(b) { init(); } Node(): a(0), b(MOD) { init(); } int mid() const { return (b-a)/2 + a; } Node * getL() { if(L == nullptr) L = new Node(a, mid()); return L; } Node * getR() { if(R == nullptr) R = new Node(mid(), b); return R; } void add(int p, int val) { v = (v + val)%MOD; s[p%2] = (s[p%2] + val)%MOD; if(b-a > 1) { if(p < mid()) { getL()->add(p, val); } else { getR()->add(p, val); } } } int total(int a1, int b1, int p) { if(a1<=a && b1 >=b) return s[p]; if(a1 >= b || b1 <= a) return 0; else { int sum = 0; if (L) sum += L->total(a1, b1, p); if (R) sum += R->total(a1, b1, p); return sum%MOD; } } }; int T[MAXN]; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; FOR(i, n) { cin >> T[i]; } Node root; root.add(0, 1); int sum = 0; int ile; FOR(i, n) { sum += T[i]; sum %= MOD; ile = (root.total(0, MOD-sum, sum%2) + root.total(MOD-sum, MOD, !(sum%2)))%MOD; root.add(MOD-sum, ile); } cout << ile << endl; }
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 | #include <algorithm> #include <iostream> using namespace std; const int MAXN = 300000; const int MOD = 1000000007; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) struct Node { int a, b; Node * L, *R; int v; int s[2]; void init() { v = 0; s[0] = s[1] = 0; L = R = nullptr; } Node(int a, int b): a(a), b(b) { init(); } Node(): a(0), b(MOD) { init(); } int mid() const { return (b-a)/2 + a; } Node * getL() { if(L == nullptr) L = new Node(a, mid()); return L; } Node * getR() { if(R == nullptr) R = new Node(mid(), b); return R; } void add(int p, int val) { v = (v + val)%MOD; s[p%2] = (s[p%2] + val)%MOD; if(b-a > 1) { if(p < mid()) { getL()->add(p, val); } else { getR()->add(p, val); } } } int total(int a1, int b1, int p) { if(a1<=a && b1 >=b) return s[p]; if(a1 >= b || b1 <= a) return 0; else { int sum = 0; if (L) sum += L->total(a1, b1, p); if (R) sum += R->total(a1, b1, p); return sum%MOD; } } }; int T[MAXN]; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; FOR(i, n) { cin >> T[i]; } Node root; root.add(0, 1); int sum = 0; int ile; FOR(i, n) { sum += T[i]; sum %= MOD; ile = (root.total(0, MOD-sum, sum%2) + root.total(MOD-sum, MOD, !(sum%2)))%MOD; root.add(MOD-sum, ile); } cout << ile << endl; } |