[ARC051D]長方形

题目大意:

给定\(A_{1\sim n}\)和\(B_{1\sim m}(n,m\le2000,|A_i|,|B_i|\le10^5)\),矩阵\(C_{i,j}=A_i+B_j\)。\(q(q\le2000)\)次询问,求坐标不超过\((x,y)\)的最大权值子矩阵的权值。

思路:

用\(maxa[i][j]\)表示\(A_{1\sim i}\)中长度为\(j\)的最大连续子段和,\(maxb\)同理。

对于询问\((x,y)\),答案即为\(\max\{maxa[x][i]\times j+maxb[y][j]\times i\mid i\le x,j\le y\}\)。

\(\max\)内变形后即为\(j\times(maxa[x][i]+\frac{maxb[y][j]\times i}j)\)。括号内的东西可以看作是关于\(\frac{maxb[y][j]}j\)的一次函数,使用李超树维护凸壳即可。

时间复杂度\(\mathcal O(qn\log n)\)。

源代码:

#include<cmath>
#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) neg|=ch=='-';
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return neg?-x:x;
}
typedef long long int64;
const int N=2001,LIM=1e5,SIZE=(LIM<<1|1)<<2;
int a[N],b[N],maxa[N][N],maxb[N][N];
class SegmentTree {
#define _left <<1
#define _right <<1|1
#define mid ((b+e)>>1)
private:
struct Node {
int a,b,tim;
};
Node node[SIZE];
public:
void insert(const int &p,const int &b,const int &e,int i,int j,const int &t) {
if(node[p].tim<t) {
node[p].a=i;
node[p].b=j;
node[p].tim=t;
return;
}
int64 lval1=1ll*node[p].a*b+node[p].b;
int64 rval1=1ll*node[p].a*e+node[p].b;
int64 lval2=1ll*i*b+j,rval2=1ll*i*e+j;
if(lval1<lval2) {
std::swap(lval1,lval2);
std::swap(rval1,rval2);
std::swap(node[p].a,i);
std::swap(node[p].b,j);
}
if(rval1>=rval2||b==e) return;
const double c=1.*(j-node[p].b)/(node[p].a-i);
if(c<=mid) {
insert(p _left,b,mid,node[p].a,node[p].b,t);
node[p].a=i;
node[p].b=j;
} else {
insert(p _right,mid+1,e,i,j,t);
}
}
double query(const int &p,const int &b,const int &e,const double &x,const int &t) const {
if(node[p].tim<t) return -1e15;
double ret=node[p].a*x+node[p].b;
if(b==e) return ret;
if(x<=mid) ret=std::max(ret,query(p _left,b,mid,x,t));
if(x>mid) ret=std::max(ret,query(p _right,mid+1,e,x,t));
return ret;
}
#undef _left
#undef _right
#undef mid
};
SegmentTree t;
int main() {
const int n=getint(),m=getint();
for(register int i=1;i<=n;i++) {
maxa[i][i]=a[i]=a[i-1]+getint();
for(register int j=1;j<i;j++) {
maxa[i][j]=std::max(maxa[i-1][j],a[i]-a[i-j]);
}
}
for(register int i=1;i<=m;i++) {
maxb[i][i]=b[i]=b[i-1]+getint();
for(register int j=1;j<i;j++) {
maxb[i][j]=std::max(maxb[i-1][j],b[i]-b[i-j]);
}
}
const int q=getint();
for(register int p=1;p<=q;p++) {
const int x=getint(),y=getint();
for(register int i=1;i<=x;i++) {
t.insert(1,-LIM,LIM,i,maxa[x][i],p);
}
int64 ans=LLONG_MIN;
for(register int i=1;i<=y;i++) {
ans=std::max(ans,llround(t.query(1,-LIM,LIM,1.*maxb[y][i]/i,p)*i));
}
printf("%lld\n",ans);
}
return 0;
}

[ARC051D]長方形的更多相关文章

  1. PCB成型製程介紹

    PCB成型製程在電子構裝中所扮演的角色 下圖是電腦主機的內部組成 我們將以插在主機板上的一片 USB擴充卡來說明PCB成型製 程在電子構裝中所扮演的角色 PCB成型製程的子製程 USB擴充卡要插入主機 ...

  2. [R] 繪圖 Par 函数

    本篇內文主引用 https://zhuanlan.zhihu.com/p/21394945 之內容再稍加整理並參照下方有用資源 [rdocumentation] https://www.rdocume ...

  3. [題解/狀壓dp]POJ_2411_Mondriaan's dream

    关于“我读过很多书,到后来大部分都被我忘记了,那阅读的意义是什么?”的疑问,我看过最巧妙的一个回答:当我还是个孩子的时候,我吃过很多的食物,大部分已经一去不复返而且被我忘记了,但可以肯定的是,它们中的 ...

  4. Halcon算子函数

    Chapter_1_:Classification 1.1  Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一個訓練樣本添加到一個高斯混合模型的 ...

  5. 妙方之解决matplotlib的图例里的中文呈现小方形

    妙方之解决matplotlib的图例里的中文呈现小方形 分析思路: 每个中文都对应地呈现一个小方形, 不多也不少. 不能说是乱码. 应该是matplotlib的默认字库不支持中文造成的. 应对办法: ...

  6. PHP 計算字符串長度函數

    PHP內置的字符串長度函數strlen無法正確處理中文字符串,它得到的只是字符串所占的字節數.對於GB2312的中文編碼,strlen得到的值是漢字個數的2倍,而對於UTF-8編碼的中文,就是3倍的差 ...

  7. C#使用多态求方形面积周长和圆的面积周长

    class class1 { public static void Main(string[] args) { //使用多态求矩形面积与周长和圆的面积与周长 Shape cl = ); double ...

  8. Html5 Css实现方形图片 圆形显示

    <!doctype html><html><head><meta charset="utf-8"><title>方形图片 ...

  9. canvas应用——将方形图片处理为圆形

    上段时间在项目中需要将方形图片处理为圆形图片,你可能会说直接用css设置border-radius: 50%就可以了,但是项目中还要将此图片的圆形图片作为一部分利用canvas将其绘制到一张背景图上面 ...

随机推荐

  1. MongoDB 3.4.2 配置 CentOS 6.5 远程连接

    1.新建用户 db.createUser({user: 'test', pwd: 'myPassword', roles: [{role: 'readWrite', db: 'test_db'}]}) ...

  2. ASLR

    @author:dlive ASLR address space layout randomization 微软从windows vista/windows server 2008(kernel ve ...

  3. python设计模式之内置装饰器使用(四)

    前言 python内部有许多内建装饰器,它们都有特别的功能,下面对其归纳一下. 系列文章 python设计模式之单例模式(一) python设计模式之常用创建模式总结(二) python设计模式之装饰 ...

  4. ubuntu无法获得锁 /var/lib/dpkg -open 问题

    问题: 方法: sudo rm   /var/lib/dpkg/lock 然后再安装就可以了

  5. 无状态Http

    无状态的根本原因 浏览器和服务器使用socket通信,服务器将请求结果返回给浏览器后,会关闭当前socket连接.而且服务器会在处理页面完毕后销毁页面对象. 应用层面的原因 浏览器和服务器之间通信都遵 ...

  6. 正排索引(forward index)与倒排索引(inverted index) (转)

    一.正排索引(前向索引) 正排索引也称为"前向索引".它是创建倒排索引的基础,具有以下字段. (1)LocalId字段(表中简称"Lid"):表示一个文档的局部 ...

  7. cvc-complex-type.2.4.a: Invalid content was found starting with element ‘init-param’(转)

    在写xml的时候又一次总是报cvc-complex-type.2.4.a: Invalid content was found starting with element 错误,还出现小红叉,在网上找 ...

  8. 【PAT】1002. A+B for Polynomials (25)

    1002. A+B for Polynomials (25) This time, you are supposed to find A+B where A and B are two polynom ...

  9. day1作业一:编写登陆接口

    作业一:编写登陆接口 1.输入用户名和密码 2.认证成功后显示欢迎信息 3.输错三次后锁定 Readme: (1)提示用户输入用户名: (2)用户名验证,验证是否已经锁定: (3)是否锁定:已锁定告诉 ...

  10. 小甲鱼C++笔记(下)25-48

    二十五  二十六  二十七  重载 运算符重载 1. 作为成员函数 #include <iostream> using namespace std; class Add { private ...