描述

给定一个序列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. P1280 尼克的任务 /// DP(选择性地)

    题目大意: https://www.luogu.org/problemnew/show/P1280 题解 手推一遍思路更清晰 #include <bits/stdc++.h> using ...

  2. iOS开发系列-iOS签名机制

    概述 想要了解iOS的签名机制需要有一定密码学有一定的了解.下面依次介绍的数据的加密解密.单向散列函数.数字签名.证书.iOS签名机制. 数据加密解密 在网络通信中想要防止数据被攻击者拦截,我们通常对 ...

  3. 判断语句(if...else)if...else语句是在指定的条件成立时执行代码,在条件不成立时执行else后的代码

    判断语句(if...else) if...else语句是在指定的条件成立时执行代码,在条件不成立时执行else后的代码. 语法: if(条件) { 条件成立时执行的代码 } else { 条件不成立时 ...

  4. HashMap数据结构

    2.1 HashMap 2.1.1 HashMap介绍 先看看HashMap类头部的源码: public class HashMap<K,V> extends AbstractMap< ...

  5. 平衡树模板【splay的实现】

    [平衡树splay实现] 无注释代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; ,MAXN=1e ...

  6. sql不用拼接语句实现动态查询条件

    DECLARE @oFrom INT SELECT * FROM baseinfo AND ( ( and Type = 'Breakfast') ) or的条件可自由添加,尤其适用互斥条件的查询.

  7. 小米手机的miui10 连接电脑。本地播放器推荐。

    问题: 电脑连接了手机却不能看到手机里面的文件. 方法一 方法二 连接和电脑一样的wifi 进入文件管理 来自:百度经验. 本地播放器推荐 爱奇艺万能播放器(还不错,目前在用).qq影音 爱奇艺万能播 ...

  8. C++系列作业

    1.编写一个完整的程序,实现功能:向用户提问“现在正在下雨吗?”,提示用户输入Y或N.若输入为Y,显示“现在正在下雨.”:若输入为N,显示“现在没有下雨”:否则继续提问“现在正在下雨吗?” #incl ...

  9. day 42 03--CSS布局设置

      03--CSS布局设置   本节目录 一 盒模型 二 padding(内边距) 三 boder(边框) 四 简单认识一下margin(外边距) 五 标准文档流 六 块级元素和行内元素 七 浮动 八 ...

  10. PAT甲级——A1094 The Largest Generation

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