首先有最大字段和的做法, 面积用水平宽乘以铅锤高
对于一个斜率为k的询问, 就是求一个最大的 , 或者反过来
如果最大字段和转化为前缀和呢, 就是选两个前缀和 (a1, a2), 使得(y2 - y1) - (x2 - x1) * k最大
于是让 y2 - x2 * k 尽量大, y1 - x1 * k 尽量小
画一下图, 发现这两个点是最优的
然后维护两个凸包, 二分查找就可以了
#include<bits/stdc++.h>
#define N 1000050
#define LL long long
using namespace std;
int read(){
int cnt = 0; char ch = 0;
while(!isdigit(ch)) ch = getchar();
while(isdigit(ch)) cnt = cnt*10 + (ch-'0'), ch = getchar();
return cnt;
}
LL x[N], y[N], Up[N], Down[N], s1, s2;
int n, m;
double slope(int i, int j){
return 1.0 * (y[j] - y[i]) / (x[j] - x[i]);
}
void prework(){
Up[++s1] = 0;
for(int i=1; i<=n; i++){
while(s1 > 1 && slope(Up[s1-1], Up[s1]) < slope(Up[s1], i)) s1--;
Up[++s1] = i;
}
Down[++s2] = 0;
for(int i=1; i<=n; i++){
while(s2 > 1 && slope(Down[s2-1], Down[s2]) > slope(Down[s2], i)) s2--;
Down[++s2] = i;
}
}
int Ask1(double k){
int l = 1, r = s1;
while(l < r){
int mid = (l+r) >> 1;
if(slope(Up[mid], Up[mid+1]) <= k) r = mid;
else l = mid+1;
} return Up[l];
}
int Ask2(double k){
int l = 1, r = s2;
while(l < r){
int mid = (l+r) >> 1;
if(slope(Down[mid], Down[mid+1]) >= k) r = mid;
else l = mid+1;
} return Down[l];
}
int main(){
n = read();
for(int i=1; i<=n; i++){
x[i] = x[i-1] + read();
y[i] = y[i-1] + read();
} prework();
m = read();
while(m--){
int x1 = read(), y1 = read(); double k = y1 * 1.0 / x1;
int p1 = Ask1(k), p2 = Ask2(k);
printf("%lld\n", (LL)((((y[p1] - y[p2]) - k * (x[p1] - x[p2])) * x1) + 0.5));
} return 0;
}