#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define pb push_back
#define F first
#define S second
#define INF (ll)2e18
#define pii pair<int,int>
#define int ll
const ll mod = (ll)1e9+7;
int add(int a, int b){
int res = a + b;
if (res >= mod)
res -= mod;
return res;
}
pii merge(pii a, pii b){
return {a.F*b.F%mod, add(a.S*b.F%mod,b.S)};
}
struct segtree{
vector<pii> data;
int n;
segtree(vector<pii> arr){
n = 1;
while(n < arr.size()) n *= 2;
data.assign(2*n,{1,0});
for (int i = 0; i < arr.size(); i++){
data[i+n] = (arr[i].S == 1 ? pii{1,arr[i].F} : pii{arr[i].S,0});
}
for (int i = n-1; i > 0; i--){
data[i] = merge(data[i*2],data[i*2+1]);
}
}
pii query(int l, int r){
pii lres = {1,0};
pii rres = {1,0};
l += n, r += n;
while (l <= r){
if (l % 2 == 1){
lres = merge(lres, data[l]);
l++;
}
if (r % 2 == 0){
rres = merge(data[r], rres);
r--;
}
l /= 2;
r /= 2;
}
return merge(lres,rres);
}
};
void solve() {
int n,q; cin >> n >> q;
vector<pii> a(n);
for (int i = 0; i < n; i++){
cin >> a[i].F >> a[i].S;
}
segtree tree(a);
vector<pii> next(n+1);
next[n] = {n,0};
for (int i = n-1; i >= 0; i--){
if (a[i].S != 1){
next[i] = {i,0};
} else {
next[i] = next[i+1];
next[i].S += a[i].F;
}
}
while(q--){
int x,l,r; cin >> x >> l >> r;
r--;
while(x < mod && l <= r){
if (next[l].F != l){
if (next[l].F > r){
x += next[l].S - next[r+1].S;
} else {
x += next[l].S;
}
l = next[l].F;
} else {
if (x + a[l].F > x * a[l].S){
x += a[l].F;
} else {
x *= a[l].S;
}
l++;
}
}
x %= mod;
if (l <= r){
pii res = tree.query(l,r);
x = add(x * res.F % mod,res.S);
}
cout << x << '\n';
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define all(a) (a).begin(),(a).end() #define allr(a) (a).rbegin(),(a).rend() #define ll long long #define ld long double #define pll pair<ll,ll> #define pb push_back #define F first #define S second #define INF (ll)2e18 #define pii pair<int,int> #define int ll const ll mod = (ll)1e9+7; int add(int a, int b){ int res = a + b; if (res >= mod) res -= mod; return res; } pii merge(pii a, pii b){ return {a.F*b.F%mod, add(a.S*b.F%mod,b.S)}; } struct segtree{ vector<pii> data; int n; segtree(vector<pii> arr){ n = 1; while(n < arr.size()) n *= 2; data.assign(2*n,{1,0}); for (int i = 0; i < arr.size(); i++){ data[i+n] = (arr[i].S == 1 ? pii{1,arr[i].F} : pii{arr[i].S,0}); } for (int i = n-1; i > 0; i--){ data[i] = merge(data[i*2],data[i*2+1]); } } pii query(int l, int r){ pii lres = {1,0}; pii rres = {1,0}; l += n, r += n; while (l <= r){ if (l % 2 == 1){ lres = merge(lres, data[l]); l++; } if (r % 2 == 0){ rres = merge(data[r], rres); r--; } l /= 2; r /= 2; } return merge(lres,rres); } }; void solve() { int n,q; cin >> n >> q; vector<pii> a(n); for (int i = 0; i < n; i++){ cin >> a[i].F >> a[i].S; } segtree tree(a); vector<pii> next(n+1); next[n] = {n,0}; for (int i = n-1; i >= 0; i--){ if (a[i].S != 1){ next[i] = {i,0}; } else { next[i] = next[i+1]; next[i].S += a[i].F; } } while(q--){ int x,l,r; cin >> x >> l >> r; r--; while(x < mod && l <= r){ if (next[l].F != l){ if (next[l].F > r){ x += next[l].S - next[r+1].S; } else { x += next[l].S; } l = next[l].F; } else { if (x + a[l].F > x * a[l].S){ x += a[l].F; } else { x *= a[l].S; } l++; } } x %= mod; if (l <= r){ pii res = tree.query(l,r); x = add(x * res.F % mod,res.S); } cout << x << '\n'; } } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int t = 1; //cin >> t; while (t--) { solve(); } return 0; } |
English