#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())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
#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(...) 2137
#endif
template<int M, int R>
struct Mod {
static const int mod = M, rt = R;
int x;
Mod(ll y = 0) : x(y % M) { x += (x < 0) * M; }
Mod& operator+=(Mod o) {
if ((x += o.x) >= M) x -= M;
return *this; }
Mod& operator-=(Mod o) {
if ((x -= o.x) < 0) x += M;
return *this; }
Mod& operator*=(Mod o) {
x = 1ll * x * o.x % M;
return *this; }
Mod& operator/=(Mod o) { return *this *= o.inv(); }
friend Mod operator+(Mod a, Mod b) { return a += b; }
friend Mod operator-(Mod a, Mod b) { return a -= b; }
friend Mod operator*(Mod a, Mod b) { return a *= b; }
friend Mod operator/(Mod a, Mod b) { return a /= b; }
auto operator<=>(const Mod&) const = default;
Mod pow(ll n) const {
Mod a = x, b = 1;
for (; n; n /= 2, a *= a) if (n % 2) b *= a;
return b; }
Mod inv() const { assert(x); return pow(M - 2); }
friend ostream& operator<<(ostream& os, Mod x) {
return os << x.x; }
};
using mint = Mod<1000000007, 5>;
using F = pair<mint, mint>;
F unit = {1, 0};
struct Tree {
typedef F T;
T f(T a, T b) { return {a.first * b.first, b.first * a.second + b.second}; } // (any associative fn)
vector<T> s; int n;
Tree(int N = 0, T def = unit) : s(2*N, def), n(N) {}
void update(int pos, T val) {
for (s[pos += n] = val; pos /= 2;)
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
T query(int b, int e) { // query [b, e)
T ra = unit, rb = unit;
for (b += n, e += n; b < e; b /= 2, e /= 2) {
if (b % 2) ra = f(ra, s[b++]);
if (e % 2) rb = f(s[--e], rb);
}
return f(ra, rb);
}
};
const int N = 500500;
const ll LIM = 1'000'000'007;
int n, q;
int a[N], b[N];
Tree t;
int nast[N];
ll suma[N];
int nz[N];
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
rep(i, 0, n) cin >> a[i] >> b[i];
t = Tree(n);
rep(i, 0, n) t.update(i, b[i] > 1 ? F(b[i], 0) : F(1, a[i]));
nast[n] = n;
nz[n] = n;
for (int i = n - 1; i >= 0; i--) {
if (b[i] > 1) {
nast[i] = i;
suma[i] = 0;
} else {
nast[i] = nast[i + 1];
suma[i] = suma[i + 1] + a[i];
}
if (a[i] > 0) nz[i] = i;
else nz[i] = nz[i + 1];
}
while (q--) {
ll x;
int l, r;
cin >> x >> l >> r;
if (x == 0) l = nz[l];
while (l < r) {
if (x >= LIM) {
auto f = t.query(l, r);
x = (f.first * x + f.second).x;
break;
}
if (b[l] > 1) {
x = max(x * b[l], x + a[l]);
l++;
} else {
if (nast[l] <= r) {
x += suma[l];
l = nast[l];
} else {
x += suma[l] - suma[r];
l = r;
}
}
}
x = mint(x).x;
cout << x << '\n';
}
}
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 | #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()) using ll = long long; using pii = pair<int, int>; using vi = vector<int>; #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(...) 2137 #endif template<int M, int R> struct Mod { static const int mod = M, rt = R; int x; Mod(ll y = 0) : x(y % M) { x += (x < 0) * M; } Mod& operator+=(Mod o) { if ((x += o.x) >= M) x -= M; return *this; } Mod& operator-=(Mod o) { if ((x -= o.x) < 0) x += M; return *this; } Mod& operator*=(Mod o) { x = 1ll * x * o.x % M; return *this; } Mod& operator/=(Mod o) { return *this *= o.inv(); } friend Mod operator+(Mod a, Mod b) { return a += b; } friend Mod operator-(Mod a, Mod b) { return a -= b; } friend Mod operator*(Mod a, Mod b) { return a *= b; } friend Mod operator/(Mod a, Mod b) { return a /= b; } auto operator<=>(const Mod&) const = default; Mod pow(ll n) const { Mod a = x, b = 1; for (; n; n /= 2, a *= a) if (n % 2) b *= a; return b; } Mod inv() const { assert(x); return pow(M - 2); } friend ostream& operator<<(ostream& os, Mod x) { return os << x.x; } }; using mint = Mod<1000000007, 5>; using F = pair<mint, mint>; F unit = {1, 0}; struct Tree { typedef F T; T f(T a, T b) { return {a.first * b.first, b.first * a.second + b.second}; } // (any associative fn) vector<T> s; int n; Tree(int N = 0, T def = unit) : s(2*N, def), n(N) {} void update(int pos, T val) { for (s[pos += n] = val; pos /= 2;) s[pos] = f(s[pos * 2], s[pos * 2 + 1]); } T query(int b, int e) { // query [b, e) T ra = unit, rb = unit; for (b += n, e += n; b < e; b /= 2, e /= 2) { if (b % 2) ra = f(ra, s[b++]); if (e % 2) rb = f(s[--e], rb); } return f(ra, rb); } }; const int N = 500500; const ll LIM = 1'000'000'007; int n, q; int a[N], b[N]; Tree t; int nast[N]; ll suma[N]; int nz[N]; int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> q; rep(i, 0, n) cin >> a[i] >> b[i]; t = Tree(n); rep(i, 0, n) t.update(i, b[i] > 1 ? F(b[i], 0) : F(1, a[i])); nast[n] = n; nz[n] = n; for (int i = n - 1; i >= 0; i--) { if (b[i] > 1) { nast[i] = i; suma[i] = 0; } else { nast[i] = nast[i + 1]; suma[i] = suma[i + 1] + a[i]; } if (a[i] > 0) nz[i] = i; else nz[i] = nz[i + 1]; } while (q--) { ll x; int l, r; cin >> x >> l >> r; if (x == 0) l = nz[l]; while (l < r) { if (x >= LIM) { auto f = t.query(l, r); x = (f.first * x + f.second).x; break; } if (b[l] > 1) { x = max(x * b[l], x + a[l]); l++; } else { if (nast[l] <= r) { x += suma[l]; l = nast[l]; } else { x += suma[l] - suma[r]; l = r; } } } x = mint(x).x; cout << x << '\n'; } } |
English