[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. 在Unity中实现屏幕空间反射Screen Space Reflection(2)

    traceRay函数 在上一篇中,我们有如下签名的traceRay函数 bool traceRay(float3 start, float3 direction, out float2 hitPixe ...

  2. HDU 1072 Nightmare (广搜)

    题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...

  3. NYOJ 1063 生活的烦恼 (二叉树)

    题目链接 描述 生活的暑假刚集训开始,他要决心学好字典树,二叉树,线段树和各种树,但生活在OJ上刷题的时候就遇到了一个特别烦恼的问题.那当然就是他最喜欢的二二叉树咯!题目是这样的:给你一颗非空的二叉树 ...

  4. python3爬虫.4.下载煎蛋网妹子图

    开始我学习爬虫的目标 ----> 煎蛋网 通过设置User-Agent获取网页,发现本该是图片链接的地方被一个js函数代替了 于是全局搜索到该函数 function jandan_load_im ...

  5. 零基础讲解JavaScript函数

    一 JavaScript函数1 什么是函数  函数是一组代码(指令)的集合,通常用来完成某个单一的功能.(书的目录和章节,电视剧剧集的名称等)2 为什么要使用函数  2.1 把复杂程序划分成不同的功能 ...

  6. bootstrap分页查询传递中文参数到后台(get方式提交)

    <!--分页 --> <div style="width: 380px; margin: 0 auto; margin-top: 50px;"> <u ...

  7. django【ORM】model字段类型

    1.AutoField 一个自增的IntegerField,一般不直接使用,Django会自动给每张表添加一个自增的primary key. 2.BigIntegerField 64位整数, -922 ...

  8. 64_n2

    nodejs-from-0.1.3-4.fc26.noarch.rpm 11-Feb-2017 15:01 9982 nodejs-from2-2.1.0-6.fc26.noarch.rpm 11-F ...

  9. 判断Selenium加载完成

    How do you make Selenium 2.0 wait for the page to load? You can also check pageloaded using followin ...

  10. Swift中的指针类型

    Swift编程语言为了能与Objective-C与C语言兼容,而引入了指针类型.尽管官方不建议频繁使用指针类型,但很多时候,使用指针能完成更多.更灵活的任务.比如,我们要实现一个交换两个整数值的函数的 ...