#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int64 MOD = 1000000007LL;
struct Line {
__int128 A, B;
Line(__int128 a=0, __int128 b=0):A(a),B(b){}
__int128 val(int64 x) const { return A*(__int128)x + B; }
};
bool bad(const Line &l1, const Line &l2, const Line &l3){
__int128 left = (l3.B - l1.B) * (l1.A - l2.A);
__int128 right = (l2.B - l1.B) * (l1.A - l3.A);
return left <= right;
}
struct Node {
vector<Line> hull;
};
int n,q;
vector<int64> a,b;
vector<Node> seg;
Node mergeNode(const Node &L, const Node &R){
vector<Line> tmp;
tmp.reserve(L.hull.size()*R.hull.size());
for(const Line &l : L.hull){
for(const Line &r : R.hull){
__int128 A = r.A * l.A;
__int128 B = r.A * l.B + r.B;
tmp.emplace_back(A,B);
}
}
sort(tmp.begin(), tmp.end(), [](const Line &x, const Line &y){
if(x.A != y.A) return x.A < y.A;
return x.B < y.B;
});
vector<Line> filtered;
for(auto &ln : tmp){
if(!filtered.empty() && filtered.back().A == ln.A){
if(filtered.back().B >= ln.B) continue;
else filtered.pop_back();
}
filtered.push_back(ln);
}
vector<Line> hull;
for(auto &ln : filtered){
while(hull.size() >= 2 && bad(hull[hull.size()-2], hull[hull.size()-1], ln))
hull.pop_back();
hull.push_back(ln);
}
Node res;
res.hull = move(hull);
return res;
}
void build(int idx, int l, int r){
if(l==r){
seg[idx].hull.clear();
seg[idx].hull.emplace_back((__int128)1, (__int128)a[l]);
seg[idx].hull.emplace_back((__int128)b[l], (__int128)0);
sort(seg[idx].hull.begin(), seg[idx].hull.end(), [](const Line &x, const Line &y){
if(x.A!=y.A) return x.A<y.A;
return x.B<y.B;
});
vector<Line> f;
for(auto &ln: seg[idx].hull){
if(!f.empty() && f.back().A==ln.A){
if(f.back().B >= ln.B) continue;
else f.pop_back();
}
f.push_back(ln);
}
vector<Line> hull;
for(auto &ln: f){
while(hull.size()>=2 && bad(hull[hull.size()-2], hull[hull.size()-1], ln))
hull.pop_back();
hull.push_back(ln);
}
seg[idx].hull = move(hull);
return;
}
int mid=(l+r)/2;
build(idx*2,l,mid);
build(idx*2+1,mid+1,r);
seg[idx] = mergeNode(seg[idx*2], seg[idx*2+1]);
}
Node queryNode(int idx, int l, int r, int ql, int qr){
if(ql>r || qr<l) return Node();
if(ql<=l && r<=qr) return seg[idx];
int mid=(l+r)/2;
Node left = queryNode(idx*2,l,mid,ql,qr);
Node right = queryNode(idx*2+1,mid+1,r,ql,qr);
if(left.hull.empty()) return right;
if(right.hull.empty()) return left;
return mergeNode(left,right);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>q;
a.resize(n+1);
b.resize(n+1);
for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
seg.assign(4*(n+5), Node());
build(1,1,n);
while(q--){
int64 x; int l,r;
cin>>x>>l>>r;
Node node = queryNode(1,1,n,l+1,r);
__int128 best = (__int128)x;
if(!node.hull.empty()){
int lo=0, hi=(int)node.hull.size()-1;
while(lo<hi){
int m = (lo+hi)/2;
__int128 v1 = node.hull[m].val(x);
__int128 v2 = node.hull[m+1].val(x);
if(v1 <= v2) lo = m+1;
else hi = m;
}
best = node.hull[lo].val(x);
}
int64 ans = (int64)(best % (__int128)MOD);
if(ans<0) ans += MOD;
cout<<ans<<"\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 | #include <bits/stdc++.h> using namespace std; using int64 = long long; const int64 MOD = 1000000007LL; struct Line { __int128 A, B; Line(__int128 a=0, __int128 b=0):A(a),B(b){} __int128 val(int64 x) const { return A*(__int128)x + B; } }; bool bad(const Line &l1, const Line &l2, const Line &l3){ __int128 left = (l3.B - l1.B) * (l1.A - l2.A); __int128 right = (l2.B - l1.B) * (l1.A - l3.A); return left <= right; } struct Node { vector<Line> hull; }; int n,q; vector<int64> a,b; vector<Node> seg; Node mergeNode(const Node &L, const Node &R){ vector<Line> tmp; tmp.reserve(L.hull.size()*R.hull.size()); for(const Line &l : L.hull){ for(const Line &r : R.hull){ __int128 A = r.A * l.A; __int128 B = r.A * l.B + r.B; tmp.emplace_back(A,B); } } sort(tmp.begin(), tmp.end(), [](const Line &x, const Line &y){ if(x.A != y.A) return x.A < y.A; return x.B < y.B; }); vector<Line> filtered; for(auto &ln : tmp){ if(!filtered.empty() && filtered.back().A == ln.A){ if(filtered.back().B >= ln.B) continue; else filtered.pop_back(); } filtered.push_back(ln); } vector<Line> hull; for(auto &ln : filtered){ while(hull.size() >= 2 && bad(hull[hull.size()-2], hull[hull.size()-1], ln)) hull.pop_back(); hull.push_back(ln); } Node res; res.hull = move(hull); return res; } void build(int idx, int l, int r){ if(l==r){ seg[idx].hull.clear(); seg[idx].hull.emplace_back((__int128)1, (__int128)a[l]); seg[idx].hull.emplace_back((__int128)b[l], (__int128)0); sort(seg[idx].hull.begin(), seg[idx].hull.end(), [](const Line &x, const Line &y){ if(x.A!=y.A) return x.A<y.A; return x.B<y.B; }); vector<Line> f; for(auto &ln: seg[idx].hull){ if(!f.empty() && f.back().A==ln.A){ if(f.back().B >= ln.B) continue; else f.pop_back(); } f.push_back(ln); } vector<Line> hull; for(auto &ln: f){ while(hull.size()>=2 && bad(hull[hull.size()-2], hull[hull.size()-1], ln)) hull.pop_back(); hull.push_back(ln); } seg[idx].hull = move(hull); return; } int mid=(l+r)/2; build(idx*2,l,mid); build(idx*2+1,mid+1,r); seg[idx] = mergeNode(seg[idx*2], seg[idx*2+1]); } Node queryNode(int idx, int l, int r, int ql, int qr){ if(ql>r || qr<l) return Node(); if(ql<=l && r<=qr) return seg[idx]; int mid=(l+r)/2; Node left = queryNode(idx*2,l,mid,ql,qr); Node right = queryNode(idx*2+1,mid+1,r,ql,qr); if(left.hull.empty()) return right; if(right.hull.empty()) return left; return mergeNode(left,right); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cin>>n>>q; a.resize(n+1); b.resize(n+1); for(int i=1;i<=n;i++) cin>>a[i]>>b[i]; seg.assign(4*(n+5), Node()); build(1,1,n); while(q--){ int64 x; int l,r; cin>>x>>l>>r; Node node = queryNode(1,1,n,l+1,r); __int128 best = (__int128)x; if(!node.hull.empty()){ int lo=0, hi=(int)node.hull.size()-1; while(lo<hi){ int m = (lo+hi)/2; __int128 v1 = node.hull[m].val(x); __int128 v2 = node.hull[m+1].val(x); if(v1 <= v2) lo = m+1; else hi = m; } best = node.hull[lo].val(x); } int64 ans = (int64)(best % (__int128)MOD); if(ans<0) ans += MOD; cout<<ans<<"\n"; } return 0; } |
English