思路:
先同时除以x,然后用凸壳维护,答案二分一下就行了
c o d e code code
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const long long MAXN = 5e5 + 10;
long long n, q, tota, totb;
pair<long long, long long> a[MAXN], b[MAXN], ca[MAXN], cb[MAXN];
long long cj(long long x, long long x1, long long y, long long y1) {
return (y - y1) * (x - x1);
}
void doa() {
sort(a + 1, a + 1 + n);
for(long long i = 1; i <= n; i ++) {
while(tota >= 1 && ca[tota].first == a[i].first
|| tota >= 2 && cj(a[i].first, ca[tota - 1].first, ca[tota - 1].second, ca[tota].second) >=
cj(ca[tota].first, ca[tota - 1].first, ca[tota - 1].second, a[i].second))
tota --;
ca[++ tota] = a[i];
}
}
void dob() {
sort(b + 1, b + 1 + n);
for(long long i = 1; i <= n; i ++) {
while(totb >= 1 && cb[totb].first == b[i].first
|| totb >= 2 && cj(b[i].first, cb[totb - 1].first, cb[totb - 1].second, cb[totb].second) >=
cj(cb[totb].first, cb[totb - 1].first, cb[totb - 1].second, b[i].second))
totb --;
cb[++ totb] = b[i];
}
}
int main() {
// freopen("a.in", "r", stdin);
// freopen("a.out", "w", stdout);
scanf("%lld%lld", &n, &q);
for(long long i = 1; i <= n; i ++) {
long long x, y;
scanf("%lld%lld", &x, &y);
a[i] = make_pair(x, y);
b[i] = make_pair(-x, -y);
}
doa();
dob();
while(q --) {
long long x;
scanf("%lld", &x);
if(x > 0) {
long long l = 1, r = tota;
while(l < r) {
long long mid = l + r >> 1;
if(ca[mid].first * x + ca[mid].second > ca[mid + 1].first * x + ca[mid + 1].second)
r = mid;
else l = mid + 1;
}
printf("%lld\n", ca[l].first * x * x + ca[l].second * x);
}
else if(x < 0) {
long long l = 1, r = totb;
while(l < r) {
long long mid = l + r >> 1;
if(cb[mid].first * x + cb[mid].second > cb[mid + 1].first * x + cb[mid + 1].second)
r = mid;
else l = mid + 1;
}
printf("%lld\n", - cb[l].first * x * x - cb[l].second * x);
}
else printf("0\n");
}
return 0;
}