题目描述

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. Linux 宝塔面板忘记密码的解决方案

    进入ssh 输入以下命令重置密码(把命令最后面的   “testpasswd”  替换成你要改的新密码)注:若是debian/ubuntu用户,请使用有root权限的账户去执行这条命令 cd /www ...

  2. 安装jupyter使用notebook

    安装jupyter pip3 install jupyter --default-timeout=1000 -i https://pypi.tuna.tsinghua.edu.cn/simple 使用 ...

  3. Git本地仓库的使用

    Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git 与 SVN 区别点: 1.Git 是分布式的,SVN 不是:这是 Git 和其它非分布式的版本控制系统,例如 S ...

  4. ASP.NET常用内置对象(二)Response

    response翻译为中文:响应. 将数据作为请求的结果从服务器发送到客户浏览器中,并提供有关响应的消息.它可用来在页面中输出数据,在页面中跳转,还可以传递各个页面的参数. Response对象是Sy ...

  5. 网络中的 TCP/IP

    TCP/IP OSI的“实现”:TCP/IP OSI七层模型 TCP/IP概念层模型 功能 TCP/IP协议族 应用层 应用层 文件传输.电子邮件.文件服务.虚拟终端 FTP,HTTP,SMTP,SN ...

  6. npm vs yarn

    npm yarn npm install yarn npm install react --save yarn add react npm uninstall react --save yarn re ...

  7. Yeoman+Bower+gulp web前端自动化工作流程(初级教程)

    Yeoman包括了三个部分yo(脚手架工具).grunt/gulp(构建工具).bower(包管理器).听说gulp更容易上手,所以我就没用grunt而选的gulp 什么是开发流程? 在我看来一个完整 ...

  8. win10文件夹 无法显示当前所有者 管理员都不行

    1.在win10系统桌面上,任务栏,右键,单击任务管理器. 2.单击性能. 3.单击打开资源监视器. 4.在单击CPU标签,然后再“关联的句柄”右侧的搜索框中输入要删除的文件夹名.例:tre文件夹名. ...

  9. babel 的简单使用

    之前在项目中使用.balelrc文件,但是一直不知道具体怎么使用,就知道可以将es6语法转码为es5语法. 今天就简单的做个例子,也算是记录一下困扰了好久的问题. 转码步骤: 首先在项目的目录中安装B ...

  10. python之爬虫(爬取.ts文件并将其合并为.MP4文件——以及一些异常的注意事项)

    //20200115 最近在看“咱们裸熊——we bears”第一季和第三季都看完了,单单就第二季死活找不到,只有腾讯有资源,但是要vip……而且还是国语版……所以就瞄上了一个视频网站——可以在线观看 ...