#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//#define DEBUG
long long A[600000], B[600000];
long long TA[1100000], TB[1100000];
long long MOD = 1000000007;
long long PS[600000];
int NZB[600000];
int NOA[600000];
int D = 1;
long long get_from_tree(long long result, int l, int r) {
vector<int> indicesL, indicesR;
l += D;
r += D;
while (l < r) {
if (l % 2) indicesL.push_back(l);
if (r % 2 == 0) indicesR.push_back(r);
l = (l+1)/2;
r = (r-1)/2;
}
if (l == r) indicesL.push_back(l);
for (int i = 0; i < indicesL.size(); i++) {
result = (result * TA[indicesL[i]]) % MOD;
result = (result + TB[indicesL[i]]) % MOD;
#ifdef DEBUG
cout << indices[i] << " (" << TA[indices[i]] << " " << TB[indices[i]] << ")" << endl;
#endif // DEBUG
}
for (int i = indicesR.size()-1; i >= 0; i--) {
result = (result * TA[indicesR[i]]) % MOD;
result = (result + TB[indicesR[i]]) % MOD;
#ifdef DEBUG
cout << indices[i] << " (" << TA[indices[i]] << " " << TB[indices[i]] << ")" << endl;
#endif // DEBUG
}
return result;
}
long long solve(int x, int l, int r) {
if (x == 0) {
if (NZB[l] == -1) {
return 0;
}
// We need a non-zero B, cos multiplying by anything gets us 0
l = NZB[l];
}
long long result = x;
bool exceeded = false;
for (int i = l; i <= r; i++) {
if (exceeded) {
return get_from_tree(result, i, r);
}
if (A[i] == 1) {
// jump to the first non-one A
int noa = NOA[i];
if (!noa || noa > r) {
noa = r+1;
}
result = (result + PS[noa - 1] - (i > 0 ? PS[i - 1] : 0));
if (result >= MOD) {
exceeded = true;
result %= MOD;
}
i = noa - 1;
#ifdef DEBUG
cout << "UNO! JUMPED TO " << noa << " AND ADDED " << PS[noa - 1] - PS[i - 1] << endl;
#endif // DEBUG
} else {
long long add = result + B[i];
long long multiply = result * A[i];
if (add >= MOD || multiply >= MOD) {
exceeded = true;
}
result = max(add, multiply) % MOD;
#ifdef DEBUG
cout << "DOS! CHOICE BETWEEN " << add << " AND " << multiply << endl;
#endif // DEBUG
}
}
return result;
}
int main() {
ios_base::sync_with_stdio(0);
int n,q;
cin >> n >> q;
while (D < n) D <<= 1;
for (int i = 0; i < n; i++) {
cin >> B[i] >> A[i];
if (A[i] == 1) {
TA[D+i] = 1;
TB[D+i] = B[i];
PS[i] = i == 0 ? B[i] : B[i] + PS[i-1];
} else {
TA[D+i] = A[i];
TB[D+i] = 0;
PS[i] = 0;
}
}
#ifdef DEBUG
cout << "TA: ";
for (int i = 0; i < n; i++) {
cout << TA[i] << " ";
}
cout << endl;
cout << endl << "TB: ";
for (int i = 0; i < n; i++) {
cout << TB[i] << " ";
}
cout << endl;
cout << endl << "PS: ";
for (int i = 0; i < n; i++) {
cout << PS[i] << " ";
}
cout << endl;
#endif // DEBUG
for (int i = D-1; i > 0; i--) {
TA[i] = TA[i*2] * TA[i*2+1] % MOD;
TB[i] = (TB[i*2] * TA[i*2+1] % MOD + TB[i*2+1]) % MOD;
}
#ifdef DEBUG
cout << endl << "TREE" << endl;
for (int i = 1; i < D + n; i++) {
cout << "(" << TA[i] << " " << TB[i] << ")";
if (i == 1 || i == 3 || i == 7 || i == 15) cout << endl;
}
cout << endl;
#endif // DEBUG
int nzb = -1;
for (int i = n-1; i >= 0; i--) {
if (B[i]) {
nzb = i;
}
NZB[i] = nzb;
}
#ifdef DEBUG
cout << "NZB: ";
for (int i = 0; i < n; i++) {
cout << NZB[i] << " ";
}
cout << endl;
#endif // DEBUG
int noa = 0;
for (int i = n-1; i >= 0; i--) {
if (A[i] != 1) {
noa = i;
}
NOA[i] = noa;
}
#ifdef DEBUG
cout << "NOA: ";
for (int i = 0; i < n; i++) {
cout << NOA[i] << " ";
}
cout << endl;
#endif // DEBUG
for (int i = 0; i < q; i++) {
int x,l,r;
cin >> x >> l >> r;
cout << solve(x,l,r-1) << 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 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | #include <iostream> #include <vector> #include <algorithm> using namespace std; //#define DEBUG long long A[600000], B[600000]; long long TA[1100000], TB[1100000]; long long MOD = 1000000007; long long PS[600000]; int NZB[600000]; int NOA[600000]; int D = 1; long long get_from_tree(long long result, int l, int r) { vector<int> indicesL, indicesR; l += D; r += D; while (l < r) { if (l % 2) indicesL.push_back(l); if (r % 2 == 0) indicesR.push_back(r); l = (l+1)/2; r = (r-1)/2; } if (l == r) indicesL.push_back(l); for (int i = 0; i < indicesL.size(); i++) { result = (result * TA[indicesL[i]]) % MOD; result = (result + TB[indicesL[i]]) % MOD; #ifdef DEBUG cout << indices[i] << " (" << TA[indices[i]] << " " << TB[indices[i]] << ")" << endl; #endif // DEBUG } for (int i = indicesR.size()-1; i >= 0; i--) { result = (result * TA[indicesR[i]]) % MOD; result = (result + TB[indicesR[i]]) % MOD; #ifdef DEBUG cout << indices[i] << " (" << TA[indices[i]] << " " << TB[indices[i]] << ")" << endl; #endif // DEBUG } return result; } long long solve(int x, int l, int r) { if (x == 0) { if (NZB[l] == -1) { return 0; } // We need a non-zero B, cos multiplying by anything gets us 0 l = NZB[l]; } long long result = x; bool exceeded = false; for (int i = l; i <= r; i++) { if (exceeded) { return get_from_tree(result, i, r); } if (A[i] == 1) { // jump to the first non-one A int noa = NOA[i]; if (!noa || noa > r) { noa = r+1; } result = (result + PS[noa - 1] - (i > 0 ? PS[i - 1] : 0)); if (result >= MOD) { exceeded = true; result %= MOD; } i = noa - 1; #ifdef DEBUG cout << "UNO! JUMPED TO " << noa << " AND ADDED " << PS[noa - 1] - PS[i - 1] << endl; #endif // DEBUG } else { long long add = result + B[i]; long long multiply = result * A[i]; if (add >= MOD || multiply >= MOD) { exceeded = true; } result = max(add, multiply) % MOD; #ifdef DEBUG cout << "DOS! CHOICE BETWEEN " << add << " AND " << multiply << endl; #endif // DEBUG } } return result; } int main() { ios_base::sync_with_stdio(0); int n,q; cin >> n >> q; while (D < n) D <<= 1; for (int i = 0; i < n; i++) { cin >> B[i] >> A[i]; if (A[i] == 1) { TA[D+i] = 1; TB[D+i] = B[i]; PS[i] = i == 0 ? B[i] : B[i] + PS[i-1]; } else { TA[D+i] = A[i]; TB[D+i] = 0; PS[i] = 0; } } #ifdef DEBUG cout << "TA: "; for (int i = 0; i < n; i++) { cout << TA[i] << " "; } cout << endl; cout << endl << "TB: "; for (int i = 0; i < n; i++) { cout << TB[i] << " "; } cout << endl; cout << endl << "PS: "; for (int i = 0; i < n; i++) { cout << PS[i] << " "; } cout << endl; #endif // DEBUG for (int i = D-1; i > 0; i--) { TA[i] = TA[i*2] * TA[i*2+1] % MOD; TB[i] = (TB[i*2] * TA[i*2+1] % MOD + TB[i*2+1]) % MOD; } #ifdef DEBUG cout << endl << "TREE" << endl; for (int i = 1; i < D + n; i++) { cout << "(" << TA[i] << " " << TB[i] << ")"; if (i == 1 || i == 3 || i == 7 || i == 15) cout << endl; } cout << endl; #endif // DEBUG int nzb = -1; for (int i = n-1; i >= 0; i--) { if (B[i]) { nzb = i; } NZB[i] = nzb; } #ifdef DEBUG cout << "NZB: "; for (int i = 0; i < n; i++) { cout << NZB[i] << " "; } cout << endl; #endif // DEBUG int noa = 0; for (int i = n-1; i >= 0; i--) { if (A[i] != 1) { noa = i; } NOA[i] = noa; } #ifdef DEBUG cout << "NOA: "; for (int i = 0; i < n; i++) { cout << NOA[i] << " "; } cout << endl; #endif // DEBUG for (int i = 0; i < q; i++) { int x,l,r; cin >> x >> l >> r; cout << solve(x,l,r-1) << endl; } } |
English