题解【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 ...
随机推荐
- 剑指offer-基础练习-快速排序-排序
/* 题目:快速排序 */ /* 思路:将一个数组分为两份,左边的数字小于index,右边的数字大于index,递归划分后形成一个排序后的数组. */ void QuickSort(int data[ ...
- 洛谷【P5004 专心OI - 跳房子】 题解
题目链接 https://www.luogu.org/problem/P5004 洛谷 P5004 专心OI - 跳房子 Imakf有一天参加了PINO 2017 PJ组,他突然看见最后一道题 他十分 ...
- Luogu2345 | 奶牛集会 (树状数组)
题目背景 MooFest, 2004 Open 题目描述 约翰的 \(N\) 头奶牛每年都会参加"哞哞大会".哞哞大会是奶牛界的盛事.集会上的活动很多,比如堆干草,跨栅栏,摸牛仔的 ...
- 用 ArcMap 发布 ArcGIS Server Feature Server Feature Access 服务
1. 安装Desktop, 2. 安装ArcGIS Server 3. 安装PostgreSQL 9.5 从 C:\Program Files (x86)\ArcGIS\Desktop10.5\Dat ...
- float浮动造成高度塌陷的解决办法
Float是我们在页面布局中常用的,也是非常重要的一个属性,可以让页面布局变得更加灵活. 但是在继续学习之后,尤其是掌握了宽高自适应之后,我们常常会发现一个奇怪的现象:如果父元素没有设置高度,而子元素 ...
- POJ3723(最小生成树,负权)
题目描述 温迪有一个国家,他想建立一支军队来保护他的国家.他收留了N个女孩和M个男孩,想把她们收留成他的士兵.征兵无特权,必须交纳一万元.女孩和男孩之间有一些关系,温迪可以利用这些关系来降低他的成本. ...
- PHP代码安全杂谈
虽然PHP是世界上最好的语言,但是也有一些因为弱类型语言的安全性问题出现.WordPress历史上就出现过由于PHP本身的缺陷而造成的一些安全性问题,如CVE-2014-0166 中的cookie伪造 ...
- 关于Oracle内存分配-解决实际运行时最大Session数不一致远小于系统配置最大的Session数目
一.相关的技术准备 1. 关于内存的介绍:https://blog.csdn.net/u013641333/article/details/82732526 2. PGA_AGGREGATE_TARG ...
- 巨杉Tech | 十分钟快速搭建 Wordpress 博客系统
介绍 很多互联网应用程序开发人员第一个接触到的网站项目就是博客系统.而全球使用最广的Wordpress常常被用户用来快速搭建个人博客网站.默认情况下,Wordpress一般在后台使用MySQL关系型数 ...
- C++-POJ2960-S-Nim-[限制型Nim]
每次只能从取集合S中个数的物品,其他和普通Nim游戏相同 预处理出每种物品堆的sg值,然后直接xor一下,xor-sum>0即必胜 #include <set> #include & ...