#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define st first
#define nd second
ll mod = 1000000007;
vector<pair<ll, ll>> tree;
pair<ll, ll> merge(pair<ll, ll> f, pair<ll, ll> s) {
return {(f.st*s.st)%mod, (f.nd*s.st + s.nd)%mod};
}
ll apply(ll x, int u, int p, int q, int l, int r) {
if (q <= l || p >= r) return x;
if (p >= l && q <= r) {
return (x * tree[u].st + tree[u].nd)%mod;
}
x = apply(x, u*2, p, (p+q)/2, l, r);
x = apply(x, u*2+1, (p+q)/2, q, l, r);
return x;
}
void solve() {
int n, q; cin >> n >> q;
vector<ll> a(n+1), b(n+1);
for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
vector<int> next_pos_a(n+1);
next_pos_a[n] = n+1;
for (int i = n-1; i >= 0; i--) {
if (a[i+1] > 0) next_pos_a[i] = i+1;
else next_pos_a[i] = next_pos_a[i+1];
}
vector<ll> pref_a(n+1);
for (int i = 1; i <= n; i++) {
pref_a[i] = pref_a[i-1]+a[i];
}
vector<int> next_big_b(n+1);
next_big_b[n] = n+1;
; for (int i = n-1; i >= 0; i--) {
if (b[i+1] > 1) next_big_b[i] = i+1;
else next_big_b[i] = next_big_b[i+1];
}
int sz = 1;
while (sz < n+1) sz <<= 1;
tree.resize(2*sz, {1, 0});
for (int i = 1; i <= n; i++) {
if (b[i] > 1) tree[i+sz] = {b[i], 0};
else tree[i+sz] = {1, a[i]};
}
for (int i = sz-1; i >= 1; i--) {
tree[i] = merge(tree[i*2], tree[i*2+1]);
}
while (q--) {
ll x;
cin >> x;
int l, r; cin >> l >> r;
if (x == 0) {
int l2 = next_pos_a[l];
if (l2 > r) {
cout << "0\n";
continue;
}
x = a[l2];
l = l2;
}
while (l < r && x < mod) {
if (b[l+1] > 1) {
x = max(x+a[l+1], x*b[l+1]);
l++;
}
else {
int l2 = next_big_b[l]-1;
l2 = min(l2, r);
x += pref_a[l2] - pref_a[l];
l = l2;
}
// cout << l << ' ' << x << '\n';
}
x %= mod;
if (l == r) {
cout << x << '\n';
continue;
}
// cout << "xd";
x = apply(x, 1, 0, sz, l+1, r+1);
cout << x << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define st first #define nd second ll mod = 1000000007; vector<pair<ll, ll>> tree; pair<ll, ll> merge(pair<ll, ll> f, pair<ll, ll> s) { return {(f.st*s.st)%mod, (f.nd*s.st + s.nd)%mod}; } ll apply(ll x, int u, int p, int q, int l, int r) { if (q <= l || p >= r) return x; if (p >= l && q <= r) { return (x * tree[u].st + tree[u].nd)%mod; } x = apply(x, u*2, p, (p+q)/2, l, r); x = apply(x, u*2+1, (p+q)/2, q, l, r); return x; } void solve() { int n, q; cin >> n >> q; vector<ll> a(n+1), b(n+1); for (int i = 1; i <= n; i++) cin >> a[i] >> b[i]; vector<int> next_pos_a(n+1); next_pos_a[n] = n+1; for (int i = n-1; i >= 0; i--) { if (a[i+1] > 0) next_pos_a[i] = i+1; else next_pos_a[i] = next_pos_a[i+1]; } vector<ll> pref_a(n+1); for (int i = 1; i <= n; i++) { pref_a[i] = pref_a[i-1]+a[i]; } vector<int> next_big_b(n+1); next_big_b[n] = n+1; ; for (int i = n-1; i >= 0; i--) { if (b[i+1] > 1) next_big_b[i] = i+1; else next_big_b[i] = next_big_b[i+1]; } int sz = 1; while (sz < n+1) sz <<= 1; tree.resize(2*sz, {1, 0}); for (int i = 1; i <= n; i++) { if (b[i] > 1) tree[i+sz] = {b[i], 0}; else tree[i+sz] = {1, a[i]}; } for (int i = sz-1; i >= 1; i--) { tree[i] = merge(tree[i*2], tree[i*2+1]); } while (q--) { ll x; cin >> x; int l, r; cin >> l >> r; if (x == 0) { int l2 = next_pos_a[l]; if (l2 > r) { cout << "0\n"; continue; } x = a[l2]; l = l2; } while (l < r && x < mod) { if (b[l+1] > 1) { x = max(x+a[l+1], x*b[l+1]); l++; } else { int l2 = next_big_b[l]-1; l2 = min(l2, r); x += pref_a[l2] - pref_a[l]; l = l2; } // cout << l << ' ' << x << '\n'; } x %= mod; if (l == r) { cout << x << '\n'; continue; } // cout << "xd"; x = apply(x, 1, 0, sz, l+1, r+1); cout << x << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } } |
English