#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define ST first
#define ND second
#define PB push_back
using ll = long long;
using ld = long double;
using pi = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pll=pair<ll,ll>;
#ifdef LOCAL
#define DTP(x, y) \
auto operator<<(auto &o, auto a)->decltype(y, o) \
{ \
o << "("; \
x; \
return o << ")"; \
}
DTP(o << a.first << ", " << a.second, a.second);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; }
#define debug(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#define debug(...) 0
#endif
const ll P=1000'000'007;
struct node{
ll b;
int l, r;
vll A;
node(int x, int y, int z){
l=r=x;
A.resize(1, y);
b=z;
}
};
ll fPow(ll a, ll b){
if(!b)
return 1;
if(b&1)
return (fPow(a, b-1)*a)%P;
ll c=fPow(a, b/2);
return (c*c)%P;
}
pair<ll,ll> Inv(pair<ll,ll> a){
pair<ll,ll> t;
t.ST=fPow(a.ST, P-2);
t.ND=(t.ST*a.ND)%P;
t.ND=(P-t.ND)%P;
return t;
}
pair<ll,ll> Conv(pair<ll,ll> a, pair<ll,ll> b){
pair<ll,ll> t;
t.ST=(a.ST*b.ST)%P;
t.ND=(a.ST*b.ND+a.ND)%P;
return t;
}
void Solve()
{
int n, q;
cin >> n >> q;
vector<node> D;
vi Where(n);
vi Non_zero(n);
vector<pair<ll,ll>> B(n);
rep(i, 0, n){
int a, b;
cin >> a >> b;
B[i].ST=b;
B[i].ND=a;
if(i==0 || b>0 || D.back().b>0){
D.PB(node(i, a, b));
}else{
++D.back().r;
D.back().A.PB(D.back().A.back()+a);
}
Where[i]=sz(D)-1;
}
Non_zero[n-1]=Where[n-1];
for(int i=n-2; i>=0; --i){
Non_zero[i]=Non_zero[i+1];
if((D[Where[i]].l==i && D[Where[i]].A.back()>0) || ( D[Where[i]].l>i && D[Where[i]].A.back()-D[Where[i]].A[i-D[Where[i]].l-1]>0))
Non_zero[i]=Where[i];
}
vector<pair<ll,ll>> Pref(n);
rep(i, 0, n){
if(B[i].ST>1)
B[i].ND=0;
}
Pref[0]=B[0];
rep(i, 1, n)
Pref[i]=Conv(B[i], Pref[i-1]);
rep(i, 0, q){
ll x;
int l, r;
cin >> x >> l >> r;
--r;
int c=Where[l];
if(x==0){
c=Non_zero[l];
}
bool BIG=false;
while(c<=Where[r] && !BIG){
if(D[c].r<=r){
if(D[c].l>=l)
x=max(x+D[c].A.back(), x*D[c].b);
else
x=max(x+D[c].A.back()-D[c].A[l-D[c].l-1], x*D[c].b);
}else{
if(D[c].l>=l)
x+=D[c].A[r-D[c].l];
else
x+=(D[c].A[r-D[c].l]-D[c].A[l-D[c].l-1]);
}
if(x>=P){
x%=P;
BIG=true;
}
++c;
}
if(c==sz(D) || D[c].l>r){
cout << x << "\n";
continue;
}
int d=D[c].l;
pair<ll,ll> t=pair<ll,ll>(1, 0);
if(d>0){
t=Inv(Pref[d-1]);
}
pair<ll,ll> t2=Conv(Pref[r], t);
x=(t2.ST*x+t2.ND)%P;
cout << x << "\n";
}
return;
}
int main()
{
ios_base::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 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 | #include<bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define ST first #define ND second #define PB push_back using ll = long long; using ld = long double; using pi = pair<int,int>; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pll=pair<ll,ll>; #ifdef LOCAL #define DTP(x, y) \ auto operator<<(auto &o, auto a)->decltype(y, o) \ { \ o << "("; \ x; \ return o << ")"; \ } DTP(o << a.first << ", " << a.second, a.second); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const ll P=1000'000'007; struct node{ ll b; int l, r; vll A; node(int x, int y, int z){ l=r=x; A.resize(1, y); b=z; } }; ll fPow(ll a, ll b){ if(!b) return 1; if(b&1) return (fPow(a, b-1)*a)%P; ll c=fPow(a, b/2); return (c*c)%P; } pair<ll,ll> Inv(pair<ll,ll> a){ pair<ll,ll> t; t.ST=fPow(a.ST, P-2); t.ND=(t.ST*a.ND)%P; t.ND=(P-t.ND)%P; return t; } pair<ll,ll> Conv(pair<ll,ll> a, pair<ll,ll> b){ pair<ll,ll> t; t.ST=(a.ST*b.ST)%P; t.ND=(a.ST*b.ND+a.ND)%P; return t; } void Solve() { int n, q; cin >> n >> q; vector<node> D; vi Where(n); vi Non_zero(n); vector<pair<ll,ll>> B(n); rep(i, 0, n){ int a, b; cin >> a >> b; B[i].ST=b; B[i].ND=a; if(i==0 || b>0 || D.back().b>0){ D.PB(node(i, a, b)); }else{ ++D.back().r; D.back().A.PB(D.back().A.back()+a); } Where[i]=sz(D)-1; } Non_zero[n-1]=Where[n-1]; for(int i=n-2; i>=0; --i){ Non_zero[i]=Non_zero[i+1]; if((D[Where[i]].l==i && D[Where[i]].A.back()>0) || ( D[Where[i]].l>i && D[Where[i]].A.back()-D[Where[i]].A[i-D[Where[i]].l-1]>0)) Non_zero[i]=Where[i]; } vector<pair<ll,ll>> Pref(n); rep(i, 0, n){ if(B[i].ST>1) B[i].ND=0; } Pref[0]=B[0]; rep(i, 1, n) Pref[i]=Conv(B[i], Pref[i-1]); rep(i, 0, q){ ll x; int l, r; cin >> x >> l >> r; --r; int c=Where[l]; if(x==0){ c=Non_zero[l]; } bool BIG=false; while(c<=Where[r] && !BIG){ if(D[c].r<=r){ if(D[c].l>=l) x=max(x+D[c].A.back(), x*D[c].b); else x=max(x+D[c].A.back()-D[c].A[l-D[c].l-1], x*D[c].b); }else{ if(D[c].l>=l) x+=D[c].A[r-D[c].l]; else x+=(D[c].A[r-D[c].l]-D[c].A[l-D[c].l-1]); } if(x>=P){ x%=P; BIG=true; } ++c; } if(c==sz(D) || D[c].l>r){ cout << x << "\n"; continue; } int d=D[c].l; pair<ll,ll> t=pair<ll,ll>(1, 0); if(d>0){ t=Inv(Pref[d-1]); } pair<ll,ll> t2=Conv(Pref[r], t); x=(t2.ST*x+t2.ND)%P; cout << x << "\n"; } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t=1; // cin >> t; while(t--) { Solve(); } return 0; } |
English