#include <iostream> #include <vector> #include <unordered_map> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; ll mod = 1e9+7; ll calculate_shift(ll val, ll shift){ return (shift+val)%mod; } ll pos_after_shift(ll val, ll shift){ ll res = (val-shift)%mod; if(res<0) res+=mod; return res; } pll calculate_zeros(ll val, pll zeros){ zeros.first = (zeros.first - val)%mod; zeros.second = (zeros.first-1)%mod; if(zeros.first<0) zeros.first+=mod; if(zeros.second<0) zeros.second+=mod; return zeros; } //const int N = 10e9 + 7; int n = 1<<30; unordered_map <ll, ll> even; unordered_map <ll, ll> odd; void modify(unordered_map <ll, ll> &t, ll p, ll value) { // set value at position p //cout<< "P: " << p << " V: " << value << "\n"; for (t[p += n] = value%mod; p > 1; p >>= 1) t[p>>1] = (t[p] + t[p^1])%mod; } ll query(unordered_map <ll, ll> &t, ll l, ll r) { // sum on interval [l, r) r++; //cout<< "L: " << l << " R: " << r << "\n"; int res = 0; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l&1) res += t[l++]; res %= mod; if (r&1) res += t[--r]; res %= mod; } if(res<0){ res+=mod; } return res%mod; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int m; cin >> m; vector<ll> a; vector<ll> ans; ans.push_back(1); for(int i=0; i<m; i++){ ll x; cin >> x; a.push_back(x); } ll shift = 0; pll zeros = {0, mod-1}; for(int i=0; i<m; i++){ shift = calculate_shift(a[i], shift); long long new_pos = pos_after_shift(a[i], shift); //cout << "NP: " <<new_pos <<"\n"; if(new_pos%2==0){ ll cur = query(even, new_pos/2, (new_pos/2)); //cout << cur << " "; modify(even, new_pos/2, cur + ans[i]); //cout << query(even, new_pos/2, (new_pos/2)) << "\n"; //cout << "E\n"; }else if(new_pos%2==1){ ll cur = query(odd, new_pos/2, (new_pos/2)); //cout << cur << " "; modify(odd, new_pos/2, cur + ans[i]); //cout << query(odd, new_pos/2, (new_pos/2)) << "\n"; //cout << "O\n"; } zeros = calculate_zeros(a[i], zeros); //cout << "Z: " <<zeros.first << " " << zeros.second <<"\n"; // for(int j=1; j<=8; j++){ // for(int k=1<<(j-1); k<1<<j; k++){ // cout << even[k] << " "; // } // cout << "\n"; // } if(zeros.first==0){ //cout << "EE\n"; ll res = query(even, zeros.first, zeros.second); ans.push_back(res); }else if(zeros.first%2==0){ //cout << "OE\n"; ll res = query(even, zeros.first/2, n-1); res += query(odd, 0, zeros.second/2); res %= mod; ans.push_back(res); }else if(zeros.first%2==1){ //cout << "EO\n"; ll res = query(odd, zeros.first/2, n-1); res += query(even, 0, zeros.second/2); res %= mod; ans.push_back(res); } } // for(int i=1; i<=m; i++){ // cout << ans[i] << " "; // } cout << ans.back() << "\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 | #include <iostream> #include <vector> #include <unordered_map> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; ll mod = 1e9+7; ll calculate_shift(ll val, ll shift){ return (shift+val)%mod; } ll pos_after_shift(ll val, ll shift){ ll res = (val-shift)%mod; if(res<0) res+=mod; return res; } pll calculate_zeros(ll val, pll zeros){ zeros.first = (zeros.first - val)%mod; zeros.second = (zeros.first-1)%mod; if(zeros.first<0) zeros.first+=mod; if(zeros.second<0) zeros.second+=mod; return zeros; } //const int N = 10e9 + 7; int n = 1<<30; unordered_map <ll, ll> even; unordered_map <ll, ll> odd; void modify(unordered_map <ll, ll> &t, ll p, ll value) { // set value at position p //cout<< "P: " << p << " V: " << value << "\n"; for (t[p += n] = value%mod; p > 1; p >>= 1) t[p>>1] = (t[p] + t[p^1])%mod; } ll query(unordered_map <ll, ll> &t, ll l, ll r) { // sum on interval [l, r) r++; //cout<< "L: " << l << " R: " << r << "\n"; int res = 0; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l&1) res += t[l++]; res %= mod; if (r&1) res += t[--r]; res %= mod; } if(res<0){ res+=mod; } return res%mod; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int m; cin >> m; vector<ll> a; vector<ll> ans; ans.push_back(1); for(int i=0; i<m; i++){ ll x; cin >> x; a.push_back(x); } ll shift = 0; pll zeros = {0, mod-1}; for(int i=0; i<m; i++){ shift = calculate_shift(a[i], shift); long long new_pos = pos_after_shift(a[i], shift); //cout << "NP: " <<new_pos <<"\n"; if(new_pos%2==0){ ll cur = query(even, new_pos/2, (new_pos/2)); //cout << cur << " "; modify(even, new_pos/2, cur + ans[i]); //cout << query(even, new_pos/2, (new_pos/2)) << "\n"; //cout << "E\n"; }else if(new_pos%2==1){ ll cur = query(odd, new_pos/2, (new_pos/2)); //cout << cur << " "; modify(odd, new_pos/2, cur + ans[i]); //cout << query(odd, new_pos/2, (new_pos/2)) << "\n"; //cout << "O\n"; } zeros = calculate_zeros(a[i], zeros); //cout << "Z: " <<zeros.first << " " << zeros.second <<"\n"; // for(int j=1; j<=8; j++){ // for(int k=1<<(j-1); k<1<<j; k++){ // cout << even[k] << " "; // } // cout << "\n"; // } if(zeros.first==0){ //cout << "EE\n"; ll res = query(even, zeros.first, zeros.second); ans.push_back(res); }else if(zeros.first%2==0){ //cout << "OE\n"; ll res = query(even, zeros.first/2, n-1); res += query(odd, 0, zeros.second/2); res %= mod; ans.push_back(res); }else if(zeros.first%2==1){ //cout << "EO\n"; ll res = query(odd, zeros.first/2, n-1); res += query(even, 0, zeros.second/2); res %= mod; ans.push_back(res); } } // for(int i=1; i<=m; i++){ // cout << ans[i] << " "; // } cout << ans.back() << "\n"; } |