#include <bits/stdc++.h>
using namespace std;
const int MOD=1e9+7;
long long power(long long base, long long exp)
{
long long res=1;
base%=MOD;
while(exp>0)
{
if(exp%2==1)
{
res=(res*base)%MOD;
}
base=(base*base)%MOD;
exp/=2;
}
return res;
}
long long inverse(long long n)
{
return power(n,MOD-2);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,q;
cin>>n>>q;
vector<long long> a(n+1);
vector<long long> b(n+1);
vector<int> mult;
vector<int> next(n+2,n+1);
for(int i=1; i<=n; i++)
{
cin>>a[i]>>b[i];
if(b[i]>=2)
{
mult.push_back(i);
}
}
for(int i=n; i>=1; i--)
{
if(a[i]>0)
{
next[i]=i;
}
else
{
next[i]=next[i+1];
}
}
vector<long long> pref(n+1,0);
vector<long long> B(n+1,1);
vector<long long> A(n+1,0);
for(int i=1; i<=n; i++)
{
pref[i]=pref[i-1]+(b[i]==1 ? a[i] : 0);
long long cb,ca;
if(b[i]>=2)
{
cb=b[i];
ca=0;
}
else
{
cb=1;
ca=a[i];
}
B[i]=(B[i-1]*cb)%MOD;
A[i]=(A[i-1]*cb+ca)%MOD;
}
while(q--)
{
long long x;
int l,r;
cin>>x>>l>>r;
long long curr=x;
int last=l;
if(curr==0)
{
int first=next[l+1];
if(first>r)
{
cout<<0<<endl;
continue;
}
curr=a[first];
last=first;
}
auto it=upper_bound(mult.begin(),mult.end(),last);
bool reached=false;
while(it!=mult.end() && *it<=r)
{
int j=*it;
curr+=(pref[j-1]-pref[last]);
if(curr>=MOD)
{
reached=true;
last=j-1;
break;
}
if(curr*b[j]>=curr+a[j])
{
curr*=b[j];
}
else
{
curr+=a[j];
}
last=j;
if(curr>=MOD)
{
reached=true;
break;
}
it++;
}
if(reached)
{
long long br=(B[r]*inverse(B[last]))%MOD;
long long ar=(A[r]-(A[last]*br)%MOD+MOD)%MOD;
long long res=((curr%MOD)*br+ar)%MOD;
cout<<res<<endl;
}
else
{
curr+=(pref[r]-pref[last]);
cout<<curr%MOD<<endl;
}
}
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | #include <bits/stdc++.h> using namespace std; const int MOD=1e9+7; long long power(long long base, long long exp) { long long res=1; base%=MOD; while(exp>0) { if(exp%2==1) { res=(res*base)%MOD; } base=(base*base)%MOD; exp/=2; } return res; } long long inverse(long long n) { return power(n,MOD-2); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n,q; cin>>n>>q; vector<long long> a(n+1); vector<long long> b(n+1); vector<int> mult; vector<int> next(n+2,n+1); for(int i=1; i<=n; i++) { cin>>a[i]>>b[i]; if(b[i]>=2) { mult.push_back(i); } } for(int i=n; i>=1; i--) { if(a[i]>0) { next[i]=i; } else { next[i]=next[i+1]; } } vector<long long> pref(n+1,0); vector<long long> B(n+1,1); vector<long long> A(n+1,0); for(int i=1; i<=n; i++) { pref[i]=pref[i-1]+(b[i]==1 ? a[i] : 0); long long cb,ca; if(b[i]>=2) { cb=b[i]; ca=0; } else { cb=1; ca=a[i]; } B[i]=(B[i-1]*cb)%MOD; A[i]=(A[i-1]*cb+ca)%MOD; } while(q--) { long long x; int l,r; cin>>x>>l>>r; long long curr=x; int last=l; if(curr==0) { int first=next[l+1]; if(first>r) { cout<<0<<endl; continue; } curr=a[first]; last=first; } auto it=upper_bound(mult.begin(),mult.end(),last); bool reached=false; while(it!=mult.end() && *it<=r) { int j=*it; curr+=(pref[j-1]-pref[last]); if(curr>=MOD) { reached=true; last=j-1; break; } if(curr*b[j]>=curr+a[j]) { curr*=b[j]; } else { curr+=a[j]; } last=j; if(curr>=MOD) { reached=true; break; } it++; } if(reached) { long long br=(B[r]*inverse(B[last]))%MOD; long long ar=(A[r]-(A[last]*br)%MOD+MOD)%MOD; long long res=((curr%MOD)*br+ar)%MOD; cout<<res<<endl; } else { curr+=(pref[r]-pref[last]); cout<<curr%MOD<<endl; } } return 0; } |
English