#include<bits/stdc++.h>
#define VAR(i,n) __typeof(n) i = (n)
#define loop(i,j,s) for(int i=j;i<s;i++)
#define loopback(i,j,s) for(int i=j;i>=s;i--)
#define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)
#define pln( x ) cout << x << "\n"
#define ps( x ) cout << x << " "
#define entr cout << "\n"
#define pcnt(i) __builtin_popcount(i)
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define SIZE(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<vector<int> > VVI;
const int INFTY=20000000;
const int MAX=500100;
const int MOD=10000000;
void coutTab(int* tab,int n){
loop(i,0,n){
cout<<tab[i]<<" ";
}
cout<<"\n";
}
template<class T> void coutVec(vector<T> tab){
for(T t : tab){
cout<<t<<" ";
}
cout<<"\n";
}
//------------------------------------------
const ll P=1000000007;
int n,q;
ll a[MAX], b[MAX];
int nxt[MAX]; // next pos with b>=2
ll sa[MAX]; // prefix sum of a[i] for b==1 events
ll C[MAX], D[MAX]; // suffix linear function: F(i..n, x) = C[i]*x + D[i]
ll Cinv[MAX]; // modular inverse of C[i]
ll pw(ll base, ll exp, ll mod){
ll res=1; base%=mod;
while(exp>0){
if(exp&1) res=res*base%mod;
base=base*base%mod;
exp>>=1;
}
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin>>n>>q;
loop(i,1,n+1) cin>>a[i]>>b[i];
// skip list: next b>=2
nxt[n+1]=n+1;
loopback(i,n,1){
if(b[i]>=2) nxt[i]=i;
else nxt[i]=nxt[i+1];
}
// prefix sums of a[i] where b[i]==1
sa[0]=0;
loop(i,1,n+1){
sa[i]=sa[i-1];
if(b[i]==1) sa[i]+=a[i];
}
// suffix linear function for "big" mode
// f(x) = b'*x + a' per event, b'=b[i] a'=0 if b>=2, b'=1 a'=a[i] if b==1
// C[i] = b'[i]*C[i+1], D[i] = a'[i]*C[i+1] + D[i+1]
C[n+1]=1; D[n+1]=0;
loopback(i,n,1){
if(b[i]>=2){
C[i]=C[i+1]*b[i]%P;
D[i]=D[i+1];
} else {
C[i]=C[i+1];
D[i]=(a[i]%P*C[i+1]%P+D[i+1])%P;
}
}
loop(i,1,n+2) Cinv[i]=pw(C[i],P-2,P);
while(q--){
ll x; int l,r;
cin>>x>>l>>r;
ll rv=x;
ll mv=x%P;
bool big=false;
int pos=nxt[l+1]; // first b>=2 in range
while(pos<=r && !big){
// add b==1 gap before this decision point
ll gap=sa[pos-1]-sa[l];
rv+=gap;
mv=(mv+gap%P)%P;
if(rv>P){ l=pos-1; big=true; break; }
// decide at pos (rv<=P so rv*b fits ll)
ll oa=rv+a[pos];
ll ob=rv*b[pos];
if(oa>=ob){
rv=oa;
mv=(mv+a[pos])%P;
} else {
rv=ob;
mv=(mv*b[pos])%P;
}
if(rv>P) big=true;
l=pos;
pos=nxt[pos+1];
}
if(big){
// remaining range [l+1..r]: use suffix linear function
// F(l+1..r, x) = (C[l+1]*x + D[l+1] - D[r+1]) * inv(C[r+1])
ll res=(C[l+1]%P*mv%P+D[l+1]-D[r+1]+P)%P;
res=res%P*Cinv[r+1]%P;
pln(res);
} else {
// still small, just add remaining b==1 gap
ll gap=sa[r]-sa[l];
mv=(mv+gap%P)%P;
pln(mv);
}
}
}
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 | #include<bits/stdc++.h> #define VAR(i,n) __typeof(n) i = (n) #define loop(i,j,s) for(int i=j;i<s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define SIZE(c) (c).size() #define ALL(c) (c).begin(), (c).end() using namespace std; typedef vector<int> VI; typedef pair<int, int> PII; typedef vector<vector<int> > VVI; const int INFTY=20000000; const int MAX=500100; const int MOD=10000000; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } template<class T> void coutVec(vector<T> tab){ for(T t : tab){ cout<<t<<" "; } cout<<"\n"; } //------------------------------------------ const ll P=1000000007; int n,q; ll a[MAX], b[MAX]; int nxt[MAX]; // next pos with b>=2 ll sa[MAX]; // prefix sum of a[i] for b==1 events ll C[MAX], D[MAX]; // suffix linear function: F(i..n, x) = C[i]*x + D[i] ll Cinv[MAX]; // modular inverse of C[i] ll pw(ll base, ll exp, ll mod){ ll res=1; base%=mod; while(exp>0){ if(exp&1) res=res*base%mod; base=base*base%mod; exp>>=1; } return res; } int main(){ ios_base::sync_with_stdio(0); cin>>n>>q; loop(i,1,n+1) cin>>a[i]>>b[i]; // skip list: next b>=2 nxt[n+1]=n+1; loopback(i,n,1){ if(b[i]>=2) nxt[i]=i; else nxt[i]=nxt[i+1]; } // prefix sums of a[i] where b[i]==1 sa[0]=0; loop(i,1,n+1){ sa[i]=sa[i-1]; if(b[i]==1) sa[i]+=a[i]; } // suffix linear function for "big" mode // f(x) = b'*x + a' per event, b'=b[i] a'=0 if b>=2, b'=1 a'=a[i] if b==1 // C[i] = b'[i]*C[i+1], D[i] = a'[i]*C[i+1] + D[i+1] C[n+1]=1; D[n+1]=0; loopback(i,n,1){ if(b[i]>=2){ C[i]=C[i+1]*b[i]%P; D[i]=D[i+1]; } else { C[i]=C[i+1]; D[i]=(a[i]%P*C[i+1]%P+D[i+1])%P; } } loop(i,1,n+2) Cinv[i]=pw(C[i],P-2,P); while(q--){ ll x; int l,r; cin>>x>>l>>r; ll rv=x; ll mv=x%P; bool big=false; int pos=nxt[l+1]; // first b>=2 in range while(pos<=r && !big){ // add b==1 gap before this decision point ll gap=sa[pos-1]-sa[l]; rv+=gap; mv=(mv+gap%P)%P; if(rv>P){ l=pos-1; big=true; break; } // decide at pos (rv<=P so rv*b fits ll) ll oa=rv+a[pos]; ll ob=rv*b[pos]; if(oa>=ob){ rv=oa; mv=(mv+a[pos])%P; } else { rv=ob; mv=(mv*b[pos])%P; } if(rv>P) big=true; l=pos; pos=nxt[pos+1]; } if(big){ // remaining range [l+1..r]: use suffix linear function // F(l+1..r, x) = (C[l+1]*x + D[l+1] - D[r+1]) * inv(C[r+1]) ll res=(C[l+1]%P*mv%P+D[l+1]-D[r+1]+P)%P; res=res%P*Cinv[r+1]%P; pln(res); } else { // still small, just add remaining b==1 gap ll gap=sa[r]-sa[l]; mv=(mv+gap%P)%P; pln(mv); } } } |
English