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 ...
随机推荐
- git工作区和暂存区图
- 7_3.springboot2.x启动配置原理_3.事件监听机制
事件监听机制配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListenerioc容器中的 ...
- 如何使用Spark大规模并行构建索引
使用Spark构建索引非常简单,因为spark提供了更高级的抽象rdd分布式弹性数据集,相比以前的使用Hadoop的MapReduce来构建大规模索引,Spark具有更灵活的api操作,性能更高,语法 ...
- JS规则 我与你同在(逻辑与操作符)数学中的“b大于a,b小于c”是“a<b<c”,那么在JavaScript中可以用&&表示
我与你同在(逻辑与操作符) 数学里面的"a>b",在JavaScript中还表示为a>b:数学中的"b大于a,b小于c"是"a<b& ...
- spring:AOP面向切面编程02
参考: https://blog.csdn.net/jeffleo/article/details/54136904 一.AOP的核心概念AOP(Aspect Oriented Programming ...
- openSUSE 安装compass,mkmf.rb can't find,checking for ffi.h...extconf.rb failed
安装compass时,提示 Fetching: sass-.gem (%) Successfully installed sass- Fetching: ffi-.gem (%) Building n ...
- Miler-Rabbin素数判定
前言 素数判定? 小学生都可以打的出来! 直接暴力O(n)O(\sqrt n)O(n)-- 然后就会发现,慢死了-- 于是我们想到了筛法,比如前几天说到的詹欧筛法. 但是线性的时间和空间成了硬伤-- ...
- ThinkPHP实现了ActiveRecords模式的ORM模型
ThinkPHP实现了ActiveRecords模式的ORM模型,采用了非标准的ORM模型:表映射到类,记录映射到对象.最大的特点就是使用方便和便于理解(因为采用了对象化),提供了开发的最佳体验,从而 ...
- 84 落单的数 III
原题网址:http://www.lintcode.com/zh-cn/problem/single-number-iii/# 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到 ...
- Java-MyBatis-MyBatis3-XML映射文件:insert, update 和 delete
ylbtech-Java-MyBatis-MyBatis3-XML映射文件:insert, update 和 delete 1.返回顶部 1. insert, update 和 delete 数据变更 ...