题解【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 ...
随机推荐
- while else ,pass执行else,break不执行else
count = 0while count <=5: count += 1 if count == 3:pass print("Loop".count) else: print ...
- sqli-labs5-10(全程sqlmap)
sqlmap注入教程:https://www.cnblogs.com/ichunqiu/p/5805108.html 前五关直接可以用默认的sqlmap语法跑: python sqlmap.py -u ...
- nodejs安装管理工具nvm的安装和使用
https://segmentfault.com/a/1190000007612011 Windows下载安装程序安装过程中,在 Set Node.js Symlink 这一步设置nodejs程序目录 ...
- VS中关于数据库的操作
1.数据库迁移 第一步: 第二步: 在窗口中选择项目中的EntitiyFramwork项目(与数据库连接的文件集) 第三步: 输入update-database 二:数据对比 第一步: 第二步:选择需 ...
- 13:IO流
IO简介 继承结构 整体架构 常用内容 分类 根据处理的数据单位不同,分为字节流和字符流:in/out相对于程序而言的输入(读取)和输出(写出)的过程,即根据数据的流向不同称为输入流和输出流 字符流的 ...
- .Net Core的总结
一.什么是.NET Core .NET Core是一个开源通用的开发框架,支持跨平台,即支持在Window,macOS,Linux等系统上的开发和部署,并且可以在硬件设备,云服务,和嵌入式/物联网方案 ...
- Cenos7 学习笔记
一.nmtui nmtui——Text User Interface for controlling NetworkManager,这是一个NetworkManager服务的网卡接口配置工具,能实现在 ...
- C++-POJ3233-Matrix Power Series[矩阵乘法][快速幂]
构造矩阵 #include <cstdio> ; struct Matrix{int a[MAXN][MAXN];}O,I;int N; ;i<MAXN;i++);j<MAXN ...
- Gin_中间件
gin可以构建中间件,但它只对注册过的路由函数起作用 对于分组路由,嵌套使用中间件,可以限定中间件的作用范围 中间件分为全局中间件,单个路由中间件和群组中间件 gin中间件必须是一个 gin.Hand ...
- 转载:dsp芯片的定点运算
转自: http://ishare.iask.sina.com.cn/f/37179153.html