#include <bits/stdc++.h>
using namespace std;
#define int long long
#define node pair<int,int>
#define st first
#define nd second
const int base = (1<<19), mod=1e9+7;
node op(node n, node m){
node ans = {(n.st*m.st)%mod, (n.nd*m.st + m.nd)%mod};
return ans;
}
node tree[base*2], t[base];
int nxt[base], pref[base];
node query(int l, int r){
l+=base; r+=base;
deque<int> lewo,prawo;
while(l<=r){
if(l%2==1) lewo.push_back(l++);
if(r%2==0) prawo.push_front(r--);
l/=2; r/=2;
}
for(int p: prawo) lewo.push_back(p);
node ans = {1,0};
for(int a: lewo) ans = op(ans, tree[a]);
return ans;
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
for(int i=0;i<base*2;i++) tree[i] = {1,0};
int n,q; cin >> n >> q;
for(int i=1;i<=n;i++){
int a,b; cin >> b >> a; //a=mnoz b=dod
t[i] = {a,b};
pref[i] = pref[i-1] + b;
if(a<2) tree[i+base] = {1,b};
else tree[i+base] = {a,0};
}
for(int i=base-1;i>0;i--) tree[i] = op(tree[i*2], tree[i*2+1]);
nxt[n+1] = n+1;
for(int i=n;i>=0;i--) if(t[i+1].st>=2) nxt[i] = i+1; else nxt[i] = nxt[i+1];
while(q--){
int x,l,r; cin >> x >> l >> r; //l to ostatnie zrobione
bool big=false;
while(l<r){
x = max(x*t[l+1].st, x+t[l+1].nd);
big |= (x>mod);
x%=mod;
if(big) {l++; break;}
if(nxt[l+1] <= r){
x += pref[nxt[l+1]-1] - pref[l+1];
l = nxt[l+1]-1;
}
else {l++; break;}
}
if(big){
node ans = query(l+1,r);
x = x*ans.st + ans.nd;
}
else x += pref[r] - pref[l];
cout << x%mod << '\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 | #include <bits/stdc++.h> using namespace std; #define int long long #define node pair<int,int> #define st first #define nd second const int base = (1<<19), mod=1e9+7; node op(node n, node m){ node ans = {(n.st*m.st)%mod, (n.nd*m.st + m.nd)%mod}; return ans; } node tree[base*2], t[base]; int nxt[base], pref[base]; node query(int l, int r){ l+=base; r+=base; deque<int> lewo,prawo; while(l<=r){ if(l%2==1) lewo.push_back(l++); if(r%2==0) prawo.push_front(r--); l/=2; r/=2; } for(int p: prawo) lewo.push_back(p); node ans = {1,0}; for(int a: lewo) ans = op(ans, tree[a]); return ans; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); for(int i=0;i<base*2;i++) tree[i] = {1,0}; int n,q; cin >> n >> q; for(int i=1;i<=n;i++){ int a,b; cin >> b >> a; //a=mnoz b=dod t[i] = {a,b}; pref[i] = pref[i-1] + b; if(a<2) tree[i+base] = {1,b}; else tree[i+base] = {a,0}; } for(int i=base-1;i>0;i--) tree[i] = op(tree[i*2], tree[i*2+1]); nxt[n+1] = n+1; for(int i=n;i>=0;i--) if(t[i+1].st>=2) nxt[i] = i+1; else nxt[i] = nxt[i+1]; while(q--){ int x,l,r; cin >> x >> l >> r; //l to ostatnie zrobione bool big=false; while(l<r){ x = max(x*t[l+1].st, x+t[l+1].nd); big |= (x>mod); x%=mod; if(big) {l++; break;} if(nxt[l+1] <= r){ x += pref[nxt[l+1]-1] - pref[l+1]; l = nxt[l+1]-1; } else {l++; break;} } if(big){ node ans = query(l+1,r); x = x*ans.st + ans.nd; } else x += pref[r] - pref[l]; cout << x%mod << '\n'; } } |
English