传送门

Luogu

解题思路

区间最大子段和板子题。

考虑用线段树来做。

对于一个线段树节点所包含区间,它的最大子段和有两种情况,包含中点与不包含。

不包含的情况直接从左右子树转移。

对于包含的情况:

我们对每个节点维护两个值:开头是左端点的最大子段和,结尾是右端点的最大子段和。

那么包含中点的情况可以用上面两个东西转移。

那么这两个东西又怎么维护呢。。。

他们也有包含与不包含中点的情况,只要记一下节点的区间和就可以了,具体方法同上。

于是便搞定了这道题。

细节注意事项

  • 咕咕咕

参考代码

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <cctype>
  7. #include <cmath>
  8. #include <ctime>
  9. #define rg register
  10. using namespace std;
  11. template < typename T > inline void read(T& s) {
  12. s = 0; int f = 0; char c = getchar();
  13. while (!isdigit(c)) f |= (c == '-'), c = getchar();
  14. while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
  15. s = f ? -s : s;
  16. }
  17. const int _ = 50010;
  18. int n, q, a[_];
  19. struct node { int sum, L, R, mx; }t[_ << 2];
  20. inline int lc(int rt) { return rt << 1; }
  21. inline int rc(int rt) { return rt << 1 | 1; }
  22. inline void pushup(int rt) {
  23. t[rt].sum = t[lc(rt)].sum + t[rc(rt)].sum;
  24. t[rt].L = max(t[lc(rt)].L, t[lc(rt)].sum + t[rc(rt)].L);
  25. t[rt].R = max(t[rc(rt)].R, t[rc(rt)].sum + t[lc(rt)].R);
  26. t[rt].mx = max(t[lc(rt)].R + t[rc(rt)].L, max(t[lc(rt)].mx, t[rc(rt)].mx));
  27. }
  28. inline void build(int rt = 1, int l = 1, int r = n) {
  29. if (l == r) { t[rt] = (node) { a[l], a[l], a[l], a[l] }; return; }
  30. int mid = (l + r) >> 1;
  31. build(lc(rt), l, mid), build(rc(rt), mid + 1, r), pushup(rt);
  32. }
  33. inline void update(int id, int v, int rt = 1, int l = 1, int r = n) {
  34. if (l == r) { t[rt] = (node) { v, v, v, v }; return; }
  35. int mid = (l + r) >> 1;
  36. if (id <= mid) update(id, v, lc(rt), l, mid);
  37. else update(id, v, rc(rt), mid + 1, r);
  38. pushup(rt);
  39. }
  40. inline node query(int ql, int qr, int rt = 1, int l = 1, int r = n) {
  41. if (ql <= l && r <= qr) return t[rt];
  42. int mid = (l + r) >> 1;
  43. if (qr <= mid) return query(ql, qr, lc(rt), l, mid);
  44. if (ql > mid) return query(ql, qr, rc(rt), mid + 1, r);
  45. node ls = query(ql, mid, lc(rt), l, mid);
  46. node rs = query(mid + 1, qr, rc(rt), mid + 1, r);
  47. node res = { 0, 0, 0, 0 };
  48. res.sum = ls.sum + rs.sum;
  49. res.L = max(ls.L, ls.sum + rs.L);
  50. res.R = max(rs.R, rs.sum + ls.R);
  51. res.mx = max(ls.R + rs.L, max(ls.mx, rs.mx));
  52. return res;
  53. }
  54. int main() {
  55. #ifndef ONLINE_JUDGE
  56. freopen("in.in", "r", stdin);
  57. #endif
  58. read(n);
  59. for (rg int i = 1; i <= n; ++i) read(a[i]);
  60. build();
  61. read(q);
  62. for (int f, x, y; q--; ) {
  63. read(f), read(x), read(y);
  64. if (!f) update(x, y);
  65. else printf("%d\n", query(x, y).mx);
  66. }
  67. return 0;
  68. }

完结撒花 \(qwq\)

「SP1716」GSS3 - Can you answer these queries III的更多相关文章

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

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

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

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

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

    题目描述 You are given a sequence \(A\) of \(N (N <= 50000)\) integers between \(-10000\) and \(10000 ...

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

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

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

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

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

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

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

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

随机推荐

  1. pdf.js的使用(1) 站在巨人的肩膀上纯干货分享,没有华丽的词藻

    以下是我在实际项目开发中的过程分享   前端是:vue+jsp 1.首先下载pdf.js(怎么下可以去百度),实在不会就私我,我发给你 1.1展示一哈我下载下来的pdf.js的目录结构 1.2接下来可 ...

  2. 一类Log-Gamma积分的一般形式

    \[\Large\int_{0}^{z}x^{t}\ln\Gamma \left ( 1+x \right )\mathrm{d}x~,~z>0\, ,\, t\in N^{*}\] \(\La ...

  3. I/O-<File区别>

    FileInputStream           FileOutputStream ObjectInputStream      ObjectOutputStream  传的是对象    需要新建一 ...

  4. Win Tomcat8 占用内存过高

    1.解压版 找到tomcat/bin/catalina.bat 文件,修改对应参数 2.安装版 windows服务执行的是bin/tomcat.exe.他读取注册表中的值,而不是catalina.ba ...

  5. 转载--centos7.4安装docker

    参考博文:https://www.cnblogs.com/yufeng218/p/8370670.html 作者:风止雨歇 Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企 ...

  6. Go安装gRPC

    grpc-go的官方安装命令 go get google.golang.org/grpc 无法正常使用. 我们可以用以下的命令替代,达到同样的效果 git clone https://github.c ...

  7. linux关闭进程

    查看端口对应的pid lsof -i:8888 若要关闭使用这个端口的程序,使用kill + 对应的pid kill -9 PID号

  8. python多线程下载ts文件

    # -*- coding: utf-8 -*- """ Created on Wed Aug 22 15:56:19 2018 @author: Administrato ...

  9. 关于TXT文件中英文单词出现频率排序问题

    题目要求: 指定文件目录, 但是会递归遍历目录下的所有子目录,输出文件中所有不重复的单词,按照出现次数由多到少排列. 源码: package word; import java.io.File;  i ...

  10. SQL的四种连接(内连接,外连接)

    一,内连接(inner join) 内连接(INNER JOIN):分显式的和隐式的,返回连接表中符合连接条件和查询条件的数据行.(所谓的连接表就是数据库在做查询形成的中间表). 1.隐式的内连接 没 ...