#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#else
#define debug(...) 228
#endif
typedef long long ll;
typedef long double ld;
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
vector<int> read_vec(int n) {
vector<int> v(n);
for (int &t: v) cin >> t;
return v;
}
template<typename T>
inline void upd_max(T &a, T b) {
if (a < b) {
a = b;
}
}
template<typename T>
inline void upd_min(T &a, T b) {
if (a > b) {
a = b;
}
}
const int mod = (int) 1e9 + 7;
int sum(int a, int b) {
int s = a + b;
if (s >= mod) s -= mod;
return s;
}
int mult(int a, int b) {
return (1LL * a * b) % mod;
}
int n, q;
const int maxN = 5e5 + 10;
int a[maxN];
int b[maxN];
pair<int, int> t[4 * maxN];
void build(int v, int tl, int tr) {
if (tl == tr) {
if (b[tl] == 1) {
t[v] = {1, a[tl]};
} else {
t[v] = {b[tl], 0};
}
return;
}
int tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
t[v] = {mult(t[2 * v].first, t[2 * v + 1].first),
sum(t[2 * v + 1].second, mult(t[2 * v + 1].first, t[2 * v].second))};
}
ll pref[maxN];
void go(int v, int tl, int tr, int l, int r, int &x) {
if (r < tl || tr < l) return;
if (l <= tl && tr <= r) {
x = sum(t[v].second, mult(x, t[v].first));
return;
}
int tm = (tl + tr) / 2;
go(2 * v, tl, tm, l, r, x);
go(2 * v + 1, tm + 1, tr, l, r, x);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
cin >> n >> q;
set<int> not_ones;
FOR(i, 1, n + 1) {
cin >> a[i] >> b[i];
if (b[i] != 1) {
not_ones.insert(i);
}
pref[i] = pref[i - 1] + a[i];
}
not_ones.insert(n + 1);
build(1, 1, n);
while (q--) {
ll x;
int l, r;
cin >> x >> l >> r;
l++;
while (x < (ll) mod && l <= r) {
int nxt = *not_ones.lower_bound(l);
x += pref[min(r, nxt - 1)] - pref[l - 1];
if (nxt > r) {
l = r + 1;
break;
}
l = nxt;
if (x >= mod) break;
x = max(x + a[nxt], x * b[nxt]);
l++;
}
if (l > r) {
assert(l == (r + 1));
cout << x % mod << '\n';
} else {
int x_mod = x % mod;
go(1, 1, n, l, r, x_mod);
cout << x_mod << '\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 | #ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif typedef long long ll; typedef long double ld; #define pb push_back #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define F0R(i, a) FOR(i, 0, a) vector<int> read_vec(int n) { vector<int> v(n); for (int &t: v) cin >> t; return v; } template<typename T> inline void upd_max(T &a, T b) { if (a < b) { a = b; } } template<typename T> inline void upd_min(T &a, T b) { if (a > b) { a = b; } } const int mod = (int) 1e9 + 7; int sum(int a, int b) { int s = a + b; if (s >= mod) s -= mod; return s; } int mult(int a, int b) { return (1LL * a * b) % mod; } int n, q; const int maxN = 5e5 + 10; int a[maxN]; int b[maxN]; pair<int, int> t[4 * maxN]; void build(int v, int tl, int tr) { if (tl == tr) { if (b[tl] == 1) { t[v] = {1, a[tl]}; } else { t[v] = {b[tl], 0}; } return; } int tm = (tl + tr) / 2; build(2 * v, tl, tm); build(2 * v + 1, tm + 1, tr); t[v] = {mult(t[2 * v].first, t[2 * v + 1].first), sum(t[2 * v + 1].second, mult(t[2 * v + 1].first, t[2 * v].second))}; } ll pref[maxN]; void go(int v, int tl, int tr, int l, int r, int &x) { if (r < tl || tr < l) return; if (l <= tl && tr <= r) { x = sum(t[v].second, mult(x, t[v].first)); return; } int tm = (tl + tr) / 2; go(2 * v, tl, tm, l, r, x); go(2 * v + 1, tm + 1, tr, l, r, x); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif cin >> n >> q; set<int> not_ones; FOR(i, 1, n + 1) { cin >> a[i] >> b[i]; if (b[i] != 1) { not_ones.insert(i); } pref[i] = pref[i - 1] + a[i]; } not_ones.insert(n + 1); build(1, 1, n); while (q--) { ll x; int l, r; cin >> x >> l >> r; l++; while (x < (ll) mod && l <= r) { int nxt = *not_ones.lower_bound(l); x += pref[min(r, nxt - 1)] - pref[l - 1]; if (nxt > r) { l = r + 1; break; } l = nxt; if (x >= mod) break; x = max(x + a[nxt], x * b[nxt]); l++; } if (l > r) { assert(l == (r + 1)); cout << x % mod << '\n'; } else { int x_mod = x % mod; go(1, 1, n, l, r, x_mod); cout << x_mod << '\n'; } } return 0; } |
English