#include<bits/stdc++.h> using namespace std; int MOD = 1e9 + 7; class Node{ public: int left = -1; int right = -1; int l; int r; int val; }; class Tree{ private: vector<Node> v; int l, r; int createNode(int L, int R){ Node node; node.l = L; node.r = R; node.val = 0; v.push_back(node); //cout<<"Created "<<v.size()<<"\n"; return (v.size() -1); } void addNumber(int pos, int current, int number){ //cout<<v[current].l<<" "<<v[current].r<<"\n"; if(v[current].l == v[current].r){ v[current].val = (v[current].val + number) % MOD; }else{ int m = (v[current].l + v[current].r) / 2; if(v[current].left == -1){ int x = createNode(v[current].l, m); v[current].left = x; } if(v[current].right == -1){ int x = createNode(m+1, v[current].r); v[current].right = x; } if(pos <= m){ addNumber(pos, v[current].left, number); }else{ addNumber(pos, v[current].right, number); } v[current].val = (v[v[current].left].val + v[v[current].right].val) % MOD; } } int rangeSum(int a, int b, int current){ if(a <= v[current].l && v[current].r <= b){ return v[current].val; }else if(a > v[current].r || b < v[current].l){ return 0; }else{ int m = (v[current].l + v[current].r) / 2; if(v[current].left == -1){ int x = createNode(v[current].l, m); v[current].left = x; } if(v[current].right == -1){ int x = createNode(m+1, v[current].r); v[current].right = x; } return (rangeSum(a, b, v[current].left) + rangeSum(a, b, v[current].right))% MOD; } } public: Tree(int size){ l = 0; int k = 1; while(k < size)k*= 2; r = k-1; Node node; node.l = l; node.r = r; node.val = 0; v.push_back(node); } void add(int pos, int val){ addNumber(pos, 0, val); } int getSum(int a, int b){ return rangeSum(a, b, 0); } }; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); srand(time(NULL)); int n; cin>>n; vector<int> v(n, 0); for(int i = 0; i < n; i++)cin>>v[i]; Tree odd(MOD); Tree even(MOD); even.add(0, 1); int shift = 0; long long res = 0; for(int i = 0; i < n; i++){ int a = v[i]; int good = 0; int a1 = (a - shift) % 2; if(a1 < 0)a1 += 2; if(a1 == 0){ good += even.getSum(0, MOD - a - 1 - shift); good %= MOD; good += even.getSum(MOD - shift, MOD - 1); good %= MOD; good += odd.getSum(max(MOD - a- shift, 0), MOD - 1 - shift); good %= MOD; good += odd.getSum(2*MOD - a - shift, MOD - 1); good %= MOD; }else{ good += odd.getSum(0, MOD - a - 1 - shift); good %= MOD; good += odd.getSum(MOD - shift, MOD - 1); good %= MOD; good += even.getSum(max(MOD - a- shift, 0), MOD - 1 - shift); good %= MOD; good += even.getSum(2*MOD - a - shift, MOD - 1); good %= MOD; } shift = (shift + a) % MOD; if(shift == 0){ even.add(0, good); }else if(shift % 2 == 0){ odd.add(MOD - shift, good); }else{ even.add(MOD - shift, good); } res = good; } cout<<res<<"\n"; }
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 | #include<bits/stdc++.h> using namespace std; int MOD = 1e9 + 7; class Node{ public: int left = -1; int right = -1; int l; int r; int val; }; class Tree{ private: vector<Node> v; int l, r; int createNode(int L, int R){ Node node; node.l = L; node.r = R; node.val = 0; v.push_back(node); //cout<<"Created "<<v.size()<<"\n"; return (v.size() -1); } void addNumber(int pos, int current, int number){ //cout<<v[current].l<<" "<<v[current].r<<"\n"; if(v[current].l == v[current].r){ v[current].val = (v[current].val + number) % MOD; }else{ int m = (v[current].l + v[current].r) / 2; if(v[current].left == -1){ int x = createNode(v[current].l, m); v[current].left = x; } if(v[current].right == -1){ int x = createNode(m+1, v[current].r); v[current].right = x; } if(pos <= m){ addNumber(pos, v[current].left, number); }else{ addNumber(pos, v[current].right, number); } v[current].val = (v[v[current].left].val + v[v[current].right].val) % MOD; } } int rangeSum(int a, int b, int current){ if(a <= v[current].l && v[current].r <= b){ return v[current].val; }else if(a > v[current].r || b < v[current].l){ return 0; }else{ int m = (v[current].l + v[current].r) / 2; if(v[current].left == -1){ int x = createNode(v[current].l, m); v[current].left = x; } if(v[current].right == -1){ int x = createNode(m+1, v[current].r); v[current].right = x; } return (rangeSum(a, b, v[current].left) + rangeSum(a, b, v[current].right))% MOD; } } public: Tree(int size){ l = 0; int k = 1; while(k < size)k*= 2; r = k-1; Node node; node.l = l; node.r = r; node.val = 0; v.push_back(node); } void add(int pos, int val){ addNumber(pos, 0, val); } int getSum(int a, int b){ return rangeSum(a, b, 0); } }; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); srand(time(NULL)); int n; cin>>n; vector<int> v(n, 0); for(int i = 0; i < n; i++)cin>>v[i]; Tree odd(MOD); Tree even(MOD); even.add(0, 1); int shift = 0; long long res = 0; for(int i = 0; i < n; i++){ int a = v[i]; int good = 0; int a1 = (a - shift) % 2; if(a1 < 0)a1 += 2; if(a1 == 0){ good += even.getSum(0, MOD - a - 1 - shift); good %= MOD; good += even.getSum(MOD - shift, MOD - 1); good %= MOD; good += odd.getSum(max(MOD - a- shift, 0), MOD - 1 - shift); good %= MOD; good += odd.getSum(2*MOD - a - shift, MOD - 1); good %= MOD; }else{ good += odd.getSum(0, MOD - a - 1 - shift); good %= MOD; good += odd.getSum(MOD - shift, MOD - 1); good %= MOD; good += even.getSum(max(MOD - a- shift, 0), MOD - 1 - shift); good %= MOD; good += even.getSum(2*MOD - a - shift, MOD - 1); good %= MOD; } shift = (shift + a) % MOD; if(shift == 0){ even.add(0, good); }else if(shift % 2 == 0){ odd.add(MOD - shift, good); }else{ even.add(MOD - shift, good); } res = good; } cout<<res<<"\n"; } |