#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define DEBUG
#ifdef DEBUG
template<typename T1,typename T2>auto& operator<<(ostream&o,pair<T1,T2>a){return o<<"("<<a.first<<", "<<a.second<<")";}
template<typename T,size_t...I>void pt(ostream&o,T t,index_sequence<I...>){o<<"(";(...,(o<<(I?", ":"")<< get<I>(t)));o<<")";}
template<typename...A>auto& operator<<(ostream&o,tuple<A...>t){pt(o,t,index_sequence_for<A...>{});return o;}
template<typename T,typename O>auto& operator<<(O&o,T a){o<<"{";for(auto b:a)o<<b<<", ";return o<<"}";}
#define db(x...) cerr << "\033[92m" << "[" #x "]: ", [](auto... args) { ((cerr << args << ", "),...) << "\033[0m" << "\n"; }(x)
#else
#define db(...)
#endif
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
template<class T>
using iset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using ld = long double;
using pll = pair<ll,ll>;
using vi = vector<int>;
mt19937 mrand(random_device{}());
ll rnd(ll l, ll r) { return l + mrand() % (r - l + 1);}
ll mod = 1e9+7;
int main() {
cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
ll n, q;
cin >> n >> q;
vector<pll> arr(n);
for(ll i = 0 ; i < n ; ++i) {
cin >> arr[i].F >> arr[i].S;
}
vector<ll> pref(n+1, 0);
for(ll i = 0 ; i < n ; ++i) {
pref[i+1] = pref[i] + arr[i].F;
}
ll K = sqrt(n);
vector<vector<pll>> decomp(n, vector<pll>(21));
for(ll i = 0 ; i < n ; ++i) {
if(arr[i].S == 1) decomp[i][0] = {1, arr[i].F};
else decomp[i][0] = {arr[i].S, 0};
}
ll b = 0;
for(ll len = 2 ; len <= n ; len <<= 1) {
b++;
for(ll i = 0 ; i+len <= n ; ++i) {
decomp[i][b].F = (decomp[i][b-1].F * decomp[i+(len/2)][b-1].F) % mod;
decomp[i][b].S = (decomp[i][b-1].S * decomp[i+(len/2)][b-1].F) % mod;
decomp[i][b].S = (decomp[i][b].S + decomp[i+(len/2)][b-1].S) % mod;
}
}
vector<ll> nxt(n, n);
if(arr[n-1].S > 1) nxt[n-1] = n-1;
for(ll i = n-2 ; i >= 0 ; --i) {
nxt[i] = nxt[i+1];
if(arr[i].S > 1) nxt[i] = i;
}
while(q--) {
ll x, l, r;
cin >> x >> l >> r;
while(l < r && l < n) {
ll rr = min(r, nxt[l]);
x += pref[rr] - pref[l];
l = rr;
if (x >= mod) {
x %= mod;
break;
}
x %= mod;
if(l == r || l == n) break;
if(x*arr[l].S > x + arr[l].F) x *= arr[l].S;
else x += arr[l].F;
if(x >= mod) {
l++;
x %= mod;
break;
}
x %= mod;
l++;
}
for(ll i = 20 ; i >= 0 ; --i) {
if(l+(1<<i) > r) continue;
x = (x * decomp[l][i].F) % mod;
x = (x + decomp[l][i].S) % mod;
l += (1<<i);
}
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 | #include <bits/stdc++.h> #include <bits/extc++.h> using namespace std; using namespace __gnu_pbds; #define DEBUG #ifdef DEBUG template<typename T1,typename T2>auto& operator<<(ostream&o,pair<T1,T2>a){return o<<"("<<a.first<<", "<<a.second<<")";} template<typename T,size_t...I>void pt(ostream&o,T t,index_sequence<I...>){o<<"(";(...,(o<<(I?", ":"")<< get<I>(t)));o<<")";} template<typename...A>auto& operator<<(ostream&o,tuple<A...>t){pt(o,t,index_sequence_for<A...>{});return o;} template<typename T,typename O>auto& operator<<(O&o,T a){o<<"{";for(auto b:a)o<<b<<", ";return o<<"}";} #define db(x...) cerr << "\033[92m" << "[" #x "]: ", [](auto... args) { ((cerr << args << ", "),...) << "\033[0m" << "\n"; }(x) #else #define db(...) #endif #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define F first #define S second template<class T> using iset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; using ll = long long; using ld = long double; using pll = pair<ll,ll>; using vi = vector<int>; mt19937 mrand(random_device{}()); ll rnd(ll l, ll r) { return l + mrand() % (r - l + 1);} ll mod = 1e9+7; int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); ll n, q; cin >> n >> q; vector<pll> arr(n); for(ll i = 0 ; i < n ; ++i) { cin >> arr[i].F >> arr[i].S; } vector<ll> pref(n+1, 0); for(ll i = 0 ; i < n ; ++i) { pref[i+1] = pref[i] + arr[i].F; } ll K = sqrt(n); vector<vector<pll>> decomp(n, vector<pll>(21)); for(ll i = 0 ; i < n ; ++i) { if(arr[i].S == 1) decomp[i][0] = {1, arr[i].F}; else decomp[i][0] = {arr[i].S, 0}; } ll b = 0; for(ll len = 2 ; len <= n ; len <<= 1) { b++; for(ll i = 0 ; i+len <= n ; ++i) { decomp[i][b].F = (decomp[i][b-1].F * decomp[i+(len/2)][b-1].F) % mod; decomp[i][b].S = (decomp[i][b-1].S * decomp[i+(len/2)][b-1].F) % mod; decomp[i][b].S = (decomp[i][b].S + decomp[i+(len/2)][b-1].S) % mod; } } vector<ll> nxt(n, n); if(arr[n-1].S > 1) nxt[n-1] = n-1; for(ll i = n-2 ; i >= 0 ; --i) { nxt[i] = nxt[i+1]; if(arr[i].S > 1) nxt[i] = i; } while(q--) { ll x, l, r; cin >> x >> l >> r; while(l < r && l < n) { ll rr = min(r, nxt[l]); x += pref[rr] - pref[l]; l = rr; if (x >= mod) { x %= mod; break; } x %= mod; if(l == r || l == n) break; if(x*arr[l].S > x + arr[l].F) x *= arr[l].S; else x += arr[l].F; if(x >= mod) { l++; x %= mod; break; } x %= mod; l++; } for(ll i = 20 ; i >= 0 ; --i) { if(l+(1<<i) > r) continue; x = (x * decomp[l][i].F) % mod; x = (x + decomp[l][i].S) % mod; l += (1<<i); } cout << x << "\n"; } return 0; } |
English