/*
Runda 1 -
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<queue>
#include<stack>
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <chrono>
using namespace std;
using namespace __gnu_pbds;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<LL> VLL;
typedef vector<LD> VLD;
typedef vector<VLL > VVLL;
typedef vector<VLD > VVLD;
typedef vector<PII > VPII;
typedef vector<PLL > VPLL;
template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
#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 REP(x, n) for(int x=0; x<(n); ++x)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second
#define THIS (*this)
#define LSB(x) (x & -x)
#define SQR(x) ((x)*(x))
LL mm = 1e9 + 7;
struct node {
VI l, h, mul, add;
node(int sz = 0) {
l.reserve(sz);
h.reserve(sz);
mul.reserve(sz);
add.reserve(sz);
}
void addElem(int _l, int _h, int _mul, int _add) {
l.PB(_l); h.PB(_h); mul.PB(_mul); add.PB(_add);
}
int find(int x) {
return lower_bound(ALL(h), x) - h.begin();
}
int size() { return l.size(); }
void print() {
REP(i, l.size()) printf("[%d %d] %d * x + %d\n", l[i], h[i], mul[i], add[i]);
}
};
class RangeTree {
private:
vector<node > el;
int s = 1;
node merge(node &a, node &b) {
if (b.size() == 0) return a;
node ans(2 * a.size());
REP(i, a.size()) {
int vlo = min(1LL * a.l[i] * a.mul[i] + a.add[i], mm);
int vhi = min(1LL * a.h[i] * a.mul[i] + a.add[i], mm);
int ii = b.find(vlo);
int jj = b.find(vhi);
if (ii == jj) ans.addElem(a.l[i], a.h[i], 1LL * a.mul[i] * b.mul[ii] % mm, (1LL * a.add[i] * b.mul[ii] + b.add[ii]) % mm);
else {
int ilo = a.l[i], thi;
while (ii < jj) {
int tlo = ilo;
thi = a.h[i];
while (tlo + 1 < thi) {
int mid = (tlo + thi) / 2;
if (b.find(min(1LL *mid * a.mul[i] + a.add[i], mm)) == ii) tlo = mid;
else thi = mid;
}
ans.addElem(ilo, tlo, 1LL * a.mul[i] * b.mul[ii] % mm, (1LL * a.add[i] * b.mul[ii] + b.add[ii]) % mm);
ilo = thi; ii = b.find(min(1LL * thi * a.mul[i] + a.add[i], mm));
}
ans.addElem(thi, a.h[i], 1LL * a.mul[i] * b.mul[ii] % mm, (1LL * a.add[i] * b.mul[ii] + b.add[ii]) % mm);
}
}
return ans;
}
void update(int i) {
int l = i << 1, r = (i << 1) + 1;
el[i] = merge(el[l], el[r]);
}
public:
RangeTree(int size = 0) {
while (s < size) s <<= 1;
el.resize(2 * s);
}
void read(int p) {
p += s;
int ai, bi; cin >> ai >> bi;
el[p] = node(2);
if (bi < 2) el[p].addElem(0, mm, 1, ai);
else {
int ci = ai / (bi - 1);
el[p].addElem(0, ci, 1, ai);
el[p].addElem(ci + 1, mm, bi, 0);
}
}
void init() {
FORD(i, s - 1, 1) {
update(i);
}
}
int get(int l, int r, int x) {
l += s; r += s;
node vl, vr;
while (l < r) {
if (l & 1) {
if (vl.size() == 0) vl = el[l];
else vl = merge(vl, el[l]);
l++;
}
if (!(r & 1)) {
if (vr.size() == 0) vr = el[r];
else vr = merge(el[r], vr);
r--;
}
l >>= 1;
r >>= 1;
}
if (l == r) {
if (vl.size() == 0) vl = el[l];
else if (vr.size() == 0) vr = el[l];
else vl = merge(vl, el[l]);
}
if (vr.size() > 0) vl = merge(vl, vr);
int ii = vl.find(x);
return (1LL * x * vl.mul[ii] + vl.add[ii]) % mm;
}
LL size() { return s; }
void print() {
FORD(i, 2 * s - 1, 1) {
printf("node i: %d\n", i);
el[i].print();
}
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
int n, q; cin >> n >> q;
RangeTree tree(n);
REP(i, n) tree.read(i);
tree.init();
while (q--) {
int x, l, r; cin >> x >> l >> r;
cout << tree.get(l, r - 1, x) << '\n';
}
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | /* Runda 1 - */ #include<cstdio> #include<iostream> #include<algorithm> #include<string> #include<vector> #include<cmath> #include<queue> #include<queue> #include<stack> #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #include <chrono> using namespace std; using namespace __gnu_pbds; typedef vector<int> VI; typedef vector<VI> VVI; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef vector<LL> VLL; typedef vector<LD> VLD; typedef vector<VLL > VVLL; typedef vector<VLD > VVLD; typedef vector<PII > VPII; typedef vector<PLL > VPLL; template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>; #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 REP(x, n) for(int x=0; x<(n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second #define THIS (*this) #define LSB(x) (x & -x) #define SQR(x) ((x)*(x)) LL mm = 1e9 + 7; struct node { VI l, h, mul, add; node(int sz = 0) { l.reserve(sz); h.reserve(sz); mul.reserve(sz); add.reserve(sz); } void addElem(int _l, int _h, int _mul, int _add) { l.PB(_l); h.PB(_h); mul.PB(_mul); add.PB(_add); } int find(int x) { return lower_bound(ALL(h), x) - h.begin(); } int size() { return l.size(); } void print() { REP(i, l.size()) printf("[%d %d] %d * x + %d\n", l[i], h[i], mul[i], add[i]); } }; class RangeTree { private: vector<node > el; int s = 1; node merge(node &a, node &b) { if (b.size() == 0) return a; node ans(2 * a.size()); REP(i, a.size()) { int vlo = min(1LL * a.l[i] * a.mul[i] + a.add[i], mm); int vhi = min(1LL * a.h[i] * a.mul[i] + a.add[i], mm); int ii = b.find(vlo); int jj = b.find(vhi); if (ii == jj) ans.addElem(a.l[i], a.h[i], 1LL * a.mul[i] * b.mul[ii] % mm, (1LL * a.add[i] * b.mul[ii] + b.add[ii]) % mm); else { int ilo = a.l[i], thi; while (ii < jj) { int tlo = ilo; thi = a.h[i]; while (tlo + 1 < thi) { int mid = (tlo + thi) / 2; if (b.find(min(1LL *mid * a.mul[i] + a.add[i], mm)) == ii) tlo = mid; else thi = mid; } ans.addElem(ilo, tlo, 1LL * a.mul[i] * b.mul[ii] % mm, (1LL * a.add[i] * b.mul[ii] + b.add[ii]) % mm); ilo = thi; ii = b.find(min(1LL * thi * a.mul[i] + a.add[i], mm)); } ans.addElem(thi, a.h[i], 1LL * a.mul[i] * b.mul[ii] % mm, (1LL * a.add[i] * b.mul[ii] + b.add[ii]) % mm); } } return ans; } void update(int i) { int l = i << 1, r = (i << 1) + 1; el[i] = merge(el[l], el[r]); } public: RangeTree(int size = 0) { while (s < size) s <<= 1; el.resize(2 * s); } void read(int p) { p += s; int ai, bi; cin >> ai >> bi; el[p] = node(2); if (bi < 2) el[p].addElem(0, mm, 1, ai); else { int ci = ai / (bi - 1); el[p].addElem(0, ci, 1, ai); el[p].addElem(ci + 1, mm, bi, 0); } } void init() { FORD(i, s - 1, 1) { update(i); } } int get(int l, int r, int x) { l += s; r += s; node vl, vr; while (l < r) { if (l & 1) { if (vl.size() == 0) vl = el[l]; else vl = merge(vl, el[l]); l++; } if (!(r & 1)) { if (vr.size() == 0) vr = el[r]; else vr = merge(el[r], vr); r--; } l >>= 1; r >>= 1; } if (l == r) { if (vl.size() == 0) vl = el[l]; else if (vr.size() == 0) vr = el[l]; else vl = merge(vl, el[l]); } if (vr.size() > 0) vl = merge(vl, vr); int ii = vl.find(x); return (1LL * x * vl.mul[ii] + vl.add[ii]) % mm; } LL size() { return s; } void print() { FORD(i, 2 * s - 1, 1) { printf("node i: %d\n", i); el[i].print(); } } }; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n, q; cin >> n >> q; RangeTree tree(n); REP(i, n) tree.read(i); tree.init(); while (q--) { int x, l, r; cin >> x >> l >> r; cout << tree.get(l, r - 1, x) << '\n'; } return 0; } |
English