2019 计蒜之道 第六场 D
题目:https://nanti.jisuanke.com/t/39458
每个ai,bi所形成的都是开口相同且向上的抛物线,所以只要求每两条抛物线的交点,并按横坐标维护单调性即可
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxx=1e6+10; LL a[maxx],b[maxx],q[maxx]; double x[maxx]; int main() { int n; cin>>n; for(int i=1;i<=n;i++) scanf("%lld",&a[i]); for(int i=1;i<=n;i++) scanf("%lld",&b[i]); int cnt=1; q[1]=1; x[1]=-999999999; for(int i=2;i<=n;i++) { int flag=1; while(cnt) { int id=q[cnt]; if(a[id]==a[i]) { if(b[id]>=b[i]) { cnt--;continue; } flag=0; break; } //(x-a[i])^2+b[i]=(x-a[id)^2+b[id] x[i]=((double)(b[i]-b[id])/(a[i]-a[id])+a[i]+a[id])/2; if(x[i]<=x[id])cnt--; else break; } if(flag)q[++cnt]=i; } int m; cin>>m; LL t; while(m--) { scanf("%lld",&t); int l=1,r=cnt; while(l<=r) { int mid=(l+r)/2; if(x[q[mid]]<=t)l=mid+1; else r=mid-1; } LL ans=(t-a[q[l-1]])*(t-a[q[l-1]])+b[q[l-1]]; printf("%lld ",ans); } return 0; }