#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define st first
#define nd second
#define ld long double
ll M = 1e9+7;
vector<pair<ll, ll>> tr (1<<20, {1, 0});
pair<ll, ll> licz(pair<ll, ll> a, pair<ll, ll> b) {
return {(a.st*b.st)%M, (a.nd*b.st+b.nd)%M};
}
pair<ll, ll> suma(int v, int p, int k, int a, int b) {
if (a <= p && k <= b) {
return tr[v];
}
if (k < a || b < p) {
return {1, 0};
}
return licz(suma(v*2, p, (p+k)/2, a, b), suma(v*2+1, (p+k)/2+1, k, a, b));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<pair<ll, ll>> a (n+1);
vector<ll> sp (n+1, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i].st >> a[i].nd;
sp[i] = sp[i-1]+a[i].st;
if (a[i].nd==1) tr[i+(1<<19)]={1, a[i].st};
else tr[i+(1<<19)]={a[i].nd, 0};
}
//cout << 1 << '\n';
for (int i = (1<<19)-1; i > 0; i--) {
tr[i] = licz(tr[i*2], tr[i*2+1]);
}
vector<ll> wsk (n+2, n+1), wskd (n+2, n+1);
for (int i = n; i > 0; i--) {
if (a[i].nd > 1) {
wsk[i] = i;
}
else wsk[i] = wsk[i+1];
if (a[i].st == 0) {
wskd[i] = wskd[i+1];
}
else wskd[i] = i;
}
for (int i = 0; i < q; i++) {
ll x, l, r;
cin >> x >> l >> r;
l++, r++;
if (x==0) {
int in = wskd[l];
if (in >= r) {
cout << 0 << '\n';
continue;
}
x += a[in].st;
l = in+1;
}
while (x < M) {
int in = wsk[l];
if (in >= r) {
x += sp[r-1]-sp[l-1];
l = r+1;
break;
}
x += sp[in-1]-sp[l-1];
if (x >= M) {
l = in;
break;
}
x = max(x+a[in].st, x*a[in].nd);
//cout << l << ' ' << x << '\n';
l = in+1;
}
if (l > r) {
cout << x%M << '\n';
continue;
}
x %= M;
//cout << l << ' ' << r-1 << '\n';
auto y = suma(1, 0, (1<<19)-1, l, r-1);
x = (x*y.st+y.nd)%M;
cout << x%M << '\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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define st first #define nd second #define ld long double ll M = 1e9+7; vector<pair<ll, ll>> tr (1<<20, {1, 0}); pair<ll, ll> licz(pair<ll, ll> a, pair<ll, ll> b) { return {(a.st*b.st)%M, (a.nd*b.st+b.nd)%M}; } pair<ll, ll> suma(int v, int p, int k, int a, int b) { if (a <= p && k <= b) { return tr[v]; } if (k < a || b < p) { return {1, 0}; } return licz(suma(v*2, p, (p+k)/2, a, b), suma(v*2+1, (p+k)/2+1, k, a, b)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; vector<pair<ll, ll>> a (n+1); vector<ll> sp (n+1, 0); for (int i = 1; i <= n; i++) { cin >> a[i].st >> a[i].nd; sp[i] = sp[i-1]+a[i].st; if (a[i].nd==1) tr[i+(1<<19)]={1, a[i].st}; else tr[i+(1<<19)]={a[i].nd, 0}; } //cout << 1 << '\n'; for (int i = (1<<19)-1; i > 0; i--) { tr[i] = licz(tr[i*2], tr[i*2+1]); } vector<ll> wsk (n+2, n+1), wskd (n+2, n+1); for (int i = n; i > 0; i--) { if (a[i].nd > 1) { wsk[i] = i; } else wsk[i] = wsk[i+1]; if (a[i].st == 0) { wskd[i] = wskd[i+1]; } else wskd[i] = i; } for (int i = 0; i < q; i++) { ll x, l, r; cin >> x >> l >> r; l++, r++; if (x==0) { int in = wskd[l]; if (in >= r) { cout << 0 << '\n'; continue; } x += a[in].st; l = in+1; } while (x < M) { int in = wsk[l]; if (in >= r) { x += sp[r-1]-sp[l-1]; l = r+1; break; } x += sp[in-1]-sp[l-1]; if (x >= M) { l = in; break; } x = max(x+a[in].st, x*a[in].nd); //cout << l << ' ' << x << '\n'; l = in+1; } if (l > r) { cout << x%M << '\n'; continue; } x %= M; //cout << l << ' ' << r-1 << '\n'; auto y = suma(1, 0, (1<<19)-1, l, r-1); x = (x*y.st+y.nd)%M; cout << x%M << '\n'; } } |
English