#include <bits/stdc++.h>
using namespace std;
const int MOD = 1'000'000'007;
struct fn {
int a, b;
int apply(int x) {
return ((long long) a * x + b) % MOD;
}
};
struct query {
int x, l, r;
};
int expo(int a, int n) {
int ans = 1;
while (n) {
if (n & 1) {
ans = ((long long) ans * a) % MOD;
}
n >>= 1;
a = ((long long) a * a) % MOD;
}
return ans;
}
int revMod(int a) {
return expo(a, MOD - 2);
}
fn compose(const fn &f, const fn &g) {
int ca = ((long long) g.a * f.a) % MOD;
int cb = ((long long) g.a * f.b + g.b) % MOD;
return {ca, cb};
}
fn invert(const fn &f) {
int ia = revMod(f.a);
int ib = ((long long) ia * -f.b) % MOD;
if (ib < 0) {
ib += MOD;
}
return {ia, ib};
}
void changeValue(int &x, bool &overMod, long long xNew) {
if (xNew >= MOD) {
overMod = true;
x = xNew % MOD;
} else {
x = xNew;
}
}
vector <int> solve(int n, int q, vector <int> &a, vector <int> &b, vector <query> &queries) {
vector <int> realAddIds;
for (int i = 0; i < n; i++) if (a[i] > 0) {
realAddIds.push_back(i);
}
realAddIds.push_back(n);
vector <int> realMulIds;
for (int i = 0; i < n; i++) if (b[i] > 1) {
realMulIds.push_back(i);
}
realMulIds.push_back(n);
vector <long long> forcedAddsPref(n, 0);
for (int i = 0; i < n; i++) {
if (b[i] == 1) {
forcedAddsPref[i] = a[i];
}
if (i > 0) {
forcedAddsPref[i] += forcedAddsPref[i - 1];
}
}
vector <fn> fnsSuff(n);
for (int i = 0; i < n; i++) {
if (b[i] > 1) {
fnsSuff[i] = {b[i], 0};
} else {
fnsSuff[i] = {1, a[i]};
}
}
for (int i = n - 2; i >= 0; i--) {
fnsSuff[i] = compose(fnsSuff[i], fnsSuff[i + 1]);
}
vector <int> ans(q);
for (int i = 0; i < q; i++) {
int x = queries[i].x, pos = queries[i].l, r = queries[i].r;
if (x == 0) {
int nextRealAdd = *lower_bound(realAddIds.begin(), realAddIds.end(), pos);
if (nextRealAdd <= r) {
pos = nextRealAdd;
} else {
ans[i] = 0;
continue;
}
}
bool overMod = false;
auto realMulIdsIt = lower_bound(realMulIds.begin(), realMulIds.end(), pos);
while (pos <= r) {
if (overMod) {
fn fRest = fnsSuff[pos];
if (r < n - 1) {
fn fExcl = fnsSuff[r + 1];
fRest = compose(fRest, invert(fExcl));
}
x = fRest.apply(x);
break;
}
if (*realMulIdsIt < pos) {
realMulIdsIt++;
}
int nextRealMul = *realMulIdsIt;
if (pos < nextRealMul) {
int posRight = min(nextRealMul - 1, r);
long long forcedAdds = forcedAddsPref[posRight] - (pos ? forcedAddsPref[pos - 1] : 0);
changeValue(x, overMod, x + forcedAdds);
pos = posRight + 1;
} else {
long long xa = (long long) x + a[pos];
long long xb = (long long) x * b[pos];
changeValue(x, overMod, max(xa, xb));
pos++;
}
}
ans[i] = x;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
int n, q;
cin >> n >> q;
vector <int> a(n), b(n);
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
vector <query> queries(q);
for (auto &qr : queries) {
cin >> qr.x >> qr.l >> qr.r; qr.r--;
}
auto ans = solve(n, q, a, b, queries);
for (int v : ans) {
cout << v << '\n';
}
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 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 180 181 | #include <bits/stdc++.h> using namespace std; const int MOD = 1'000'000'007; struct fn { int a, b; int apply(int x) { return ((long long) a * x + b) % MOD; } }; struct query { int x, l, r; }; int expo(int a, int n) { int ans = 1; while (n) { if (n & 1) { ans = ((long long) ans * a) % MOD; } n >>= 1; a = ((long long) a * a) % MOD; } return ans; } int revMod(int a) { return expo(a, MOD - 2); } fn compose(const fn &f, const fn &g) { int ca = ((long long) g.a * f.a) % MOD; int cb = ((long long) g.a * f.b + g.b) % MOD; return {ca, cb}; } fn invert(const fn &f) { int ia = revMod(f.a); int ib = ((long long) ia * -f.b) % MOD; if (ib < 0) { ib += MOD; } return {ia, ib}; } void changeValue(int &x, bool &overMod, long long xNew) { if (xNew >= MOD) { overMod = true; x = xNew % MOD; } else { x = xNew; } } vector <int> solve(int n, int q, vector <int> &a, vector <int> &b, vector <query> &queries) { vector <int> realAddIds; for (int i = 0; i < n; i++) if (a[i] > 0) { realAddIds.push_back(i); } realAddIds.push_back(n); vector <int> realMulIds; for (int i = 0; i < n; i++) if (b[i] > 1) { realMulIds.push_back(i); } realMulIds.push_back(n); vector <long long> forcedAddsPref(n, 0); for (int i = 0; i < n; i++) { if (b[i] == 1) { forcedAddsPref[i] = a[i]; } if (i > 0) { forcedAddsPref[i] += forcedAddsPref[i - 1]; } } vector <fn> fnsSuff(n); for (int i = 0; i < n; i++) { if (b[i] > 1) { fnsSuff[i] = {b[i], 0}; } else { fnsSuff[i] = {1, a[i]}; } } for (int i = n - 2; i >= 0; i--) { fnsSuff[i] = compose(fnsSuff[i], fnsSuff[i + 1]); } vector <int> ans(q); for (int i = 0; i < q; i++) { int x = queries[i].x, pos = queries[i].l, r = queries[i].r; if (x == 0) { int nextRealAdd = *lower_bound(realAddIds.begin(), realAddIds.end(), pos); if (nextRealAdd <= r) { pos = nextRealAdd; } else { ans[i] = 0; continue; } } bool overMod = false; auto realMulIdsIt = lower_bound(realMulIds.begin(), realMulIds.end(), pos); while (pos <= r) { if (overMod) { fn fRest = fnsSuff[pos]; if (r < n - 1) { fn fExcl = fnsSuff[r + 1]; fRest = compose(fRest, invert(fExcl)); } x = fRest.apply(x); break; } if (*realMulIdsIt < pos) { realMulIdsIt++; } int nextRealMul = *realMulIdsIt; if (pos < nextRealMul) { int posRight = min(nextRealMul - 1, r); long long forcedAdds = forcedAddsPref[posRight] - (pos ? forcedAddsPref[pos - 1] : 0); changeValue(x, overMod, x + forcedAdds); pos = posRight + 1; } else { long long xa = (long long) x + a[pos]; long long xb = (long long) x * b[pos]; changeValue(x, overMod, max(xa, xb)); pos++; } } ans[i] = x; } return ans; } int main() { ios_base::sync_with_stdio(false); int n, q; cin >> n >> q; vector <int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } vector <query> queries(q); for (auto &qr : queries) { cin >> qr.x >> qr.l >> qr.r; qr.r--; } auto ans = solve(n, q, a, b, queries); for (int v : ans) { cout << v << '\n'; } return 0; } |
English