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 ...
随机推荐
- Metaspolit工具----基础
Metasploit框架(Metasploit Framework,MSF)是一个开源工具,旨在方便渗透测试,他是有Ruby程序语言编写的模板化框架,具有很好的扩展性,便于渗透测试人员开发.使用定制的 ...
- [DE] ML on Big data: MLlib
Pipeline的最终目的就是学会Spark MLlib,这里先瞧瞧做到心里有数:知道之后要学什么,怎么学. 首要问题 一.哪些机器学习算法可以并行实现? 四类算法:分类.回归.聚类.协同过滤 以及特 ...
- 2019windows上安装Mac OS 10.14过程详细截图
之前VMware12里面的Mac OS10.10升级后,键盘鼠标就用不了了.试了几次都这样,只能重装VMware14, 安装Mac OS 10.14系统.把步骤截下图,分享一下. 一.材料准备 1.虚 ...
- Java源码跟踪阅读技巧
转:https://www.jianshu.com/p/ab865109070c 本文基于Eclipse IDE 1.Quick Type Hierarchy 快速查看类继承体系. 快捷键:Ctrl ...
- Spring Data JPA 梳理 - JPA与“Spring、Spring Data JPA”的关系
JPA其实也就是java实体对象和关系型数据库建立起映射关系,通过面向对象编程的思想操作关系型数据库的规范. Spring 框架对 JPA 提供的支持主要体现在如下几个方面: 首先,它使得 JPA 配 ...
- .net core 3.0 Signalr - 02 使用强类型的Hub
## 强类型的优缺点 - 优点 强类型的Hub可以避免魔法函数名,相比弱类型更容易维护和发现问题,直接上代码 - 缺点 特么的得多些好几行代码 ## 代码 ### 接口定义 ``` C# /// // ...
- asp.net编译中出现 数据库 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\test1.mdf' 已存在。请选择其他数据库名称。
关于asp.net编译中出现数据库 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\test1.mdf ...
- Locomotion和Navigation的区别
Locomotion和navigation两者都是移动.漫游的意思.但是locomotion是一个比navigation更大的概念,它指的是所有的第一人称视角的变换(first-person moti ...
- ng form组件(表单)
tip: 数据的双向绑定(数据的双向绑定只是针对表单) 实现数据的双向绑定需要在app_module.ts(根模块)中进行注册一些东西 import {FormsModule} from '@angu ...
- [PHP] php, apache, VS Code安装与配置
1. 下载