#include <algorithm>
#include <bitset>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) (int)(x).size()
#define CLEAR(tab) memset(tab, 0, sizeof(tab))
#define REP(x, n) for(int x = 0; x < (n); x++)
#define FOR(x, b, e) for(int x = (b); x <= (e); x++)
#define FORD(x, b, e) for(int x = (b); x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
typedef long long int LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int MXN = 2 * 500010;
const int C = 2 * 262144;
const int INF = 1000000001;
const LL MOD = 1000000007;
const LL MAX_V = 2 * MOD;
int n, q;
LL a[MXN], b[MXN];
LL add(LL a, LL b) {
if(a + b > MAX_V)
return MAX_V;
return a + b;
}
LL mult(LL a, LL b) {
if(!a || !b)
return 0;
if (a > MAX_V / b)
return MAX_V;
return a * b;
}
struct State {
bool overflow = 0;
LL value_exact;
LL value_mod;
State(LL x) {
value_exact = x;
value_mod = 0;
overflow = 0;
}
State(LL x, bool _) {
overflow = 1;
value_mod = x % MOD;
value_exact = 0;
}
};
struct Node {
LL a, b;
LL a_overflow, b_overflow;
LL max_d;
} tree[2 * C + 100];
Node make_node(int k) {
Node res;
if(b[k] < 2) {
res.a = res.a_overflow = 1;
res.b = res.b_overflow = a[k];
res.max_d = 0;
}
else {
res.a = res.a_overflow = b[k];
res.b = res.b_overflow = 0;
res.max_d = max((a[k] - 1) / (b[k] - 1), 0LL);
}
return res;
}
Node merge(Node &x, Node &y) {
Node res;
res.a = (x.a * y.a) % MOD;
res.b = ((x.b * y.a) % MOD + y.b) % MOD;
res.a_overflow = mult(x.a_overflow, y.a_overflow);
res.b_overflow = add(mult(x.b_overflow, y.a_overflow), y.b_overflow);
res.max_d = max(x.max_d, y.max_d);
return res;
}
State calc_subtree(int w, State state) {
if(state.overflow)
return State(((state.value_mod * tree[w].a) % MOD + tree[w].b) % MOD, 1);
LL tmp = add(mult(state.value_exact, tree[w].a_overflow), tree[w].b_overflow);
if(tmp >= MOD)
return State(((((state.value_exact * tree[w].a) % MOD) + tree[w].b) % MOD), 1);
return State(tmp);
}
State calc_inner(int w, int L, int R, int l, int r, State state) {
if(state.overflow || state.value_exact > tree[w].max_d)
return calc_subtree(w, state);
if(L == R) {
if (state.value_exact + a[L] >= MOD)
return State(state.value_exact + a[L], 1);
return State(state.value_exact + a[L]);
}
int S = (L + R) / 2;
state = calc_inner(2 * w, L, S, l, r, state);
return calc_inner(2 * w + 1, S + 1, R, l, r, state);
}
State calc(int w, int L, int R, int l, int r, State state) {
if(l <= L && R <= r) {
if(state.overflow || state.value_exact > tree[w].max_d)
return calc_subtree(w, state);
return calc_inner(w, L, R, l, r, state);
}
int S = (L + R) / 2;
if(l <= S)
state = calc(2 * w, L, S, l, r, state);
if(r > S)
state = calc(2 * w + 1, S + 1, R, l, r, state);
return state;
}
int main() {
scanf("%d %d", &n, &q);
FOR(i, 1, n)
scanf("%lld %lld", &a[i], &b[i]);
FOR(i, C, 2 * C + 1)
tree[i] = make_node(i - C);
FORD(i, C - 1, 1)
tree[i] = merge(tree[2 * i], tree[2 * i + 1]);
FOR(i, 1, q) {
int x, l, r;
scanf("%d %d %d", &x, &l, &r);
State state = State(x);
state = calc(1, 0, C - 1, l + 1, r, state);
printf("%lld\n", state.overflow ? state.value_mod : state.value_exact);
}
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 | #include <algorithm> #include <bitset> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define PB push_back #define MP make_pair #define F first #define S second #define ALL(x) (x).begin(),(x).end() #define SIZE(x) (int)(x).size() #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define REP(x, n) for(int x = 0; x < (n); x++) #define FOR(x, b, e) for(int x = (b); x <= (e); x++) #define FORD(x, b, e) for(int x = (b); x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) typedef long long int LL; typedef pair<int,int> PII; typedef vector<int> VI; const int MXN = 2 * 500010; const int C = 2 * 262144; const int INF = 1000000001; const LL MOD = 1000000007; const LL MAX_V = 2 * MOD; int n, q; LL a[MXN], b[MXN]; LL add(LL a, LL b) { if(a + b > MAX_V) return MAX_V; return a + b; } LL mult(LL a, LL b) { if(!a || !b) return 0; if (a > MAX_V / b) return MAX_V; return a * b; } struct State { bool overflow = 0; LL value_exact; LL value_mod; State(LL x) { value_exact = x; value_mod = 0; overflow = 0; } State(LL x, bool _) { overflow = 1; value_mod = x % MOD; value_exact = 0; } }; struct Node { LL a, b; LL a_overflow, b_overflow; LL max_d; } tree[2 * C + 100]; Node make_node(int k) { Node res; if(b[k] < 2) { res.a = res.a_overflow = 1; res.b = res.b_overflow = a[k]; res.max_d = 0; } else { res.a = res.a_overflow = b[k]; res.b = res.b_overflow = 0; res.max_d = max((a[k] - 1) / (b[k] - 1), 0LL); } return res; } Node merge(Node &x, Node &y) { Node res; res.a = (x.a * y.a) % MOD; res.b = ((x.b * y.a) % MOD + y.b) % MOD; res.a_overflow = mult(x.a_overflow, y.a_overflow); res.b_overflow = add(mult(x.b_overflow, y.a_overflow), y.b_overflow); res.max_d = max(x.max_d, y.max_d); return res; } State calc_subtree(int w, State state) { if(state.overflow) return State(((state.value_mod * tree[w].a) % MOD + tree[w].b) % MOD, 1); LL tmp = add(mult(state.value_exact, tree[w].a_overflow), tree[w].b_overflow); if(tmp >= MOD) return State(((((state.value_exact * tree[w].a) % MOD) + tree[w].b) % MOD), 1); return State(tmp); } State calc_inner(int w, int L, int R, int l, int r, State state) { if(state.overflow || state.value_exact > tree[w].max_d) return calc_subtree(w, state); if(L == R) { if (state.value_exact + a[L] >= MOD) return State(state.value_exact + a[L], 1); return State(state.value_exact + a[L]); } int S = (L + R) / 2; state = calc_inner(2 * w, L, S, l, r, state); return calc_inner(2 * w + 1, S + 1, R, l, r, state); } State calc(int w, int L, int R, int l, int r, State state) { if(l <= L && R <= r) { if(state.overflow || state.value_exact > tree[w].max_d) return calc_subtree(w, state); return calc_inner(w, L, R, l, r, state); } int S = (L + R) / 2; if(l <= S) state = calc(2 * w, L, S, l, r, state); if(r > S) state = calc(2 * w + 1, S + 1, R, l, r, state); return state; } int main() { scanf("%d %d", &n, &q); FOR(i, 1, n) scanf("%lld %lld", &a[i], &b[i]); FOR(i, C, 2 * C + 1) tree[i] = make_node(i - C); FORD(i, C - 1, 1) tree[i] = merge(tree[2 * i], tree[2 * i + 1]); FOR(i, 1, q) { int x, l, r; scanf("%d %d %d", &x, &l, &r); State state = State(x); state = calc(1, 0, C - 1, l + 1, r, state); printf("%lld\n", state.overflow ? state.value_mod : state.value_exact); } return 0; } |
English