#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (auto i=(a); i<(b); i++)
#define FORD(i, a, b) for (auto i=(a); i>(b); i--)
#define SZ(x) ((int)(x).size())
#define ITH_BIT(m, i) ((m)>>(i) & 1)
#define PPC(x) __builtin_popcount(x)
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = std::min(a, (T1)b); }
template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = std::max(a, (T1)b); }
const int maxN = 1 << 19, maxTS = maxN * 2, mod = 1'000'000'007;
using Matrix = std::array <std::array <long long, 2>, 2>;
const auto UNIT = Matrix {{{1, 0}, {0, 1}}};
Matrix mult(const Matrix& A, const Matrix& B)
{
Matrix C;
C[0][0] = (A[0][0] * B[0][0] + A[0][1] * B[1][0]) % mod;
C[0][1] = (A[0][0] * B[0][1] + A[0][1] * B[1][1]) % mod;
C[1][0] = (A[1][0] * B[0][0] + A[1][1] * B[1][0]) % mod;
C[1][1] = (A[1][0] * B[0][1] + A[1][1] * B[1][1]) % mod;
return C;
}
struct Tree
{
int offset, qbegin, qend;
Matrix T[maxTS], res;
void qpriv(int v, int left, int right)
{
if (left >= qend or right <= qbegin)
return;
if (left >= qbegin and right <= qend)
{
res = mult(res, T[v]);
return;
}
qpriv(v*2+1, (left + right) / 2, right);
qpriv(v * 2, left, (left + right) / 2);
}
Matrix multipLeaf(int b)
{
return Matrix {{{b, 0}, {0, 1}}};
}
Matrix additLeaf(int a)
{
return Matrix {{{1, a}, {0, 1}}};
}
void init(int n, int A[maxN], int B[maxN])
{
for (offset = 1; offset < n; offset *= 2) ;
FOR(i, 0, n)
T[offset + i] = B[i] == 1
? additLeaf(A[i])
: multipLeaf(B[i])
;
FOR(i, n, offset)
T[offset + i] = UNIT;
FORD(i, offset-1, 0)
T[i] = mult(T[i*2+1], T[i * 2]);
}
Matrix query(int a, int b)
{
qbegin = a + offset;
qend = b + offset;
res = UNIT;
qpriv(1, offset, offset * 2);
return res;
}
} tree;
int A[maxN], B[maxN], firstNoUnit[maxN];
long long praf[maxN];
long long sum(int a, int b)
{
return praf[b-1] - praf[a-1];
}
void prepare(int n)
{
FOR(i, 1, n+1)
praf[i] = praf[i-1] + A[i];
tree.init(n+1, A, B);
firstNoUnit[n+1] = n+1;
FORD(i, n, 0)
firstNoUnit[i] = B[i] == 1 ? firstNoUnit[i+1] : i;
}
void brute(int& a, int& b, long long& x)
{
while (x < mod and a < b)
{
if (B[a] == 1)
{
int c = std::min(firstNoUnit[a], b);
x += sum(a, c);
a = c;
}
else
{
x = std::max(x + A[a], x * B[a]);
a++;
}
}
x %= mod;
}
void solve()
{
int n, q;
scanf ("%d%d", &n, &q);
FOR(i, 1, n+1)
scanf ("%d%d", A+i, B+i);
prepare(n);
while (q--)
{
long long x;
int a, b;
scanf ("%lld%d%d", &x, &a, &b);
a++, b++;
brute(a, b, x);
auto mat = tree.query(a, b);
long long res = mat[0][0] * x + mat[0][1];
res %= mod;
printf("%lld\n", res);
}
}
int main()
{
int tc = 1;
// scanf ("%d", &tc);
FOR(cid, 1, tc+1)
{
// printf("Case #%d: ", cid);
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 | #pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (auto i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ITH_BIT(m, i) ((m)>>(i) & 1) #define PPC(x) __builtin_popcount(x) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = std::min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = std::max(a, (T1)b); } const int maxN = 1 << 19, maxTS = maxN * 2, mod = 1'000'000'007; using Matrix = std::array <std::array <long long, 2>, 2>; const auto UNIT = Matrix {{{1, 0}, {0, 1}}}; Matrix mult(const Matrix& A, const Matrix& B) { Matrix C; C[0][0] = (A[0][0] * B[0][0] + A[0][1] * B[1][0]) % mod; C[0][1] = (A[0][0] * B[0][1] + A[0][1] * B[1][1]) % mod; C[1][0] = (A[1][0] * B[0][0] + A[1][1] * B[1][0]) % mod; C[1][1] = (A[1][0] * B[0][1] + A[1][1] * B[1][1]) % mod; return C; } struct Tree { int offset, qbegin, qend; Matrix T[maxTS], res; void qpriv(int v, int left, int right) { if (left >= qend or right <= qbegin) return; if (left >= qbegin and right <= qend) { res = mult(res, T[v]); return; } qpriv(v*2+1, (left + right) / 2, right); qpriv(v * 2, left, (left + right) / 2); } Matrix multipLeaf(int b) { return Matrix {{{b, 0}, {0, 1}}}; } Matrix additLeaf(int a) { return Matrix {{{1, a}, {0, 1}}}; } void init(int n, int A[maxN], int B[maxN]) { for (offset = 1; offset < n; offset *= 2) ; FOR(i, 0, n) T[offset + i] = B[i] == 1 ? additLeaf(A[i]) : multipLeaf(B[i]) ; FOR(i, n, offset) T[offset + i] = UNIT; FORD(i, offset-1, 0) T[i] = mult(T[i*2+1], T[i * 2]); } Matrix query(int a, int b) { qbegin = a + offset; qend = b + offset; res = UNIT; qpriv(1, offset, offset * 2); return res; } } tree; int A[maxN], B[maxN], firstNoUnit[maxN]; long long praf[maxN]; long long sum(int a, int b) { return praf[b-1] - praf[a-1]; } void prepare(int n) { FOR(i, 1, n+1) praf[i] = praf[i-1] + A[i]; tree.init(n+1, A, B); firstNoUnit[n+1] = n+1; FORD(i, n, 0) firstNoUnit[i] = B[i] == 1 ? firstNoUnit[i+1] : i; } void brute(int& a, int& b, long long& x) { while (x < mod and a < b) { if (B[a] == 1) { int c = std::min(firstNoUnit[a], b); x += sum(a, c); a = c; } else { x = std::max(x + A[a], x * B[a]); a++; } } x %= mod; } void solve() { int n, q; scanf ("%d%d", &n, &q); FOR(i, 1, n+1) scanf ("%d%d", A+i, B+i); prepare(n); while (q--) { long long x; int a, b; scanf ("%lld%d%d", &x, &a, &b); a++, b++; brute(a, b, x); auto mat = tree.query(a, b); long long res = mat[0][0] * x + mat[0][1]; res %= mod; printf("%lld\n", res); } } int main() { int tc = 1; // scanf ("%d", &tc); FOR(cid, 1, tc+1) { // printf("Case #%d: ", cid); solve(); } return 0; } |
English