题解【SP1716】GSS3 - Can you answer these queries III
题目描述
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\{A_i + A_{i+1} + .. + A_j\) \(|\) \(x<=i<=j<=y \}\).
输入输出格式
输入格式
The first line of input contains an integer \(N\).
The following line contains \(N\) integers, representing the sequence \(A_{1}..A_{N}\).
The third line contains an integer \(M\). The next \(M\) lines contain the operations in following form:
0 x y: modify \(A_x\) into \(y\) \((|y|<=10000)\).
1 x y: print \(max\{A_i + A_{i+1} + .. + A_j\) \(|\) \(x<=i<=j<=y\}\).
输出格式
For each query, print an integer as the problem required.
输入输出样例
输入样例#1
4
1 2 3 4
4
1 1 3
0 3 -3
1 2 4
1 3 3
输出样例#1
6
4
-3
题意翻译
\(n\) 个数,\(q\) 次操作
操作0 x y把\(A_x\) 修改为\(y\)
操作1 l r询问区间\([l, r]\)的最大子段和
题解
这题就是GSS1的带修改版本,建议先看一看我的题解,了解不带修改的版本怎么写。
本题的代码基于我GSS1的题解,一些注释也可以在那里看到。
这里讲一讲怎么修改:
与线段树相似,如果当前已经访问到了叶子节点,就直接将这个节点的所有参数都设为要修改的值即可,否则就递归左子树或右子树。最后记得上传节点!
下面给出\(AC\)代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar();}
return f * x;
}
int n, m;
struct Node
{
int sum, lans, rans, ans;
} t[50005 << 2];
inline void pushup(int x)
{
t[x].sum = t[x << 1].sum + t[(x << 1) | 1].sum;
t[x].lans = max(t[x << 1].lans, t[x << 1].sum + t[(x << 1) | 1].lans);
t[x].rans = max(t[(x << 1) | 1].rans, t[(x << 1) | 1].sum + t[x << 1].rans);
t[x].ans = max(max(t[x << 1].ans, t[(x << 1) | 1].ans), t[x << 1].rans + t[(x << 1) | 1].lans);
}
void bulid(int s, int o, int p)
{
if (s == o)
{
t[p].sum = t[p].lans = t[p].rans = t[p].ans = gi();
return;
}
int mid = (s + o) >> 1;
bulid(s, mid, p << 1);
bulid(mid + 1, o, (p << 1) | 1);
pushup(p);
}
void modify(int l, int r, int s, int o, int p)//修改操作
{
if (s == o)//已经是叶子节点了
{
t[p].ans = t[p].lans = t[p].rans = t[p].sum = r;//就更新它的所有参数
return;
}
int mid = (s + o) >> 1;//找中点
if (l <= mid) modify(l, r, s, mid, p << 1);//点在中点左边就递归左子树
else modify(l, r, mid + 1, o, (p << 1) | 1);//否则递归右子树
pushup(p);//上传节点
}
Node getans(int l, int r, int s, int o, int p)
{
if (l <= s && r >= o)
{
return t[p];
}
int mid = (s + o) >> 1;
if (l > mid) return getans(l, r, mid + 1, o, (p << 1) | 1);
if (r <= mid) return getans(l, r, s, mid, p << 1);
else
{
Node ans, a, b;
a = getans(l, r, s, mid, p << 1), b = getans(l, r, mid + 1, o, (p << 1) | 1);
ans.sum = a.sum + b.sum;
ans.ans = max(max(a.ans, a.rans + b.lans), b.ans);
ans.lans = max(a.lans, a.sum + b.lans);
ans.rans = max(b.rans, b.sum + a.rans);
return ans;
}
}
int main()
{
n = gi();
bulid(1, n, 1);
m = gi();
for (int i = 1; i <= m; i++)
{
int fl = gi(), x = gi(), y = gi();
if (fl == 1) printf("%d\n", getans(x, y, 1, n, 1).ans);//如果是要求答案就输出答案
else modify(x, y, 1, n, 1);//否则就进行修改
}
return 0;
}
题解【SP1716】GSS3 - Can you answer these queries III的更多相关文章
- 线段树 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 线段树
问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...
- SP1716 GSS3 - Can you answer these queries III
题面 题解 相信大家写过的传统做法像这样:(这段代码蒯自Karry5307的题解) struct SegmentTree{ ll l,r,prefix,suffix,sum,maxn; }; //.. ...
- 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[线段树]
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】
\[ Preface \] 没有 Preface. \[ Description \] 维护一个长度为 \(n\) 的数列 \(A\) ,需要支持以下操作: 0 x y 将 \(A_x\) 改为 \( ...
- 【SP1716】GSS3 - Can you answer these queries III(动态DP)
题目链接 之前用线段树写了一遍,现在用\(ddp\)再写一遍. #include <cstdio> #define lc (now << 1) #define rc (now ...
随机推荐
- Linux网络课程学习第六天
本节课程主要内容:针对第四章节进行了收尾,以及对第五章的用户身份与文件权限进行了详细讲解. 学习心得:干货很多,收获满满.
- 如何在 messager/alert/confirm等消息提示框中 获取 / 设置 嵌入 html内容中的 input[type=checkbox]等的选中状态?
总结, 有3点: 不能/不要 在 这些消息框 / 提示框/ 对话框中的 回调函数中去写代码: 获取嵌入 内容中input.checkbox的选中状态, 因为 虽然在这些框存在的时候, 这个 check ...
- Tiptop ERP 采购运费一键分摊
项目背景: 公司的采购运费在逐年上升,之前财务都是做在管理费用中,金额大了后已经严重造成成本失真,所以财务要求it部能帮助分摊运费 1.纸质单据 2.系统入库单apmt720 3.系统请款单apm ...
- C# WPF遮罩对话框(Popup Message Overlay/ Dialog Host)
时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...
- css总结 -使用display:inline-block,出现元素高度错位
在进行页面布局时发现一个问题,两个相同高度的元素显示高度不一致,发生错位. <style> .left{ display:inline-block; height:110p ...
- sift代码实现详解
1.创建高斯金字塔第-1组 1.1.将源图片转成灰度图 void ConvertToGray(const Mat& src, Mat& dst) { cv::Size size = s ...
- 如何查看oracle当前连接数,会话数
第一步,在cmd命令行,输入sqlplus 第二步,根据提示输入用户名与密码 1. 查看processes和sessions参数 SQL> show parameter processes NA ...
- LaTeX技巧011:LaTtex中如何产生直立体希腊字母?
%\usepackage{upgreek}\upmu \uppi
- Java期末考试冲刺总结
经过长达将近三个小时的冲刺,我感觉身心俱疲,但它无法掩盖我敲代码的欲望! 三个小时我只实现了公文流转系统的的部分功能. 我深刻的意识到建民老师说的这套关系之复杂,它真的是太复杂了!!!没有系统的梳理, ...
- MongoDB geonear和文本命令驱动程序2.0
文本查询,q作为查询字符串: coll.FindAsync<Foo>(Builders<Foo>.Filter.Text(q)); 文本查询需要一个文本索引.要从C#创建代码, ...