GSS3 - Can you answer these queries III
题意翻译
nnn 个数, qqq 次操作
操作0 x y把 AxA_xAx 修改为 yyy
操作1 l r询问区间 [l,r][l, r][l,r] 的最大子段和
感谢 @Edgration 提供的翻译
题目描述
You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations:
modify the i-th element in the sequence or for given x y print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.
输入输出格式
输入格式:
The first line of input contains an integer N. The following line contains N integers, representing the sequence A1..AN.
The third line contains an integer M. The next M lines contain the operations in following form:
0 x y: modify Ax into y (|y|<=10000).
1 x y: print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.
输出格式:
For each query, print an integer as the problem required.
输入输出样例
4
1 2 3 4
4
1 1 3
0 3 -3
1 2 4
1 3 3
6
4
-3 提交地址 : luogu SP1716
spoj; 分析:
线段树水题; 用线段树维护四个值 : 这一区间的最大子段和, 这一区间的从最左端开始的最大子段和, 从右端开始的最大子段和,还有这一段的和;
怎么维护?
t[o].sum = t[ls(o)].sum + t[rs(o)].sum;
t[o].lsum = max(t[ls(o)].lsum, t[ls(o)].sum + t[rs(o)].lsum);
t[o].rsum = max(t[rs(o)].rsum, t[rs(o)].sum + t[ls(o)].rsum);
t[o].dat = max(t[ls(o)].rsum + t[rs(o)].lsum, max(t[ls(o)].dat, t[rs(o)].dat));
就解释一个:你左端开始的最大子段和一定是你左二子的左端点开始的最大子段和, 还有左二子全选加上右儿子的左端开始的最大子段和;
其他的都大同小异;
一样的按照普通线段树写;
主要讲讲查询操作;
因为我们要找一个连续的序列,而不是每个dat取max;
所以我们要维护一个前缀和qzh;
因为我们维护的是前缀和, 所以每次可以用 qzh+t[o].lsum 和 t[o].dat 取max来更新ans;
然后我们再改变qzh的值 在 qzh + t[o].sum 和 t[o].rsum中取max;
代码奉上:
//zZhBr
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define int long long inline int read()
{
int res=;bool flag=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')flag=;ch=getchar();};
while(isdigit(ch)){res=(res<<)+(res<<)+(ch-'');ch=getchar();}
return flag?-res:res;
} const int N = ; int n, a[N], m;
int ans, qzh; struct Segment
{
int ls, rs;
int l, r;
int sum;
int lsum, rsum;
int dat;
}t[N<<];
int cnt = ;
int root;
#define ls(x) t[x].ls
#define rs(x) t[x].rs inline void pushup(int o)
{
t[o].l = t[ls(o)].l, t[o].r = t[rs(o)].r;
t[o].sum = t[ls(o)].sum + t[rs(o)].sum;
t[o].lsum = max(t[ls(o)].lsum, t[ls(o)].sum + t[rs(o)].lsum);
t[o].rsum = max(t[rs(o)].rsum, t[rs(o)].sum + t[ls(o)].rsum);
t[o].dat = max(t[ls(o)].rsum + t[rs(o)].lsum, max(t[ls(o)].dat, t[rs(o)].dat));
} inline void build(int l, int r, int o)
{
if (l == r)
{
t[o].sum = a[l];
t[o].lsum = a[l];
t[o].rsum = a[l];
t[o].dat = a[l];
t[o].l = t[o].r = l;
return;
} int mid = l + r >> ;
t[o].ls = cnt++;
t[o].rs = cnt++;
build(l, mid, ls(o));
build(mid + , r, rs(o));
pushup(o);
} inline void change(int o, int x, int v)
{
if (t[o].l == t[o].r)
{
t[o].sum = v;
t[o].dat = v;
t[o].lsum = t[o].rsum = v;
return;
} int mid = t[o].l + t[o].r >> ; if (x <= mid) change(ls(o), x, v);
else change(rs(o), x, v);
pushup(o);
} inline void query(int o, int li, int ri)
{
if (li <= t[o].l and ri >= t[o].r)
{
ans = max(ans, max(qzh + t[o].lsum, t[o].dat));
qzh = max(qzh + t[o].sum, t[o].rsum);
return;
}
int res = ;
int mid = t[o].r + t[o].l >> ;
if (li <= mid) query(ls(o), li, ri);
if (ri > mid) query(rs(o), li, ri);
} signed main()
{
n = read();
for (register int i = ; i <= n ; i ++) a[i] = read();
m = read();
root = cnt++;
build(, n, root); while (m--)
{
int opt = read();
int x = read(), y = read(); if (opt == )
{
change(root, x, y);
}
else
{
ans = -1e9, qzh = -1e9;
query(root, x, y);
printf("%lld\n", ans);
}
} return ; }
GSS3 - Can you answer these queries III的更多相关文章
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- 数据结构(线段树):SPOJ GSS3 - Can you answer these queries III
GSS3 - Can you answer these queries III You are given a sequence A of N (N <= 50000) integers bet ...
- 线段树 SP1716 GSS3 - Can you answer these queries III
SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] ...
- SP1716 GSS3 - Can you answer these queries III(单点修改,区间最大子段和)
题意翻译 nnn 个数, qqq 次操作 操作0 x y把 AxA_xAx 修改为 yyy 操作1 l r询问区间 [l,r][l, r][l,r] 的最大子段和 题目描述 You are give ...
- SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树
GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) ...
- SPOJ GSS3 Can you answer these queries III
Time Limit: 330MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- spoj 1557 GSS3 - Can you answer these queries III 线段树
题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...
- SP1716 GSS3 - Can you answer these queries III
题面 题解 相信大家写过的传统做法像这样:(这段代码蒯自Karry5307的题解) struct SegmentTree{ ll l,r,prefix,suffix,sum,maxn; }; //.. ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
- [SPOJ1716] GSS3 - Can you answer these queries III
线段树操作. 维护一个区间最大连续子段和,左最大连续子段和,右最大连续子段和即可. 最后不知道怎么搞,query的时候返回了个结构体. #include <cstdio> #include ...
随机推荐
- java使用FileSystem上传文件到hadoop文件系统
import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; import org.ap ...
- Apache Commons Collections 反序列化详细分析学习总结
0x01.环境准备: Apache Commons Collections 3.1版本,下载链接参考: https://www.secfree.com/a/231.html jd jui地址(将jar ...
- Linux ln 软、硬链接
最近在学习Linux系统的,给我的感觉就是“智慧的结晶,智慧的大脑,智慧的操作” 今天研究到了一个有趣的命令 ln 我们先来看一下它的概念吧 Linux ln命令是一个非常重要命令,它的功能是为 ...
- Day 10 用户的提权,用户组的创建删除
1.如何为用户设定密码,又如何修改密码? 2.用户的创建流程? [扩展了解] 3.用户组如何管理? 4.普通用户无权限怎么办? 切换身份 or 提权? su 切换用户 sudo 提权 5.为用户添 ...
- 55 (OC)* 图片圆角处理
iOS图片设置圆角性能优化 问题 圆角虽好,但如果使用不当,它就是你的帧数杀手,特别当它出现在滚动列表的时候.下面来看圆角如何毁掉你的流畅度的. 实测 layer.cornerRadius 我创建 ...
- Redis会遇到的问题以及解决方案
1.缓存雪崩 发生场景:当Redis服务器重启或者大量缓存在同一时期失效时,此时大量的流量会全部冲击到数据库上面,数据库有可能会因为承受不住而宕机 解决办法: 1)随机均匀设置失效时间 2)设置过期标 ...
- 【POJ - 3723 】Conscription(最小生成树)
Conscription Descriptions 需要征募女兵N人,男兵M人. 每招募一个人需要花费10000美元. 如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱. 给出若干男女之前的1 ...
- 新手学习Git之在本地使用Git
每个开发人员应该都会一个版本管理工具,在Git和SVN中,我选择以Git,以下是我的一些心得 什么是 Git Git是目前世界上最先进的分布式版本控制系统(没有之一). 一.Git安装 1).linu ...
- 【面试题】SpringMVC部分面试题
SpringMVC面试题 什么是SpringMVC ? 简单介绍下你对SpringMVC的理解 ? SpringMVC是一个基于Java的实现了MVC设计模式的请求驱动类型的轻量级Web框架,通过Mo ...
- Multiple types were found that match the controller named 'Auth'.
偶然的一个机会,修改了已经开发完毕的一个项目的命名.突然运行发现: { "Message": "An error has occurred.", "E ...