TZOJ 4021 Ugly Problem(线段树区间子段最大)
描述
给定一个序列A[0],A[1],…A[N-1],要求找到p0,p1,p2,p3使得A[p0]+A[p0+1]+…+A[p1] + A[p2]+A[p2+1]+…+A[p3]最大(0<=p0<=p1<p2<=p3<N)。你只需要求出这个最大值就可以。
输入
输入的第一行为一个数N(2<=N<=10000),接下来的一行为N个整数(-100<A[i]<100)。
输出
输出一个整数表示最大值。
样例输入
5
-1 -2 -4 -5 4
样例输出
3
题意
如上。
题解
枚举分割点,求左区间最大子段和和到右区间最大字段和。
区间最大值段和是一个经典题,线段树维护。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node{
int sum,lmax,rmax,lrs;
}tree[maxn<<];
void pushup(int x){
tree[x].sum=tree[x<<].sum+tree[x<<|].sum;
tree[x].lmax=max(tree[x<<].lmax,tree[x<<|].lmax+tree[x<<].sum);
tree[x].rmax=max(tree[x<<|].rmax,tree[x<<].rmax+tree[x<<|].sum);
tree[x].lrs=max(max(tree[x<<].lrs,tree[x<<|].lrs),tree[x<<].rmax+tree[x<<|].lmax);
}
void build(int l,int r,int p){
if(l==r){
scanf("%d",&tree[p].sum);
tree[p].lmax=tree[p].lrs=tree[p].rmax=tree[p].sum;
return;
}
int mid=(l+r)>>;
build(l,mid,p<<);
build(mid+,r,p<<|);
pushup(p);
}
void update(int k,int v,int l,int r,int p){
if(l==r){
tree[p].lmax=tree[p].lrs=tree[p].rmax=tree[p].sum=v;
return;
}
int mid=(l+r)>>;
if(k<=mid)update(k,v,l,mid,p<<);
else update(k,v,mid+,r,p<<|);
pushup(p);
}
node quert(int L,int R,int l,int r,int p){
if(L<=l&&r<=R)return tree[p];
int mid=(l+r)>>;
node vis,f1,f2;
vis.sum=;
if(L<=mid)vis=f1=quert(L,R,l,mid,p<<);
if(R>mid)vis=f2=quert(L,R,mid+,r,p<<|);
if(L<=mid&&R>mid){
vis.sum=f1.sum+f2.sum;
vis.lmax=max(f1.lmax,f1.sum+f2.lmax);
vis.rmax=max(f2.rmax,f2.sum+f1.rmax);
vis.lrs=max(max(f1.lrs,f2.lrs),f1.rmax+f2.lmax);
}
return vis;
}
int main(){
int n,maxx=-1e9;
scanf("%d",&n);
build(,n,);
for(int i=;i<n;i++)
maxx=max(quert(,i,,n,).lrs+quert(i+,n,,n,).lrs,maxx);
printf("%d\n",maxx);
return ;
}
TZOJ 4021 Ugly Problem(线段树区间子段最大)的更多相关文章
- TZOJ 3315 买火车票(线段树区间最小值)
描述 Byteotian州铁道部决定赶上时代,为此他们引进了城市联网.假设城市联网顺次连接着n 个市从1 到n 编号(起始城市编号为1,终止城市编号为n).每辆火车有m个座位且在任何两个运送更多的乘客 ...
- UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)
"Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...
- hdu 5475 An easy problem(暴力 || 线段树区间单点更新)
http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...
- poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...
- POJ 3468:A Simple Problem with Integers(线段树区间更新模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 141093 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- POJ 3468 A Simple Problem with Integers(线段树&区间更新)题解
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
随机推荐
- 6_1.springboot2.x整合JDBC与数据源配置原理解析
1.引言 对于数据访问层,无论是SQL还是NOSQL,Spring Boot默认采用整合 Spring Data的方式进行统一处理,添加大量自动配置,屏蔽了很多设置.引入各种xxxTemplate,x ...
- Nodejs之路(四)—— MongoDB&MySQL
一.MongoDB 1.1概述 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案.MongoDB 是一个介于关系数据库和非 ...
- angularJs FileUpload插件上传同一文件无效问题记录
参考:https://blog.csdn.net/qq_34829447/article/details/83780392 问题:使用FileUpload插件进行文件上传时,发现无法上传与上个文件相同 ...
- memcache 使用手册
Memcached 教程 Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. Memcached是以LiveJournal旗下Danga Interactive公司的Brad Fit ...
- 【默默努力】react-drag-grid
先放项目地址:https://github.com/Bilif/react-drag-grid 项目运行效果 感谢无私开源的程序员 先看项目入口文件 //index.js import React f ...
- [转]C#操作Excel初探
近期一段时间正好在做winform导出Excel报表的问题,学习了一下C#操作Excel的一些方法(如:向Excel中插入图片:删除Excel指定sheet中的某行或某列,在Excel指定的单元格中画 ...
- 两个datagrid的数据移动(支持多选)
1.需求 :点击卸车和撤销按钮可以实现 1和2 之间数据的移动(支持多选) 2. 代码 (这里只写一个撤销的功能) //撤销按钮 function moveOut() { var item = $(' ...
- maven相互依赖导致无法编译成功
起初是新加了个模块,启动前编译时error,提示找不到依赖模块的类,但java文件上是没有报错的. 后经过排查,发现是循环依赖导致的此问题. 如图,弹出框中有循环依赖的模块会显示红色,右键Open M ...
- React弹窗组件
原文地址 小寒的博客 这里的弹窗泛指所有的弹出组件,这些组件不受页面其他UI布局影响,处于DOM结构的顶层,绝对定位在body元素下. 这个特殊性也给它的开发提出了特殊的要求. react新版本中的c ...
- python基础-基础知识考试_day5 (包括:函数_递归等知识)
老男孩 Python 基础知识练习(三) 1.列举布尔值为 False 的值空,None,0, False, '', [], {}, () 2.写函数:根据范围获取其中 3 和 7 整除的所有数的和, ...