#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;
using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) {
*(int *)0 = 0;
});
# define DTP(x, y) \
auto operator<<(auto &o, auto a)->decltype(y, o) { \
o << "("; \
x; \
return o << ")"; \
}
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) {
((cerr << x << ", "), ...) << '\n';
}
# define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
# define deb(...) 0
#endif
const int MOD = 1e9 + 7;
inline bool isIndependentValue(long long x) {
return x >= MOD;
}
// x is very large, hence all choices are independent of its exact value:
// choose mul if greater than one, otherwise choose add
struct IndependentXSimulator {
vector<pair<long long, long long> > tree;
int tree_base;
IndependentXSimulator(const vi &add, const vi &mul) {
tree_base = 2;
while (tree_base < sz(add))
tree_base *= 2;
tree.resize(2 * tree_base);
rep(i, sz(add)) {
if (mul[i] != 1) {
tree[tree_base + i] = {mul[i], 0ll};
} else {
tree[tree_base + i] = {1ll, add[i]};
}
}
for (int i = tree_base - 1; i > 0; i--) {
auto [mul_l, add_l] = tree[2 * i];
auto [mul_r, add_r] = tree[2 * i + 1];
tree[i] = {(mul_l * mul_r) % MOD, (add_l * mul_r + add_r) % MOD};
}
}
long long simulate(long long x, int l, int r) {
long long mul = 1, add = 0, res = x % MOD;
for (l += tree_base, r += tree_base; l <= r; l /= 2, r /= 2) {
if (l % 2 == 1) {
auto [mul_l, add_l] = tree[l++];
res = (res * mul_l + add_l) % MOD;
}
if (r % 2 == 0) {
auto [mul_r, add_r] = tree[r--];
add = (add_r * mul + add) % MOD;
mul = (mul * mul_r) % MOD;
}
}
return (res * mul + add) % MOD;
}
};
struct DependentXSimStepFinder {
DependentXSimStepFinder(const vi &add, const vi &mul) :
_add(add),
_mul(mul),
sufsum_add(add.size() + 1),
next_mul_gt_one(mul.size() + 1),
next_add_gt_zero(add.size() + 1) {
next_mul_gt_one[sz(mul)] = sz(mul);
next_add_gt_zero[sz(add)] = sz(add);
for (int i = sz(add) - 1; i >= 0; i--) {
sufsum_add[i] = sufsum_add[i + 1] + add[i];
next_mul_gt_one[i] = (mul[i] > 1 ? i : next_mul_gt_one[i + 1]);
next_add_gt_zero[i] = (add[i] > 0 ? i : next_add_gt_zero[i + 1]);
}
}
// returns {next_x, next_l} that is the value of x after taking all steps
// until next_l exclusive
pair<long long, int> findSimStep(long long x, int l, int r) {
if (x == 0) {
int next_add_pos = min(next_add_gt_zero[l], r + 1);
if (next_add_pos > r) {
return {0, next_add_pos};
}
x += _add[next_add_pos];
return {x, next_add_pos + 1};
}
int next_gt_one_pos = min(next_mul_gt_one[l], r + 1);
x += sufsum_add[l] - sufsum_add[next_gt_one_pos];
if (isIndependentValue(x) || next_gt_one_pos > r) {
return {x, next_gt_one_pos};
}
x = max(x + _add[next_gt_one_pos], x * _mul[next_gt_one_pos]);
return {x, next_gt_one_pos + 1};
}
vi _add, _mul;
vector<long long> sufsum_add;
vi next_mul_gt_one;
vi next_add_gt_zero;
};
struct Simulator {
Simulator(const vi &add, const vi &mul) :
dependent_finder(add, mul), independent_simulator(add, mul) {
}
long long simulateDependent(long long x, int l, int r) {
// x is dependent, there will be at most O(log(MOD)) such steps
// until x becomes independent
while (l <= r && !isIndependentValue(x)) {
auto [next_x, next_l] = dependent_finder.findSimStep(x, l, r);
x = next_x;
l = next_l;
}
x %= MOD;
return independent_simulator.simulate(x, l, r);
}
DependentXSimStepFinder dependent_finder;
IndependentXSimulator independent_simulator;
};
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vi add(n), mul(n);
rep(i, n) cin >> add[i] >> mul[i];
Simulator sim(add, mul);
rep(i, q) {
long long x;
int l, r;
cin >> x >> l >> r;
cout << sim.simulateDependent(x, l, r - 1) << '\n';
}
#ifdef LOCF
cout.flush();
cerr << "- - - - - - - - -\n";
(void)!system(
"grep VmPeak /proc/$PPID/status | sed s/....kB/\' MB\'/1 >&2"); // 4x.kB
// ....kB
#endif
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 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(size(X)) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); # define DTP(x, y) \ auto operator<<(auto &o, auto a)->decltype(y, o) { \ o << "("; \ x; \ return o << ")"; \ } DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } # define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else # define deb(...) 0 #endif const int MOD = 1e9 + 7; inline bool isIndependentValue(long long x) { return x >= MOD; } // x is very large, hence all choices are independent of its exact value: // choose mul if greater than one, otherwise choose add struct IndependentXSimulator { vector<pair<long long, long long> > tree; int tree_base; IndependentXSimulator(const vi &add, const vi &mul) { tree_base = 2; while (tree_base < sz(add)) tree_base *= 2; tree.resize(2 * tree_base); rep(i, sz(add)) { if (mul[i] != 1) { tree[tree_base + i] = {mul[i], 0ll}; } else { tree[tree_base + i] = {1ll, add[i]}; } } for (int i = tree_base - 1; i > 0; i--) { auto [mul_l, add_l] = tree[2 * i]; auto [mul_r, add_r] = tree[2 * i + 1]; tree[i] = {(mul_l * mul_r) % MOD, (add_l * mul_r + add_r) % MOD}; } } long long simulate(long long x, int l, int r) { long long mul = 1, add = 0, res = x % MOD; for (l += tree_base, r += tree_base; l <= r; l /= 2, r /= 2) { if (l % 2 == 1) { auto [mul_l, add_l] = tree[l++]; res = (res * mul_l + add_l) % MOD; } if (r % 2 == 0) { auto [mul_r, add_r] = tree[r--]; add = (add_r * mul + add) % MOD; mul = (mul * mul_r) % MOD; } } return (res * mul + add) % MOD; } }; struct DependentXSimStepFinder { DependentXSimStepFinder(const vi &add, const vi &mul) : _add(add), _mul(mul), sufsum_add(add.size() + 1), next_mul_gt_one(mul.size() + 1), next_add_gt_zero(add.size() + 1) { next_mul_gt_one[sz(mul)] = sz(mul); next_add_gt_zero[sz(add)] = sz(add); for (int i = sz(add) - 1; i >= 0; i--) { sufsum_add[i] = sufsum_add[i + 1] + add[i]; next_mul_gt_one[i] = (mul[i] > 1 ? i : next_mul_gt_one[i + 1]); next_add_gt_zero[i] = (add[i] > 0 ? i : next_add_gt_zero[i + 1]); } } // returns {next_x, next_l} that is the value of x after taking all steps // until next_l exclusive pair<long long, int> findSimStep(long long x, int l, int r) { if (x == 0) { int next_add_pos = min(next_add_gt_zero[l], r + 1); if (next_add_pos > r) { return {0, next_add_pos}; } x += _add[next_add_pos]; return {x, next_add_pos + 1}; } int next_gt_one_pos = min(next_mul_gt_one[l], r + 1); x += sufsum_add[l] - sufsum_add[next_gt_one_pos]; if (isIndependentValue(x) || next_gt_one_pos > r) { return {x, next_gt_one_pos}; } x = max(x + _add[next_gt_one_pos], x * _mul[next_gt_one_pos]); return {x, next_gt_one_pos + 1}; } vi _add, _mul; vector<long long> sufsum_add; vi next_mul_gt_one; vi next_add_gt_zero; }; struct Simulator { Simulator(const vi &add, const vi &mul) : dependent_finder(add, mul), independent_simulator(add, mul) { } long long simulateDependent(long long x, int l, int r) { // x is dependent, there will be at most O(log(MOD)) such steps // until x becomes independent while (l <= r && !isIndependentValue(x)) { auto [next_x, next_l] = dependent_finder.findSimStep(x, l, r); x = next_x; l = next_l; } x %= MOD; return independent_simulator.simulate(x, l, r); } DependentXSimStepFinder dependent_finder; IndependentXSimulator independent_simulator; }; int32_t main() { cin.tie(0)->sync_with_stdio(0); int n, q; cin >> n >> q; vi add(n), mul(n); rep(i, n) cin >> add[i] >> mul[i]; Simulator sim(add, mul); rep(i, q) { long long x; int l, r; cin >> x >> l >> r; cout << sim.simulateDependent(x, l, r - 1) << '\n'; } #ifdef LOCF cout.flush(); cerr << "- - - - - - - - -\n"; (void)!system( "grep VmPeak /proc/$PPID/status | sed s/....kB/\' MB\'/1 >&2"); // 4x.kB // ....kB #endif return 0; } |
English