#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto &operator<<(auto &o, pair<auto, auto> p) {
return o << "(" << p.first << ", " << p.second <<")";
}
auto operator<<(auto &o, auto x)-> decltype(x.end(), o) {
o << "{";int i = 0;
for(auto e : x) o << ", "+!i++<<e;
return o <<"}";
}
#define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x)
#else
#define debug(...) {}
#endif
#define int long long
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define fi first
#define se second
typedef pair <int, int> pii;
constexpr int MOD = 1e9+7;
struct tree {
vector <pii> data;
int lc;
pii merge(pii &a, pii &b) {
int r1 = (a.fi * b.se + b.fi)%MOD;
int r2 = (a.se * b.se)%MOD;
return {r1, r2};
}
tree (vector <pii> &inp) {
for (lc = 1; lc < sz(inp); lc *= 2)
;
data = vector <pii> (lc*2 + 7, {0, 1});
for (int i=0; i<sz(inp); ++i) {
if (inp[i].se == 1) {
data[i + lc] = inp[i];
}
else {
data[i + lc] = {0, inp[i].se};
}
}
for (int i=lc-1; i>0; --i) {
data[i] = merge(data[i*2], data[i*2+1]);
}
}
pii query(int l, int r) {
if (r < l)
return {0, 1};
return query(1, 0, lc-1, l, r);
}
pii query(int v, int a, int b, int l, int r) {
if (l <= a && b <= r) {
return data[v];
}
if (r < a || b < l) {
return {0, 1};
}
int m = (a + b)/2;
pii left = query(v*2, a, m, l, r);
pii right = query(v*2+1, m+1, b, l, r);
return merge(left, right);
}
};
void test() {
debug("TC______________________");
int n, q; cin>>n>>q;
vector <pii> dates(n);
vector <int> pref(n);
for (int i=0; i<n; ++i) {
cin>>dates[i].fi>>dates[i].se;
pref[i] = (i ? pref[i-1] : 0) + dates[i].fi;
}
vector <int> first_mul(n+1);
first_mul[n] = n;
for (int i=n-1; i>=0; --i) {
if (dates[i].se == 1) {
first_mul[i] = first_mul[i+1];
}
else {
first_mul[i] = i;
}
}
debug(first_mul);
tree T(dates);
debug(T.data);
auto get_sum = [&](int l, int r) -> int {
if (r < l) return 0;
if (l == 0) return pref[r];
return pref[r] - pref[l-1];
};
for (int i=0; i<q; ++i) {
int x, l, r; cin>>x>>l>>r;
r--;
debug(x, l, r);
while (l <= r) {
debug(x, l);
x = max(x + dates[l].fi, x * dates[l].se);
l++;
if (x > MOD || l > r) {
break;
}
int next = min(r+1, first_mul[l]);
debug(next);
int add = get_sum(l, next-1);
x += add;
l = next;
if (x > MOD) {
break;
}
}
debug(l, r);
x %= MOD;
pii rest = T.query(l, r);
debug(rest);
x = (x * rest.se)%MOD;
x = (x + rest.fi)%MOD;
cout<<x<<'\n';
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1;
while (t--) {
test();
}
}
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 | #include <bits/stdc++.h> using namespace std; #ifdef DEBUG auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.first << ", " << p.second <<")"; } auto operator<<(auto &o, auto x)-> decltype(x.end(), o) { o << "{";int i = 0; for(auto e : x) o << ", "+!i++<<e; return o <<"}"; } #define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif #define int long long #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() #define pb push_back #define fi first #define se second typedef pair <int, int> pii; constexpr int MOD = 1e9+7; struct tree { vector <pii> data; int lc; pii merge(pii &a, pii &b) { int r1 = (a.fi * b.se + b.fi)%MOD; int r2 = (a.se * b.se)%MOD; return {r1, r2}; } tree (vector <pii> &inp) { for (lc = 1; lc < sz(inp); lc *= 2) ; data = vector <pii> (lc*2 + 7, {0, 1}); for (int i=0; i<sz(inp); ++i) { if (inp[i].se == 1) { data[i + lc] = inp[i]; } else { data[i + lc] = {0, inp[i].se}; } } for (int i=lc-1; i>0; --i) { data[i] = merge(data[i*2], data[i*2+1]); } } pii query(int l, int r) { if (r < l) return {0, 1}; return query(1, 0, lc-1, l, r); } pii query(int v, int a, int b, int l, int r) { if (l <= a && b <= r) { return data[v]; } if (r < a || b < l) { return {0, 1}; } int m = (a + b)/2; pii left = query(v*2, a, m, l, r); pii right = query(v*2+1, m+1, b, l, r); return merge(left, right); } }; void test() { debug("TC______________________"); int n, q; cin>>n>>q; vector <pii> dates(n); vector <int> pref(n); for (int i=0; i<n; ++i) { cin>>dates[i].fi>>dates[i].se; pref[i] = (i ? pref[i-1] : 0) + dates[i].fi; } vector <int> first_mul(n+1); first_mul[n] = n; for (int i=n-1; i>=0; --i) { if (dates[i].se == 1) { first_mul[i] = first_mul[i+1]; } else { first_mul[i] = i; } } debug(first_mul); tree T(dates); debug(T.data); auto get_sum = [&](int l, int r) -> int { if (r < l) return 0; if (l == 0) return pref[r]; return pref[r] - pref[l-1]; }; for (int i=0; i<q; ++i) { int x, l, r; cin>>x>>l>>r; r--; debug(x, l, r); while (l <= r) { debug(x, l); x = max(x + dates[l].fi, x * dates[l].se); l++; if (x > MOD || l > r) { break; } int next = min(r+1, first_mul[l]); debug(next); int add = get_sum(l, next-1); x += add; l = next; if (x > MOD) { break; } } debug(l, r); x %= MOD; pii rest = T.query(l, r); debug(rest); x = (x * rest.se)%MOD; x = (x + rest.fi)%MOD; cout<<x<<'\n'; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) { test(); } } |
English