描述

给定一个序列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(线段树区间子段最大)的更多相关文章

  1. TZOJ 3315 买火车票(线段树区间最小值)

    描述 Byteotian州铁道部决定赶上时代,为此他们引进了城市联网.假设城市联网顺次连接着n 个市从1 到n 编号(起始城市编号为1,终止城市编号为n).每辆火车有m个座位且在任何两个运送更多的乘客 ...

  2. UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)

    "Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...

  3. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  4. poj3468 A Simple Problem with Integers(线段树区间更新)

    https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...

  5. POJ 3468:A Simple Problem with Integers(线段树区间更新模板)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 141093 ...

  6. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  7. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  8. (简单) 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 ...

  9. 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 ...

随机推荐

  1. java_DateTimeFormatter

    日期时间的格式化和解析: public class DateTimeFormatterTest { /** * 时间日期格式化 * @param args */ public static void ...

  2. CF1097E Egor and an RPG game

    最少反链划分数 = 最长链.实现:每次找出所有极大元作为一个反链. 任意长度小于k * (k + 1) / 2的排列都能被划分为不多于k个单调序列.且这是一个紧的上界. 然后这题就可以切了. 题意:给 ...

  3. linux mint & ubuntu 安装QQ国际版

    ubuntu安装QQ相对简单 下载qq国际版deb文件,直接安装即可. 下载地址: http://pan.baidu.com/s/1hqmYzlU 下面就重点说一下linux mint 安装qq. 1 ...

  4. 深入浅出 Java Concurrency (36): 线程池 part 9 并发操作异常体系[转]

    并发包引入的工具类很多方法都会抛出一定的异常,这些异常描述了任务在线程池中执行时发生的例外情况,而通常这些例外需要应用程序进行捕捉和处理. 例如在Future接口中有如下一个API: java.uti ...

  5. 用JS把数组内的日期转换为星期

    核心代码 给定日期是星期几,应把字符串转为Date: new.Date('2017-12-30').getDay(); // 返回0,表示星期天,1:星期一…… 案例: 需求: 调用此方法 funct ...

  6. PAT甲级——A1094 The Largest Generation

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...

  7. VC窗体透明而控件不透明以及Static文本背景透明方法

    出自http://my.oschina.net/ypimgt/blog/60951 优点:    1.Dialog 窗体完全透明.     2. 窗体上的控件不透明. DC 绘制的图形不透明.     ...

  8. NoSQL 文档数据库

  9. [POI2017]Sabota【观察+树形Dp】

    Online Judge:Bzoj4726 Label:观察,树形Dp,水题 题目描述 某个公司有n个人, 上下级关系构成了一个有根树.公司中出了个叛徒(这个人不知道是谁). 对于一个人, 如果他下属 ...

  10. 如何使用Junit进行单元测试

    测试方法的要求: 必须是public 无返回值 无参数 @Testpublic void f1(){ .....} 在@Test上按下 Ctrl+1(快速锁定错误) 引入Junit包 在方法名上右键 ...