#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1e9+7;
const int M = 1 << 19;
pair<LL, LL> t[M * 2 + 5];
void update(int a, pair<LL, LL> v) {
a += M;
t[a] = v;
a /= 2;
while (a != 0) {
int l = 2 * a;
int r = 2 * a + 1;
t[a].first = (t[l].first * t[r].second + t[r].first) % MOD;
t[a].second = (t[l].second * t[r].second) % MOD;
a /= 2;
}
}
pair<LL, LL> query(int a, int b) {
a += M;
b += M;
pair<LL, LL> resl = t[a];
pair<LL, LL> resr = t[b];
if(a==b)return resl;
while (a / 2 != b / 2) {
if ((a & 1) == 0) {
resl.first = (resl.first*t[a + 1].second + t[a + 1].first)%MOD;
resl.second = (resl.second*t[a + 1].second)%MOD;
}
if ((b & 1) == 1) {
resr.first = (t[b - 1].first*resr.second + resr.first)%MOD;
resr.second = (t[b - 1].second * resr.second)%MOD;
}
a /= 2;
b /= 2;
}
pair<LL, LL> res = {(resl.first*resr.second + resr.first)%MOD, (resl.second*resr.second)%MOD};
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin>>n>>q;
vector<pair<LL, LL>> v;
v.push_back({0, 1});
vector<LL> dodawnie;
vector<int> skip(n+5);
dodawnie.push_back(0);
int start=0;
for(int i=1; i<=n; i++){
LL a, b;
cin>>a>>b;
if(b==1){
update(i, {a, b});
if(start == 0){
start=i;
}
}else{
update(i, {0, b});
if(start!=0){
for(int j=start; j<i; j++){
skip[j]=i-1;
}
start=0;
}
}
v.push_back({a, b});
dodawnie.push_back(dodawnie.back() + a);
}
if(start!=0){
for(int j=start; j<=n; j++){
skip[j]=n;
}
start=0;
}
while(q--){
LL x, l, r;
cin>>x>>l>>r;
l++;
int krok = 32;
while(krok && l<=r){
if(skip[l]){
int end = min(skip[l], (int)r);
x+=dodawnie[end] - dodawnie[l-1];
l=end+1;
if(x >= MOD){
x%=MOD;
break;
}
}else{
krok--;
if(x + v[l].first > x*v[l].second){
x+=v[l].first;
}else{
x*=v[l].second;
}
l++;
if(x >= MOD){
x%=MOD;
break;
}
}
}
if(l <= r){
auto [a, b] = query(l, r);
x = ((x*b)%MOD + a)%MOD;
}
cout<<x<<"\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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; const LL MOD = 1e9+7; const int M = 1 << 19; pair<LL, LL> t[M * 2 + 5]; void update(int a, pair<LL, LL> v) { a += M; t[a] = v; a /= 2; while (a != 0) { int l = 2 * a; int r = 2 * a + 1; t[a].first = (t[l].first * t[r].second + t[r].first) % MOD; t[a].second = (t[l].second * t[r].second) % MOD; a /= 2; } } pair<LL, LL> query(int a, int b) { a += M; b += M; pair<LL, LL> resl = t[a]; pair<LL, LL> resr = t[b]; if(a==b)return resl; while (a / 2 != b / 2) { if ((a & 1) == 0) { resl.first = (resl.first*t[a + 1].second + t[a + 1].first)%MOD; resl.second = (resl.second*t[a + 1].second)%MOD; } if ((b & 1) == 1) { resr.first = (t[b - 1].first*resr.second + resr.first)%MOD; resr.second = (t[b - 1].second * resr.second)%MOD; } a /= 2; b /= 2; } pair<LL, LL> res = {(resl.first*resr.second + resr.first)%MOD, (resl.second*resr.second)%MOD}; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, q; cin>>n>>q; vector<pair<LL, LL>> v; v.push_back({0, 1}); vector<LL> dodawnie; vector<int> skip(n+5); dodawnie.push_back(0); int start=0; for(int i=1; i<=n; i++){ LL a, b; cin>>a>>b; if(b==1){ update(i, {a, b}); if(start == 0){ start=i; } }else{ update(i, {0, b}); if(start!=0){ for(int j=start; j<i; j++){ skip[j]=i-1; } start=0; } } v.push_back({a, b}); dodawnie.push_back(dodawnie.back() + a); } if(start!=0){ for(int j=start; j<=n; j++){ skip[j]=n; } start=0; } while(q--){ LL x, l, r; cin>>x>>l>>r; l++; int krok = 32; while(krok && l<=r){ if(skip[l]){ int end = min(skip[l], (int)r); x+=dodawnie[end] - dodawnie[l-1]; l=end+1; if(x >= MOD){ x%=MOD; break; } }else{ krok--; if(x + v[l].first > x*v[l].second){ x+=v[l].first; }else{ x*=v[l].second; } l++; if(x >= MOD){ x%=MOD; break; } } } if(l <= r){ auto [a, b] = query(l, r); x = ((x*b)%MOD + a)%MOD; } cout<<x<<"\n"; } return 0; } |
English