#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
o << "{";
for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
return o << "}"; }
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
return o << "(" << x.first << ", " << x.second << ")"; }
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) {}
#endif
#define x first
#define y second
#define ir(a, x, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using pii = pair<int, int>;
template <int MOD>
struct Modular {
int value;
static const int MOD_value = MOD;
Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;}
Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}
Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}
friend Modular mexp(Modular a, long long e) {
Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
return res;
}
friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }
Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
friend Modular operator+(Modular a, Modular const b) { return a += b; }
friend Modular operator-(Modular a, Modular const b) { return a -= b; }
friend Modular operator-(Modular const a) { return 0 - a; }
friend Modular operator*(Modular a, Modular const b) { return a *= b; }
friend Modular operator/(Modular a, Modular const b) { return a /= b; }
friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};
using mint = Modular<ll(1e9)+7>;
struct linfunc {
mint a = 1, b = 0;
// f(g(x))
static linfunc compose(linfunc f, linfunc g) {
return {
.a = f.a * g.a,
.b = g.b * f.a + f.b
};
}
linfunc invert() const {
mint ainv = inverse(a);
return {
.a = ainv,
.b = (-ainv) * b
};
}
mint apply(mint x) const {
return x * a + b;
}
};
const ll MOD = ll(1e9)+7;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, Q;
cin >> N >> Q;
vec<ll> pref(N+1);
vec<pii> t(N);
vec<linfunc> fpref(N+1);
vec<int> adduntil(N+1, N);
rep (n, 0, N) {
int a, b;
cin >> a >> b;
t[n] = { b, a };
pref[n+1] = pref[n] + a;
}
for (int n = N-1; n >= 0; n--) {
if (t[n].x == 1) {
adduntil[n] = adduntil[n+1];
} else {
adduntil[n] = n;
}
}
rep (n, 0, N) {
if (t[n].x == 1) {
fpref[n+1] = linfunc::compose({1, t[n].y}, fpref[n]);
} else {
fpref[n+1] = linfunc::compose({t[n].x, 0}, fpref[n]);
}
debug(fpref[n+1].a, fpref[n+1].b);
}
while (Q--) {
ll x; int l, r;
cin >> x >> l >> r;
debug(l, r);
while (x < MOD && l < r) {
if (t[l].x == 1) {
debug("here");
// skip adding
int until = min(adduntil[l], r);
debug(until);
x += pref[until] - pref[l];
l = until;
} else {
x = max(t[l].x * x, x + t[l].y);
l++;
}
debug(l, x);
}
debug(x);
x %= MOD;
debug(fpref[r].a, fpref[r].b);
debug(fpref[l].a, fpref[l].b);
debug(l, r);
cout << fpref[r].apply(fpref[l].invert().apply(x)).value << "\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 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 | #include <bits/stdc++.h> using namespace std; #ifdef LOCAL auto operator<<(auto& o, auto x) -> decltype(x.first, o); auto operator<<(auto& o, auto x) -> decltype(x.end(), o) { o << "{"; for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y; return o << "}"; } auto operator<<(auto& o, auto x) -> decltype(x.first, o) { return o << "(" << x.first << ", " << x.second << ")"; } void __print(auto... x) { ((cerr << x << " "), ...) << endl; } #define debug(x...) __print("[" #x "]:", x) #else #define debug(...) {} #endif #define x first #define y second #define ir(a, x, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) (x).begin(), (x).end() using ll = long long; using pii = pair<int, int>; template <int MOD> struct Modular { int value; static const int MOD_value = MOD; Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;} Modular(long long a, long long b) : value(0){ *this += a; *this /= b;} Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;} Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;} Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;} friend Modular mexp(Modular a, long long e) { Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; } return res; } friend Modular inverse(Modular a) { return mexp(a, MOD - 2); } Modular& operator/=(Modular const& b) { return *this *= inverse(b); } friend Modular operator+(Modular a, Modular const b) { return a += b; } friend Modular operator-(Modular a, Modular const b) { return a -= b; } friend Modular operator-(Modular const a) { return 0 - a; } friend Modular operator*(Modular a, Modular const b) { return a *= b; } friend Modular operator/(Modular a, Modular const b) { return a /= b; } friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;} friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;} friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;} }; using mint = Modular<ll(1e9)+7>; struct linfunc { mint a = 1, b = 0; // f(g(x)) static linfunc compose(linfunc f, linfunc g) { return { .a = f.a * g.a, .b = g.b * f.a + f.b }; } linfunc invert() const { mint ainv = inverse(a); return { .a = ainv, .b = (-ainv) * b }; } mint apply(mint x) const { return x * a + b; } }; const ll MOD = ll(1e9)+7; int main() { cin.tie(0)->sync_with_stdio(0); int N, Q; cin >> N >> Q; vec<ll> pref(N+1); vec<pii> t(N); vec<linfunc> fpref(N+1); vec<int> adduntil(N+1, N); rep (n, 0, N) { int a, b; cin >> a >> b; t[n] = { b, a }; pref[n+1] = pref[n] + a; } for (int n = N-1; n >= 0; n--) { if (t[n].x == 1) { adduntil[n] = adduntil[n+1]; } else { adduntil[n] = n; } } rep (n, 0, N) { if (t[n].x == 1) { fpref[n+1] = linfunc::compose({1, t[n].y}, fpref[n]); } else { fpref[n+1] = linfunc::compose({t[n].x, 0}, fpref[n]); } debug(fpref[n+1].a, fpref[n+1].b); } while (Q--) { ll x; int l, r; cin >> x >> l >> r; debug(l, r); while (x < MOD && l < r) { if (t[l].x == 1) { debug("here"); // skip adding int until = min(adduntil[l], r); debug(until); x += pref[until] - pref[l]; l = until; } else { x = max(t[l].x * x, x + t[l].y); l++; } debug(l, x); } debug(x); x %= MOD; debug(fpref[r].a, fpref[r].b); debug(fpref[l].a, fpref[l].b); debug(l, r); cout << fpref[r].apply(fpref[l].invert().apply(x)).value << "\n"; } return 0; } |
English