#include <algorithm>
#include <cstdio>
// #define DEBUG
constexpr int MX = 500005;
constexpr long long MOD = 1000000007;
// Problem input.
int n;
long long a[MX], b[MX];
// Precalculated data.
int end_of_1_segment_precalc[MX];
long long sum_of_segment_precalc[MX];
long long product_of_segment_precalc[MX];
long long sum_of_expression_precalc[MX];
long long inverse_precalc[MX];
long long SumOfSegment(int l, int r) {
if (l <= r) {
long long result = sum_of_segment_precalc[r];
if (l > 0) {
result -= sum_of_segment_precalc[l - 1];
}
return result;
} else {
#ifdef DEBUG
printf("ERROR: SumOfSegment(%d, %d) where l > r.\n", l, r);
#endif
return 0;
}
}
long long Solve(long long x, int l, int r) {
// Step 1. Make x >= MOD.
while (x < MOD && l <= r) {
#ifdef DEBUG
printf("x = %lld l = %d r = %d\n", x, l, r);
#endif
if (b[l] >= 2) {
#ifdef DEBUG
printf("b[l] >= 2 %lld\n", b[l]);
#endif
x = std::max(x + a[l], x * b[l]);
++l;
} else {
#ifdef DEBUG
printf("b[l] == 1 %lld\n", b[l]);
#endif
int end_of_1_segment = end_of_1_segment_precalc[l];
if (end_of_1_segment > r) {
end_of_1_segment = r;
}
x += SumOfSegment(l, end_of_1_segment);
l = end_of_1_segment + 1;
}
}
#ifdef DEBUG
printf("%d\n", x >= MOD);
printf("x = %lld l = %d r = %d\n", x, l, r);
#endif
x %= MOD;
if (l > r) {
return x;
}
// Step 2. Calculate the long expression.
long long const_part = sum_of_expression_precalc[l];
if (r + 1 < n) {
const_part = (const_part - sum_of_expression_precalc[r + 1] + MOD) % MOD;
const_part = const_part * inverse_precalc[r + 1] % MOD;
}
#ifdef DEBUG
printf("const_part = %lld\n", const_part);
#endif
long long var_part = (x * product_of_segment_precalc[l]) % MOD;
#ifdef DEBUG
printf("var_part = %lld\n", var_part);
#endif
if (r + 1 < n) {
var_part = (var_part * inverse_precalc[r + 1]) % MOD;
}
#ifdef DEBUG
printf("const_part = %lld var_part = %lld\n", const_part, var_part);
#endif
return (const_part + var_part) % MOD;
}
long long PartialResult(int i) {
if (b[i] >= 2) {
return 0;
} else {
long long result = a[i];
if (i + 1 < n) {
result = result * product_of_segment_precalc[i + 1] % MOD;
}
return result;
}
}
long long Inverse(long long x) {
long long p = MOD - 2;
long long result = 1;
while (p > 0) {
if (p & 1) {
result = (result * x) % MOD;
}
p >>= 1;
x = (x * x) % MOD;
}
return result;
}
void Precalculate() {
sum_of_segment_precalc[0] = a[0];
for (int i = 1; i < n; ++i) {
sum_of_segment_precalc[i] = sum_of_segment_precalc[i - 1] + a[i];
}
product_of_segment_precalc[n - 1] = b[n - 1];
inverse_precalc[n - 1] = Inverse(b[n - 1]);
for (int i = n - 2; i >= 0; --i) {
#ifdef DEBUG
printf("t[%d] = %lld * %lld\n", i, product_of_segment_precalc[i+1], b[i]);
#endif
product_of_segment_precalc[i] = (product_of_segment_precalc[i + 1] * b[i]) % MOD;
inverse_precalc[i] = Inverse(product_of_segment_precalc[i]);
}
sum_of_expression_precalc[n - 1] = PartialResult(n - 1);
for (int i = n - 2; i >= 0; --i) {
sum_of_expression_precalc[i] = (sum_of_expression_precalc[i + 1] + PartialResult(i)) % MOD;
}
end_of_1_segment_precalc[n - 1] = -1;
if (b[n - 1] == 1) {
end_of_1_segment_precalc[n - 1] = n - 1;
}
for (int i = n - 2; i >= 0; --i) {
if (b[i] >= 2) {
end_of_1_segment_precalc[i] = -1;
} else {
end_of_1_segment_precalc[i] = std::max(i, end_of_1_segment_precalc[i + 1]);
}
}
#ifdef DEBUG
for (int i = 0; i < n; ++i) {
printf("end_of_1_segment_precalc[%d]\t=\t%d\n", i, end_of_1_segment_precalc[i]);
printf("product_of_segment_precalc[%d]\t=\t%lld\n", i, product_of_segment_precalc[i]);
printf("sum_of_expression_precalc[%d]\t=\t%lld\n", i, sum_of_expression_precalc[i]);
printf("iiiiiiiinverse_precalc[%d]\t=\t%lld\n", i, inverse_precalc[i]);
printf("==================================================\n");
}
#endif
}
int main() {
int q;
scanf("%d%d", &n, &q);
for (int i = 0; i < n; ++i) {
scanf("%lld%lld", &a[i], &b[i]);
}
Precalculate();
while (q > 0) {
long long x;
int l, r;
scanf("%lld%d%d", &x, &l, &r);
--r;
printf("%lld\n", Solve(x, l, r));
--q;
}
}
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | #include <algorithm> #include <cstdio> // #define DEBUG constexpr int MX = 500005; constexpr long long MOD = 1000000007; // Problem input. int n; long long a[MX], b[MX]; // Precalculated data. int end_of_1_segment_precalc[MX]; long long sum_of_segment_precalc[MX]; long long product_of_segment_precalc[MX]; long long sum_of_expression_precalc[MX]; long long inverse_precalc[MX]; long long SumOfSegment(int l, int r) { if (l <= r) { long long result = sum_of_segment_precalc[r]; if (l > 0) { result -= sum_of_segment_precalc[l - 1]; } return result; } else { #ifdef DEBUG printf("ERROR: SumOfSegment(%d, %d) where l > r.\n", l, r); #endif return 0; } } long long Solve(long long x, int l, int r) { // Step 1. Make x >= MOD. while (x < MOD && l <= r) { #ifdef DEBUG printf("x = %lld l = %d r = %d\n", x, l, r); #endif if (b[l] >= 2) { #ifdef DEBUG printf("b[l] >= 2 %lld\n", b[l]); #endif x = std::max(x + a[l], x * b[l]); ++l; } else { #ifdef DEBUG printf("b[l] == 1 %lld\n", b[l]); #endif int end_of_1_segment = end_of_1_segment_precalc[l]; if (end_of_1_segment > r) { end_of_1_segment = r; } x += SumOfSegment(l, end_of_1_segment); l = end_of_1_segment + 1; } } #ifdef DEBUG printf("%d\n", x >= MOD); printf("x = %lld l = %d r = %d\n", x, l, r); #endif x %= MOD; if (l > r) { return x; } // Step 2. Calculate the long expression. long long const_part = sum_of_expression_precalc[l]; if (r + 1 < n) { const_part = (const_part - sum_of_expression_precalc[r + 1] + MOD) % MOD; const_part = const_part * inverse_precalc[r + 1] % MOD; } #ifdef DEBUG printf("const_part = %lld\n", const_part); #endif long long var_part = (x * product_of_segment_precalc[l]) % MOD; #ifdef DEBUG printf("var_part = %lld\n", var_part); #endif if (r + 1 < n) { var_part = (var_part * inverse_precalc[r + 1]) % MOD; } #ifdef DEBUG printf("const_part = %lld var_part = %lld\n", const_part, var_part); #endif return (const_part + var_part) % MOD; } long long PartialResult(int i) { if (b[i] >= 2) { return 0; } else { long long result = a[i]; if (i + 1 < n) { result = result * product_of_segment_precalc[i + 1] % MOD; } return result; } } long long Inverse(long long x) { long long p = MOD - 2; long long result = 1; while (p > 0) { if (p & 1) { result = (result * x) % MOD; } p >>= 1; x = (x * x) % MOD; } return result; } void Precalculate() { sum_of_segment_precalc[0] = a[0]; for (int i = 1; i < n; ++i) { sum_of_segment_precalc[i] = sum_of_segment_precalc[i - 1] + a[i]; } product_of_segment_precalc[n - 1] = b[n - 1]; inverse_precalc[n - 1] = Inverse(b[n - 1]); for (int i = n - 2; i >= 0; --i) { #ifdef DEBUG printf("t[%d] = %lld * %lld\n", i, product_of_segment_precalc[i+1], b[i]); #endif product_of_segment_precalc[i] = (product_of_segment_precalc[i + 1] * b[i]) % MOD; inverse_precalc[i] = Inverse(product_of_segment_precalc[i]); } sum_of_expression_precalc[n - 1] = PartialResult(n - 1); for (int i = n - 2; i >= 0; --i) { sum_of_expression_precalc[i] = (sum_of_expression_precalc[i + 1] + PartialResult(i)) % MOD; } end_of_1_segment_precalc[n - 1] = -1; if (b[n - 1] == 1) { end_of_1_segment_precalc[n - 1] = n - 1; } for (int i = n - 2; i >= 0; --i) { if (b[i] >= 2) { end_of_1_segment_precalc[i] = -1; } else { end_of_1_segment_precalc[i] = std::max(i, end_of_1_segment_precalc[i + 1]); } } #ifdef DEBUG for (int i = 0; i < n; ++i) { printf("end_of_1_segment_precalc[%d]\t=\t%d\n", i, end_of_1_segment_precalc[i]); printf("product_of_segment_precalc[%d]\t=\t%lld\n", i, product_of_segment_precalc[i]); printf("sum_of_expression_precalc[%d]\t=\t%lld\n", i, sum_of_expression_precalc[i]); printf("iiiiiiiinverse_precalc[%d]\t=\t%lld\n", i, inverse_precalc[i]); printf("==================================================\n"); } #endif } int main() { int q; scanf("%d%d", &n, &q); for (int i = 0; i < n; ++i) { scanf("%lld%lld", &a[i], &b[i]); } Precalculate(); while (q > 0) { long long x; int l, r; scanf("%lld%d%d", &x, &l, &r); --r; printf("%lld\n", Solve(x, l, r)); --q; } } |
English