题意

给出N 个形如$f_i(x) = a_i x^2 + b_i x $的二次函数。

有Q 次询问,每次给出一个x,询问$max{\{f_i(x)\}}$。$N,Q \leq 5*10^5$。


思考

首先将x大于0还是小于0分类,对于某一类全都除以x,那么就得到了一些直线。最优的答案一定在某条最上方或最下方的直线上,半平面交或凸包即可。


代码

 // luogu-judger-enable-o2
#pragma GCC optimize 2
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
const int maxn=5E5+;
const int base=;
const ld eps=1E-;
const ld inf=1E17;
int n,m,T;
ll a[maxn],b[maxn],ans[maxn];
int idR[maxn],idL[maxn];
ld rightPlane[maxn],leftPlane[maxn];
int q[maxn];
struct pt
{
double x,y;
pt(double a=,double b=):x(a),y(b){}
pt operator+(const pt&A){return pt(x+A.x,y+A.y);}
pt operator-(const pt&A){return pt(x-A.x,y-A.y);}
pt operator*(ld d){return pt(x*d,y*d);}
double operator*(const pt&A){return x*A.y-y*A.x;}
void out(){cout<<"("<<x<<","<<y<<")"<<endl;}
};
struct line
{
pt A,B;
int id,b;
int slope;
line(){}
line(pt a,pt b):A(a),B(b){}
}s[maxn];
inline ll max(ll x,ll y)
{
return x>y?x:y;
}
inline ll min(ll x,ll y)
{
return x<y?x:y;
}
inline ll get(ll x,ll a,ll b)
{
return (a*x+b)*x;
}
inline int read()
{
bool flag=;
char ch=getchar();
if(ch=='-')
flag^=;
while(!isdigit(ch))
{
ch=getchar();
if(ch=='-')
flag^=;
}
int sum=ch-'';
ch=getchar();
while(isdigit(ch))
{
sum=sum*+ch-'';
ch=getchar();
}
return flag?-sum:sum;
}
void write(ll x)
{
if(x>=)
write(x/);
putchar(''+x%);
}
inline void writen(ll x)
{
if(x<)
{
putchar('-');
write(-x);
}
else
write(x);
putchar('\n');
}
inline int cross(pt A,pt B)
{
ld d=A*B;
if(abs(d)<=eps)
return ;
return d>?:-;
}
inline pt intersection(line a,line b)
{
pt A=b.B-b.A,B=a.B-a.A,C=b.A-a.A;
if(cross(A,B)==)
return pt(inf,inf);
ld d=-(B*C)/(B*A);
return b.A+A*d;
}
inline int onClockwise(line a,line b,line c)
{
return cross(a.B-a.A,intersection(b,c)-a.A)==-;
}
bool cmpR(line A,line B)
{
if(A.slope==B.slope)
return A.b<B.b;
return A.slope<B.slope;
}
bool cmpL(line A,line B)
{
if(A.slope==B.slope)
return A.b>B.b;
return A.slope<B.slope;
}
void initR()
{
sort(s+,s+n+,cmpR);
int L=,R=;
for(int i=;i<=n;++i)
{
while(i!=n&&s[i].slope==s[i+].slope)
++i;
while(R->=L&&onClockwise(s[i],s[q[R]],s[q[R-]]))
--R;
q[++R]=i;
}
for(int i=;i<=R;++i)
idR[i]=s[q[i]].id;
for(int i=;i<R;++i)
rightPlane[i]=intersection(s[q[i]],s[q[i+]]).x;
int pos=;
for(int i=-base;i<=base;++i)
{
while(pos<R&&rightPlane[pos]<ld(i))
++pos;
ans[i+base]=get(i,s[q[pos]].slope,s[q[pos]].b);
}
}
void initL()
{
sort(s+,s+n+,cmpL);
int L=,R=;
for(int i=;i<=n;++i)
{
while(i!=n&&s[i].slope==s[i+].slope)
++i;
while(R->=L&&onClockwise(s[i],s[q[R]],s[q[R-]]))
--R;
q[++R]=i;
}
for(int i=;i<=R;++i)
idL[i]=s[q[i]].id;
for(int i=;i<R;++i)
leftPlane[i]=intersection(s[q[i]],s[q[i+]]).x;
int pos=;
for(int i=base;i>=-base;--i)
{
while(pos<R&&ld(i)<leftPlane[pos])
++pos;
ans[i+base]=max(ans[i+base],get(i,s[q[pos]].slope,s[q[pos]].b));
}
}
int main()
{
// freopen("A.in","r",stdin);
// freopen("A.out","w",stdout);
ios::sync_with_stdio(false);
n=read(),T=read();
for(int i=;i<=n;++i)
{
a[i]=read(),b[i]=read();
s[i].A=pt(,b[i]);
s[i].B=pt(,a[i]+b[i]);
s[i].slope=a[i];
s[i].b=b[i];
s[i].id=i;
}
initR();
for(int i=;i<=n;++i)
swap(s[i].A,s[i].B);
initL();
while(T--)
{
int x=read();
writen(ans[x+base]);
}
return ;
}

最新文章

  1. android Canvas 和 Paint用法
  2. Burp Suite 常用功能 0x01 扫描后台
  3. 实现ApplicationContextAware接口时,获取ApplicationContext为null
  4. 整理常用的iOS第三方资源
  5. JS的学习体会与分享
  6. Scrum1.2--spring计划
  7. MFC中添加消息响应函数
  8. static关键字的用法和main函数
  9. ARM-Linux S5PV210 UART驱动(5)----串口的open操作(tty_open、uart_open)
  10. HDU 1117 免费馅饼 二维动态规划
  11. ip头、tcp头、udp头详解及定义,结合Wireshark抓包看实际情况
  12. C和指针 (pointers on C)——第五章:操作符和表达式
  13. devexpress中用ChartControl生成柱状图
  14. jquery之选项卡效果
  15. win彩 百款皮肤任选任换.可视化
  16. mysql中使用enum,如何获取所有可能的值
  17. django + 阿里云云服务器网站搭建
  18. ubuntu16.04服务器apache的ssl证书配置
  19. python中 try、except、finally 的执行顺序
  20. 【设计模式】 模式PK:策略模式VS状态模式

热门文章

  1. Strongly Connected Tournament
  2. Servlet request 面试题
  3. Struts2 类型转换(易百教程)
  4. 前端vue——阿里图标的使用方法
  5. DEVOPS技术实践_05:sonar静态代码扫描
  6. $Poj1934\ Trip$ 线性$DP+$搜索
  7. Python 愤怒的小鸟代码实现:物理引擎pymunk使用
  8. Go数组和切片你不知道的区别
  9. 不只是安装,Kolla 让 OpenStack 运维变简单
  10. Eclipse直接运行算法第4版例子(重定向和读取指定路径文件)