题目:Potted Flower

Description

The little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrounded by N pots of flowers. Each potted flower will be assigned to an integer number (possibly negative) denoting how attractive it is. See the following graph as an example:

(Positions of potted flowers are assigned to index numbers in the range of 1 ... N. The i-th pot and the (i + 1)-th pot are consecutive for any given i (1 <= i < N), and 1st pot is next to N-th pot in addition.)

The board chairman informed the little cat to construct "ONE arc-style cane-chair" for tourists having a rest, and the sum of attractive values of the flowers beside the cane-chair should be as large as possible. You should notice that a cane-chair cannot be a total circle, so the number of flowers beside the cane-chair may be 1, 2, ..., N - 1, but cannot be N. In the above example, if we construct a cane-chair in the position of that red-dashed-arc, we will have the sum of 3+(-2)+1+2=4, which is the largest among all possible constructions.

Unluckily, some booted cats always make trouble for the little cat, by changing some potted flowers to others. The intelligence agency of little cat has caught up all the M instruments of booted cats' action. Each instrument is in the form of "A B", which means changing the A-th potted flowered with a new one whose attractive value equals to B. You have to report the new "maximal sum" after each instruction.

Input

There will be a single test data in the input. You are given an integer N (4 <= N <= 100000) in the first input line.

The second line contains N integers, which are the initial attractive value of each potted flower. The i-th number is for the potted flower on the i-th position.

A single integer M (4 <= M <= 100000) in the third input line, and the following M lines each contains an instruction "A B" in the form described above.

Restriction: All the attractive values are within [-1000, 1000]. We guarantee the maximal sum will be always a positive integer.

Output

For each instruction, output a single line with the maximum sum of attractive values for the optimum cane-chair.

Sample Input

5
3 -2 1 2 -5
4
2 -2
5 -5
2 -4
5 -1

Sample Output

4
4
3
5

题目大意:给定一个环形数列,每次更改一个数,求其最大子段和(长度不能为 n )

数据的\(n,m\)极大,\(O(n^2)\)的方法就否决了。第一次知道线段树可求最大子段和,线段树tql。

环形最大子段和在不破坏原有队列情况下,只有两种可能,1.中间的一段 2.左端前缀和+右端前缀和

先不管长度,于是我们可以维护左最大前缀,右最大前缀,区间最大值,区间和,则pushup函数就出来了:

tree[id].lmx=max(tree[ls].lmx,tree[ls].s+tree[rs].lmx);
tree[id].rmx=max(tree[rs].rmx,tree[rs].s+tree[ls].rmx);
tree[id].s=tree[ls].s+tree[rs].s;
tree[id].mx=max(tree[ls].mx,max(tree[rs].mx,tree[ls].rmx+tree[rs].lmx));

然后对于更新后输出结果讨论,只需要比较上述的两种可能就可以了。

还是得管长度,当长度为n时,我们需要减去最短的子段和,所以线段树也需要维护所有最小值:

tree[id].lmn=min(tree[ls].lmn,tree[ls].s+tree[rs].lmn);
tree[id].rmn=min(tree[rs].rmn,tree[rs].s+tree[ls].rmn);
tree[id].mn=min(tree[ls].mn,min(tree[rs].mn,tree[ls].rmn+tree[rs].lmn));

所以答案是中间的一段max即tree[1].mx,左端前缀和+右端前缀和即tree[1].s-tree[1].mn比大小

因为tree[1].mx有可能包含整个数列,所以当tree[1].mx=tree[1].s时只取左右端前缀和。(证明如下)

如果tree[1].sum<=0,则tree[1].maxsum分两种情况讨论:
1.tree[1].maxsum包含区间1~n,则与tree[1].sum等价,输出tree[1].sum-tree[1].minsum正确
2.tree[1].maxsum只包含区间1~n部分,因为tree[1].maxsum是确定的,且tree[1].maxsum=tree[1].sum,tree[1].minsum<0(因为tree[1].sum<0),则
tree[1].sum-tree[1].minsum仍大于等于tree[1].maxsum,结果仍正确
如果tree[1].sum>0,易证。

Code

#include<cstdio>
#include<iostream>
using namespace std;
const int N=1e5+5;
struct qh{
int s,mx,mn,lmx,lmn,rmx,rmn;
}tree[N<<2];
inline int Rd(){
int s=0,w=1;char ch=getchar();
while (ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
while (ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+ch-'0',ch=getchar();
return s*w;
}
#define ls id<<1
#define rs ls|1
#define M (l+r>>1)
void pushup(int id){
tree[id].s=tree[ls].s+tree[rs].s;
tree[id].lmx=max(tree[ls].lmx,tree[ls].s+tree[rs].lmx);
tree[id].lmn=min(tree[ls].lmn,tree[ls].s+tree[rs].lmn);
tree[id].rmx=max(tree[rs].rmx,tree[rs].s+tree[ls].rmx);
tree[id].rmn=min(tree[rs].rmn,tree[rs].s+tree[ls].rmn);
tree[id].mx=max(tree[ls].mx,max(tree[rs].mx,tree[ls].rmx+tree[rs].lmx));
tree[id].mn=min(tree[ls].mn,min(tree[rs].mn,tree[ls].rmn+tree[rs].lmn));
return ;
}
void update(int id,int l,int r,int x,int v){
if(l==r){
tree[id].s=tree[id].mx=tree[id].mn=tree[id].lmx=tree[id].lmn=tree[id].rmx=tree[id].rmn=v;
return ;
}
if(x<=M) update(ls,l,M,x,v);
else update(rs,M+1,r,x,v);
pushup(id);
}
#undef ls
#undef rs
#undef M
int main(){
int n=Rd();
for(int i=1,x;i<=n;i++) x=Rd(),update(1,1,n,i,x);
int m=Rd();
while (m--){
int a=Rd(),b=Rd();
update(1,1,n,a,b);
if(tree[1].mx==tree[1].s) printf("%d\n",tree[1].s-tree[1].mn);
else printf("%d\n",max(tree[1].mx,tree[1].s-tree[1].mn));
}
return 0;
}

Start :2022.09.07:16:20

Finish:2022.09.07:17:22

dp线段树优化的更多相关文章

  1. [USACO2005][POJ3171]Cleaning Shifts(DP+线段树优化)

    题目:http://poj.org/problem?id=3171 题意:给你n个区间[a,b],每个区间都有一个费用c,要你用最小的费用覆盖区间[M,E] 分析:经典的区间覆盖问题,百度可以搜到这个 ...

  2. HDU4719-Oh My Holy FFF(DP线段树优化)

    Oh My Holy FFF Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) T ...

  3. UVA-1322 Minimizing Maximizer (DP+线段树优化)

    题目大意:给一个长度为n的区间,m条线段序列,找出这个序列的一个最短子序列,使得区间完全被覆盖. 题目分析:这道题不难想,定义状态dp(i)表示用前 i 条线段覆盖区间1~第 i 线段的右端点需要的最 ...

  4. zoj 3349 dp + 线段树优化

    题目:给出一个序列,找出一个最长的子序列,相邻的两个数的差在d以内. /* 线段树优化dp dp[i]表示前i个数的最长为多少,则dp[i]=max(dp[j]+1) abs(a[i]-a[j])&l ...

  5. 完美字符子串 单调队列预处理+DP线段树优化

    题意:有一个长度为n的字符串,每一位只会是p或j.你需要取出一个子串S(注意不是子序列),使得该子串不管是从左往右还是从右往左取,都保证每时每刻已取出的p的个数不小于j的个数.如果你的子串是最长的,那 ...

  6. Contest20140906 ProblemA dp+线段树优化

    Problem A 内存限制 256MB 时间限制 5S 程序文件名 A.pas/A.c/A.cpp 输入文件 A.in 输出文件 A.out 你有一片荒地,为了方便讨论,我们将这片荒地看成一条直线, ...

  7. POJ 3171.Cleaning Shifts-区间覆盖最小花费-dp+线段树优化(单点更新、区间查询最值)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4721   Accepted: 1593 D ...

  8. 题解 HDU 3698 Let the light guide us Dp + 线段树优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...

  9. 省选模拟赛 4.26 T1 dp 线段树优化dp

    LINK:T1 算是一道中档题 考试的时候脑残了 不仅没写优化 连暴力都打挂了. 容易发现一个性质 那就是同一格子不会被两种以上的颜色染.(颜色就三种. 通过这个性质就可以进行dp了.先按照左端点排序 ...

  10. 【uva1502/hdu4117-GRE Words】DP+线段树优化+AC自动机

    这题我的代码在hdu上AC,在uva上WA. 题意:按顺序输入n个串以及它的权值di,要求在其中选取一些串,前一个必须是后一个的子串.问d值的和最大是多少. (1≤n≤2×10^4 ,串的总长度< ...

随机推荐

  1. 【SpringBoot】02 概述

    [目标] - 什么是SpringBoot? 并不是新技术,只是一个Spring的加强 解脱XML配置,增加了新的注解,但是并不是新的内容 - 新型配置文件技术 YAML - 自动装配原理[了解即可,不 ...

  2. NVIDIA中的cupti的作用及设置: CUDA profiling tools interface —— Could not load dynamic library 'libcupti.so.10.1' —— failed with error CUPTI_ERROR_INSUFFICIENT_PRIVILEGES

    NVIDIA官方给出的说明: 可以知道,这个组件的作用是对NVIDIA的CUDA进程进行性能分析的,通过对这个组件的调用可以实现对CUDA进程的性能监测. 在使用深度学习框架时有时需要对运行的代码的C ...

  3. python网络连接报错:ValueError("Unable to determine SOCKS version from %s" % proxy_url) ValueError: Unable to determine SOCKS version from socks://192.168.1.100:1080/

    python应用proxy网络连接报错: return super().send(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...

  4. 如何修复ubuntu的uefi启动——如何将Ubuntu安装入移动硬盘中

    交代一下使用场景,个人平时经常使用Ubuntu系统,由于不喜欢总在一个地方呆但是来回搬电脑又不是十分的方便,于是想到了一个好的方案,那就是把Ubuntu系统安装到移动硬盘中,这样不论是在家还是在实验室 ...

  5. canvas实现手动绘制矩形

    开场白 虽然在实际的开发中我们很少去绘制流程图 就算需要,我们也会通过第3方插件去实现 下面我们来简单实现流程图中很小的一部分 手动绘制矩形 绘制一个矩形的思路 我们这里绘制矩形 会使用到canvas ...

  6. portainer安装&升级

    2024年4月15日 关于升级: 如果需要升级 Portainer,请按以下步骤操作: 使用以下命令列出所有镜像: docker ps -a 根据需要删除指定镜像: docker rm <镜像名 ...

  7. 我的 PowerShell 配置

    安装 Scoop: Scoop 是 Windows 上的包管理器 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUse ...

  8. 怎样在局域网中给网站作ssl认证,使其能以https协议访问(转)

    如果要在局域网达到效果需要满足以下几点要求: 1. 证书由可信任的CA机构颁发 2. 证书在有效期 3. 访问地址和证书的认证地址一致 说明: 1. 需要在局域网内构建CA机构 2. 证书的有效期建议 ...

  9. Java多线程并发之同步容器和并发容器-第一篇

    Java多线程并发之同步容器和并发容器-第一篇 概述 本文主要讲解在Java多线程并发开发中,集合中有哪些支持并发的的.什么是同步容器(集合),什么是并发容器(集合)?并发容器分类有哪些?每个分类都有 ...

  10. 在一个简单的pwn题目中探究执行系统调用前堆栈的对齐问题

    题目介绍:在输入AAAAAAAAAAAAAAAAAAAAAAAAA后,程序会打开一个shell,这是为什么?字符串中的A能否更换为@? 1.程序接收输入AAAAAAAAAAAAAAAAAAAAAAAA ...