题目描述

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的更多相关文章

  1. 线段树 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] ...

  2. SP1716 GSS3 - Can you answer these queries III 线段树

    问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...

  3. SP1716 GSS3 - Can you answer these queries III

    题面 题解 相信大家写过的传统做法像这样:(这段代码蒯自Karry5307的题解) struct SegmentTree{ ll l,r,prefix,suffix,sum,maxn; }; //.. ...

  4. 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 ...

  5. SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树

    GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) ...

  6. 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 ...

  7. 数据结构(线段树):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 ...

  8. 题解 SP1716 【GSS3 - Can you answer these queries III】

    \[ Preface \] 没有 Preface. \[ Description \] 维护一个长度为 \(n\) 的数列 \(A\) ,需要支持以下操作: 0 x y 将 \(A_x\) 改为 \( ...

  9. 【SP1716】GSS3 - Can you answer these queries III(动态DP)

    题目链接 之前用线段树写了一遍,现在用\(ddp\)再写一遍. #include <cstdio> #define lc (now << 1) #define rc (now ...

随机推荐

  1. 与soul上的一个妹子聊天有感

    写此篇的原因: 妹子说,我考上公务员了~,当时自己自己顿时哽咽了,不知道说什么,习惯性的说了句,恭喜恭喜啊.感受到妹子的欢喜与喜悦,我也没必要打扰她的兴致,她开心就好了嘛. 每个人的成就都是自己奋斗的 ...

  2. Project Euler 133: Repunit nonfactors

    题意 英文 做法 结论1:\(R(a)|R(am)(a,m\ge 1)\) \[\frac{R(am)}{R(a)}=\frac{\frac{10^{am}-1}{9}}{R(a)}=\frac{\f ...

  3. ArcGISServer发布流程

    发布数据服务 在进行WebGIS开发中,地图显示的内容可以分成两类:一类是底图,或者是矢量的世界地图.中国地图.某个地区的底图:另一类就是业务图,对于用于遥感数据发布的WebGIS应用就是遥感影像的边 ...

  4. mysql 数据库优化的几种方法

    1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得尽 ...

  5. 实现Windows数据更新

    一.枚举 枚举是一组描述性的名称 定义一组有限的值,不能包含方法 对可能的值进行约束 .定义枚举类 public enum Gender { Male,Female } .使用枚举表示整数值 publ ...

  6. 精简Command版SqlHelper

    我在写CSharp程序对数据库进行操作时发现Connection对象起到了连接数据库的做用,实际执行SQL语句使用的是Command对象的方法,所以对SqlHelper进行了重写,具体如下: 一.创建 ...

  7. SpringBoot2.x打包成war(看这篇就够了)

    springboot默认打包成jar,如果想打包成war,则需要做以下三步. 1.修改pom.xml文件 a.将jar改成war <groupId>com.test</groupId ...

  8. java exec python program

    I find three methods, the first is using jython, the module of jython can transform the type of data ...

  9. ubuntu set up 3 - cuda

    https://www.pugetsystems.com/labs/hpc/How-to-install-CUDA-9-2-on-Ubuntu-18-04-1184/ How to install C ...

  10. php实现自定义中间logo的微信小程序码

    小程序码生成的时候是默认使用小程序后台设置的小程序icon图片的,但是在有些场景我们可能要替换成我们自己想要的icon. 下面先放代码: public function makeNewQrCodeAc ...