描述

给定一个序列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 高级面试知识点汇总!

    1.常用设计模式 单例模式:懒汉式.饿汉式.双重校验锁.静态加载,内部类加载.枚举类加载.保证一个类仅有一个实例,并提供一个访问它的全局访问点. 代理模式:动态代理和静态代理,什么时候使用动态代理. ...

  2. Redis Cluste部署

    一.原生搭建篇Cluster了解cluster的架构 Redis-cluster是使用的是一致性哈希算法来切分数据存储,总计16383个槽,分成16383/N(redis节点)个分区,存取时将key转 ...

  3. ps photoshop

    PS-前端切图教程(切jpg图和切png图) 参考线显示和隐藏:ctrol+h alt+v+e或者打开标尺然后从点击标尺就能拖拽出来,删除也是拖到标尺附近就删除 显示.隐藏标尺:ctrol+R 显示网 ...

  4. SPOJ694 New Distinct Substrings

    New Distinct Substrings 题目大意 给定一个字符串,求本质不同的子串个数 题解 SA常见思想:每一个子串都是某个后缀的前缀 考虑每一个后缀的贡献,首先他拥有n - sa[i]个( ...

  5. 1.spark核心RDD特点

    RDD(Resilient Distributed Dataset) Spark源码:https://github.com/apache/spark   abstract class RDD[T: C ...

  6. Ansible-playbook简单应用的几个实例

    ①ansible-playbook的循环: 重复执行某任务:对迭代项的引用,固定变量名为“item”,而后要在task中使用with_items给定要迭代的元素列表,列表方法:字符串/字典(类似jso ...

  7. Python遇到的第一个问题

    1.运行如下代码: 输入成绩80之后报错: 2.问题分析:字符串跟整型不能比 3.查看score的类型 print(type(score)), 由此看出score是string类型的,因为input接 ...

  8. 2016.10.4初中部下午NOIP普及组比赛总结

    2016.10.4初中部下午NOIP普及组比赛总结 这次的题有些水,只是第四题有点坑. 题目: 比赛:AC+0+AC+50=250 改题:AC+AC+AC+50=350 一.Bill 满地都是水 题目 ...

  9. 廖雪峰Java11多线程编程-3高级concurrent包-8CompletableFuture

    使用Future可以获得异步执行结果 Future<String> future = executor.submit(task); String result = future.get() ...

  10. String相加解析

    Java代码 package com.utils.test; public class TestObject { public static void main(String[] args) { St ...