「SP1716」GSS3 - Can you answer these queries III
传送门
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的更多相关文章
- 【SP1716】GSS3 - Can you answer these queries III(动态DP)
题目链接 之前用线段树写了一遍,现在用\(ddp\)再写一遍. #include <cstdio> #define lc (now << 1) #define rc (now ...
- 题解 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
题目描述 You are given a sequence \(A\) of \(N (N <= 50000)\) integers between \(-10000\) and \(10000 ...
- 线段树 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] ...
- 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(单点修改,区间最大子段和)
题意翻译 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 线段树
问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...
- SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树
GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) ...
随机推荐
- slf4j-api整合maven 工程日志配置文件
springmvc项目 pom.xml: <dependency> <groupId>org.slf4j</groupId> <artifactId>s ...
- 吴裕雄 python 神经网络——TensorFlow 实现LeNet-5模型处理MNIST手写数据集
import os import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import ...
- rtt之通用bootloader
目前只支持F1/F4;使用步骤 1 在官网注册产品,根据系列设定参数,接收邮箱,点击生成就可以在自己的邮箱中收到对应的bootloader.bin文件.用jlink就可以将其烧写进单片机. 2 存储被 ...
- 洛谷 T2691 桶哥的问题——送桶
嗯... 题目链接:https://www.luogu.org/problem/T2691 这道题有一点贪心的思想吧...并且思路与题目是倒着来的(貌似这种思路已经很常见的... 先举个栗子: 引出思 ...
- 「CF197B Limit」
题目撞名 题目大意: 给出两个函数 \(P(x),Q(x)\). \(P(x)=a_0 \times x^N+a_1 \times x^{N-1}+a_2 \times x^{N-2} \cdots ...
- python浅谈编程规范和软件开发目录规范的重要性
前言 我们这些初学者,目前要做的就是遵守代码规范,这是最基本的,而且每个团队的规范可能还不一样,以后工作了,尽可能和团队保持一致,目前初学者就按照官方的要求即可 新人进入一个企业,不会接触到核心的架构 ...
- WPF 体系结构
转载地址:http://blog.csdn.net/changtianshuiyue/article/details/38963477 本主题提供 Windows Presentation Found ...
- Java入门笔记 02-数组
介绍: Java的数组既可以存储基本类型的数据,也可以存储引用类型的数据,但是要求所有的数组元素具有相同的数据类型.另外,Java数组也是一种数据类型,其本身就是一种引用类型. 一.数组的定义: 数据 ...
- CSP2019 滚粗记
目录 CSP 2019 游记 DAY 0 DAY 1 DAY 2 CSP总结 自测之后 CSP 2019 游记 坐标:GD,GZ 人物:hyf 组别:J和S 任务:划水 目标:划水 任务奖励:退役证书 ...
- js学习:函数
概述 函数的声明 JavaScript 有三种声明函数的方法 function 命令 function命令声明的代码区块,就是一个函数.function命令后面是函数名,函数名后面是一对圆括号,里面是 ...