题意:求买纸的最小值,用best[i]数组记录第i之后的最优值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 100010;
long long p[MAXN],s[MAXN],best[MAXN];
int n,m;
int main(){
int cas;
scanf("%d",&cas);
while (cas--){
memset(s,0,sizeof(s));
memset(p,0,sizeof(p));
scanf("%d%d",&n,&m);
int a=1,b=1;
for (int i = 1; i <= 2*n; i++){
if (i & 1)
scanf("%lld",&s[a++]);
else scanf("%lld",&p[b++]);
}
long long Min = p[n] * s[n];
best[n] = Min;
for (int i = n-1; i > 1; i--){
Min = min(Min,s[i]*p[i]);
best[i] = Min;
}
for (int i = 1; i <= m; i++){
long long que;
scanf("%lld",&que);
if (que > s[n]){
printf("%lld\n",que*p[n]);
continue;
}
int t = 1;
while (que > s[t]) t++;
long long sum = min(que*p[t-1],best[t]);
printf("%lld\n",sum);
}
}
return 0;
}