传送门

Luogu

解题思路

区间最大子段和板子题。

考虑用线段树来做。

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

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

对于包含的情况:

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

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

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

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

于是便搞定了这道题。

细节注意事项

  • 咕咕咕

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} const int _ = 50010; int n, q, a[_];
struct node { int sum, L, R, mx; }t[_ << 2]; inline int lc(int rt) { return rt << 1; } inline int rc(int rt) { return rt << 1 | 1; } inline void pushup(int rt) {
t[rt].sum = t[lc(rt)].sum + t[rc(rt)].sum;
t[rt].L = max(t[lc(rt)].L, t[lc(rt)].sum + t[rc(rt)].L);
t[rt].R = max(t[rc(rt)].R, t[rc(rt)].sum + t[lc(rt)].R);
t[rt].mx = max(t[lc(rt)].R + t[rc(rt)].L, max(t[lc(rt)].mx, t[rc(rt)].mx));
} inline void build(int rt = 1, int l = 1, int r = n) {
if (l == r) { t[rt] = (node) { a[l], a[l], a[l], a[l] }; return; }
int mid = (l + r) >> 1;
build(lc(rt), l, mid), build(rc(rt), mid + 1, r), pushup(rt);
} inline void update(int id, int v, int rt = 1, int l = 1, int r = n) {
if (l == r) { t[rt] = (node) { v, v, v, v }; return; }
int mid = (l + r) >> 1;
if (id <= mid) update(id, v, lc(rt), l, mid);
else update(id, v, rc(rt), mid + 1, r);
pushup(rt);
} inline node query(int ql, int qr, int rt = 1, int l = 1, int r = n) {
if (ql <= l && r <= qr) return t[rt];
int mid = (l + r) >> 1;
if (qr <= mid) return query(ql, qr, lc(rt), l, mid);
if (ql > mid) return query(ql, qr, rc(rt), mid + 1, r);
node ls = query(ql, mid, lc(rt), l, mid);
node rs = query(mid + 1, qr, rc(rt), mid + 1, r);
node res = { 0, 0, 0, 0 };
res.sum = ls.sum + rs.sum;
res.L = max(ls.L, ls.sum + rs.L);
res.R = max(rs.R, rs.sum + ls.R);
res.mx = max(ls.R + rs.L, max(ls.mx, rs.mx));
return res;
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int i = 1; i <= n; ++i) read(a[i]);
build();
read(q);
for (int f, x, y; q--; ) {
read(f), read(x), read(y);
if (!f) update(x, y);
else printf("%d\n", query(x, y).mx);
}
return 0;
}

完结撒花 \(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. Golang核心编程

    源码地址: https://github.com/mikeygithub/GoCode 第1章 1Golang 的学习方向 Go 语言,我们可以简单的写成 Golang 1.2Golang 的应用领域 ...

  2. jQuery结合CSS实现手风琴组件(2)----利用seajs实现静态资源模块化引入

    1. 目录结构(webStrom) 2. 代码 1.html <!DOCTYPE html> <html lang="en"> <head> & ...

  3. 关于calendar修改前的代码和修改后的代码

    Java编写的日历,输入年月,输出这个月的日期与星期 修改前的代码: import java.io.BufferedReader; import java.io.IOException; import ...

  4. wc、grep 、 cut、paste 和 tr 命令的用法

    1 wc 命令 wc 命令是一个统计的工具,主要用来显示文件所包含的行.字和字节数. wc 命令是 word count 的缩写. (1)命令格式 wc [选项] [文件] (2)常用参数 参数 描述 ...

  5. JShell的使用

    JShell脚本工具是JDK9的新特性 启动JShell工具,在DOS命令行直接输入JShell命令.(如下例) 这里我们用Hello,World举例: 结果显示是正确的. 这里再举一个例子: 结果可 ...

  6. Style 继承

    在应用某个主题后,想在此基础上自定义新的样式,可以使用如下方式继承样式. <Style x:Key="ListViewItemStyle" TargetType=" ...

  7. 了解jQuery

    前言-- 通过这篇文章[https://www.cnblogs.com/cchHers/p/9880439.html]了解到JavaScript是编写控制器这种角色语言.文章中也提到了web开始是一门 ...

  8. 重新理解业务里程碑----HHR计划----以太一堂第二课

    ---- 理解业务背后的逻辑,抓住创业重点. 第一课:开始学习 1,FA : financial advisor.财务顾问. 2,本节课的目的:抓住创业的重点. 3,预热思考题: (1) 如果把你的整 ...

  9. java基础课程笔记 static 主函数 静态工具类 classpath java文档注释 静态代码块 对象初始化过程 设计模式 继承 子父类中的函数 继承中的构造函数 对象转型 多态 封装 抽象类 final 接口 包 jar包

    Static那些事儿 Static关键字 被static修饰的变量成为静态变量(类变量) 作用:是一个修饰符,用于修饰成员(成员变量,成员方法) 1.被static修饰后的成员变量只有一份 2.当成员 ...

  10. mybatis 查询标签

    语法 参考:http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html <![CDATA[内容]]>: 参考: http://blog.csd ...