// Author: Bartek Knapik
#include<cstdio>
const unsigned long long MOD = 1000000007ULL;
const int MAXN = 500009;
using namespace std;
struct State
{
bool large;
long long value;
};
int n, q, l, r;
unsigned long long a[MAXN], b[MAXN], value;
State state;
void step(State& state, int step)
{
unsigned long long val = state.value;
bool large = state.large;
if (b[step] == 1 || (!large && val + a[step] > val * b[step]))
{
val += a[step];
if (val >= MOD)
{
val %= MOD;
large = true;
}
}
else
{
val *= b[step];
if (val >= MOD)
{
val %= MOD;
large = true;
}
}
state.value = val;
state.large = large;
}
int main()
{
scanf("%d%d", &n, &q);
for (int i = 0; i < n; ++i)
scanf("%llu%llu", &a[i], &b[i]);
for (int i = 0; i < q; ++i)
{
scanf("%llu%d%d", &value, &l, &r);
state.large = false;
state.value = value;
for (int j = l; j < r; ++j)
step(state, j);
printf("%llu\n", state.value);
}
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 | // Author: Bartek Knapik #include<cstdio> const unsigned long long MOD = 1000000007ULL; const int MAXN = 500009; using namespace std; struct State { bool large; long long value; }; int n, q, l, r; unsigned long long a[MAXN], b[MAXN], value; State state; void step(State& state, int step) { unsigned long long val = state.value; bool large = state.large; if (b[step] == 1 || (!large && val + a[step] > val * b[step])) { val += a[step]; if (val >= MOD) { val %= MOD; large = true; } } else { val *= b[step]; if (val >= MOD) { val %= MOD; large = true; } } state.value = val; state.large = large; } int main() { scanf("%d%d", &n, &q); for (int i = 0; i < n; ++i) scanf("%llu%llu", &a[i], &b[i]); for (int i = 0; i < q; ++i) { scanf("%llu%d%d", &value, &l, &r); state.large = false; state.value = value; for (int j = l; j < r; ++j) step(state, j); printf("%llu\n", state.value); } return 0; } |
English