计蒜客 38228. Max answer-线段树维护单调栈(The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer 南昌邀请赛网络赛) 2019ICPC南昌邀请赛网络赛
Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values in the interval, multiplied by the smallest value in the interval.
Now she is planning to find the max value of the intervals in her array. Can you help her?
Input
First line contains an integer n(1 \le n \le 5 \times 10 ^5n(1≤n≤5×105).
Second line contains nn integers represent the array a (-10^5 \le a_i \le 10^5)a(−105≤ai≤105).
Output
One line contains an integer represent the answer of the array.
样例输入复制
5
1 2 3 4 5
样例输出复制
36
用单调栈判断以每个值为最小值的最大左边界和右边界。后对对每个值分成负数和正数讨 论取可行区间内的最小或最大值,方法为求前缀和和后缀和,然后用线段树求区间最值。
线段树维护的时候,左区间维护后缀和,右区间维护前缀和,找的时候,在i值左边找后缀,在i值右边找前缀,然后交叉的部分就是满足的区间。
代码:
//I-线段树+单调栈
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+;
const int inf=0x3f3f3f3f; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 int a[maxn],l[maxn],r[maxn];
ll pre[maxn],beh[maxn],maxl[maxn<<],minl[maxn<<],maxr[maxn<<],minr[maxn<<]; void pushup(int rt)
{
maxl[rt]=max(maxl[rt<<],maxl[rt<<|]);
minl[rt]=min(minl[rt<<],minl[rt<<|]);
maxr[rt]=max(maxr[rt<<],maxr[rt<<|]);
minr[rt]=min(minr[rt<<],minr[rt<<|]);
} void build(int l,int r,int rt)
{
if(l==r){
maxl[rt]=minl[rt]=beh[l];
maxr[rt]=minr[rt]=pre[l];
return ;
} int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
} ll query(int op,int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
if (op==) return maxl[rt];
else if(op==) return minl[rt];
else if(op==) return maxr[rt];
else if(op==) return minr[rt];
} int m=(l+r)>>;
ll ret;
if(op==||op==){
ret=-inf;
if(L<=m) ret=max(ret,query(op,L,R,lson));
if(R> m) ret=max(ret,query(op,L,R,rson));
}
else if(op==||op==){
ret=inf;
if(L<=m) ret=min(ret,query(op,L,R,lson));
if(R> m) ret=min(ret,query(op,L,R,rson));
}
return ret;
} deque<int> deq;//因为是双端队列,所以插的时候要插到头上才能实现栈的功能 int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++){
pre[i]=pre[i-]+a[i];
}
for(int i=n;i>=;i--){
beh[i]=beh[i+]+a[i];
}
build(,n,);
for(int i=;i<=n;i++){
while(deq.size()&&a[deq.front()]>=a[i]) deq.pop_front();
if(deq.empty()) l[i]=;
else l[i]=deq.front()+;
deq.push_front(i);
}
deq.clear();
for(int i=n;i>=;i--){
while(deq.size()&&a[deq.front()]>=a[i]) deq.pop_front();
if(deq.empty()) r[i]=n;
else r[i]=deq.front()-;
deq.push_front(i);
}
ll maxx=-inf,ret;
for(int i=;i<=n;i++){
if(a[i]>=){
ret=query(,l[i],i,,n,);
ret+=query(,i,r[i],,n,);
ret=ret-beh[i]-pre[i]+a[i];
ret*=a[i];
// cout<<query(1,l[i],i,1,n,1)<<" "<<query(3,i,r[i],1,n,1)<<endl;
}
else{
ret=query(,l[i],i,,n,);
ret+=query(,i,r[i],,n,);
ret=ret-beh[i]-pre[i]+a[i];
ret*=a[i];
}
maxx=max(maxx,ret);
}
printf("%lld\n",maxx);
}
计蒜客 38228. Max answer-线段树维护单调栈(The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer 南昌邀请赛网络赛) 2019ICPC南昌邀请赛网络赛的更多相关文章
- The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)
题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈
题面 题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值 题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok 我们 ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- Max answer(The Preliminary Contest for ICPC China Nanchang National Invitational)
Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values ...
- [CSP-S模拟测试]:陶陶摘苹果(线段树维护单调栈)
题目传送门(内部题116) 输入格式 第一行两个整数$n,m$,如题 第二行有$n$个整数表示$h_1-h_n(1\leqslant h_i\leqslant 10^9)$ 接下来有$m$行,每行两个 ...
- 洛谷 P4198 楼房重建 线段树维护单调栈
P4198 楼房重建 题目链接 https://www.luogu.org/problemnew/show/P4198 题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上 ...
- 【原创】tyvj1038 忠诚 & 计蒜客 管家的忠诚 & 线段树(单点更新,区间查询)
最简单的线段树之一,中文题目,不翻译.... 注释讲的比较少,这已经是最简单的线段树,如果看不懂真的说明最基础的理论没明白 推荐一篇文章http://www.cnblogs.com/liwenchi/ ...
- [计蒜客T2238]礼物_线段树_归并排序_概率期望
礼物 题目大意: 数据范围: 题解: 这题有意思啊($md$卡常 直接做怎么做? 随便上个什么东西,维护一下矩阵乘和插入,比如说常数还算小的$KD-Tree$(反正我是没见人过过 我们漏掉了一个条件, ...
- [BZOJ 2957]楼房重建(THU2013集训)(线段树维护单调栈)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2957 分析: 根据题意,就是比较斜率大小 只看一段区间的话,那么这段区间能看见的楼房数量就是这 ...
随机推荐
- ubuntu系统下防火墙简单使用
apt-get install ufw 安装防火墙sudo ufw enable|disable|status 开启/关闭/查看防火墙状态sudo ufw allow 22/ ...
- WPF 程序鼠标在窗口之外的时候,控件拿到的鼠标位置在哪里?
原文:WPF 程序鼠标在窗口之外的时候,控件拿到的鼠标位置在哪里? 在 WPF 程序中,我们有 Mouse.GetPosition(IInputElement relativeTo) 方法可以拿到鼠标 ...
- sql servse 常用维护sql
1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份 ...
- 使用KONG网关实现接口迁移的灰度验证
在我们对一个API站点进行微服务化的过程中,使用KONG网关可以实现以下几个效果: 1. 业务线无感知,其实内部已经被Kong转到其他站点上执行了,这对业务线特别友好. 2. 可以实现租户级/接口级灰 ...
- Visual Studio 2012 VC下 OpenGL 配置与使用
Windows环境下的GLUT下载地址:(大小约为150k) Download 1 32位Windows环境下安装GLUT的步骤1.将glut.h复制到C:\Program Files (x86 ...
- Python实现柱状图【数字精准展示,使用不同颜色】
一.简介 主要使用matplotlib基于python的可视化组件实现. 二.代码实现 # -*- coding: utf-8 -*- """ Created on Mo ...
- 【python】文件下载---基础版
基于TCP协议的基础版本,不支持大文件 Client.py import socket def main(): # 1. 创建套接字 tcp_socket = socket.socket(socket ...
- 为安卓项目添加FileProvider
简单记录 android7.0开始出现FileProvider.从一个小白角度看需要注意这几点: Manifest.xml中添加provider节点 添加xml文件 模块的build.gradle中添 ...
- GNS3、Wireshark、SecureCRT 环境部署
本次GNS3环境部署教程基于官方推荐的稳定版1.5.4.初次接触此软件,详细的使用方法不是很清楚,所以以此作为学习记录,仅供参考,后期补充. 软件介绍 GNS3 GNS3是一款具有图形化界面可以运行在 ...
- 【异常(待解决)】org.apache.http.NoHttpResponseException: api.weixin.qq.com:443 failed to respond
一.记录下异常堆栈信息 -- ::-exec-] ERROR weixin.popular.client.LocalHttpClient - execute error org.apache.http ...