「 SPOJ GSS3 」 Can you answer these queries III
# 题目大意
GSS3 - Can you answer these queries III
需要你维护一种数据结构,支持两种操作:
- 单点修改
- 求一个区间的最大子段和
# 解题思路
一个区间的最大子段和(GSS),只能通过三种方式转移而来。
- 左儿子的最大子段和
- 右儿子的最大子段和
- 左儿子的最大右子段和+右儿子的最大左子段和
那这就比较好办了。只需要维护四个东西就可以了
- sum,区间和
- gss,最大子段和
- gssl,最大左子段和
- gssr,最大右子段和
emmm,比较可做。
# 代码
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
inline int read() {
int x = , f = ; char c = getchar();
while (c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while (c <= '' && c >= '') {x = x* + c-''; c = getchar();}
return x * f;
}
const int maxn = 5e4+, inf = ;
int n, m, opt, l, r;
struct node {
int l, r, gss, gssr, gssl, sum;
}tree[maxn << ];
struct TREE {
#define Lson (k << 1)
#define Rson ((k << 1) + 1)
inline int MAX(int a, int b, int c) {
return max(max(a, b), c);
}
inline void build(int k, int ll, int rr) {
tree[k].l = ll, tree[k].r = rr;
if(tree[k].l == tree[k].r) {
tree[k].sum = read();
tree[k].gss = tree[k].gssr = tree[k].gssl = tree[k].sum;
return ;
}
int mid = (tree[k].l + tree[k].r) >> ;
build (Lson, tree[k].l, mid);
build (Rson, mid+, tree[k].r);
tree[k].sum = tree[Lson].sum + tree[Rson].sum;
tree[k].gss = MAX(tree[Lson].gss, tree[Rson].gss, tree[Lson].gssr+tree[Rson].gssl);
tree[k].gssr = max(tree[Rson].gssr, tree[Rson].sum+tree[Lson].gssr);
tree[k].gssl = max(tree[Lson].gssl, tree[Lson].sum+tree[Rson].gssl);
}
inline void update(int k, int pos, int num) {
if(tree[k].l == tree[k].r && tree[k].l == pos) {
tree[k].sum = num;
tree[k].gss = tree[k].gssr = tree[k].gssl = tree[k].sum;
return ;
}
int mid = (tree[k].l + tree[k].r) >> ;
if(pos <= mid) update(Lson, pos, num);
else update(Rson, pos, num);
tree[k].sum = tree[Lson].sum + tree[Rson].sum;
tree[k].gss = MAX(tree[Lson].gss, tree[Rson].gss, tree[Lson].gssr+tree[Rson].gssl);
tree[k].gssr = max(tree[Rson].gssr, tree[Rson].sum+tree[Lson].gssr);
tree[k].gssl = max(tree[Lson].gssl, tree[Lson].sum+tree[Rson].gssl);
}
inline node query(int k, int L, int R) {
if(tree[k].l == L && tree[k].r == R) return tree[k];
int mid = (tree[k].l + tree[k].r) >> ;
if(L > mid) return query(Rson, L, R);
else if(R <= mid) return query(Lson, L, R);
else {
node lson, rson, res;
lson = query(Lson, L, mid);
rson = query(Rson, mid+, R);
res.sum = lson.sum + rson.sum;
res.gss = MAX(lson.gss, rson.gss, lson.gssr+rson.gssl);
res.gssl = max(lson.gssl, lson.sum+rson.gssl);
res.gssr = max(rson.gssr, rson.sum+lson.gssr);
return res;
}
}
}T;
int main() {
n = read(), T.build(, , n);
m = read();
for(int i=; i<=m; i++) {
opt = read(), l = read(), r = read();
if(opt == ) printf("%d\n", T.query(, l, r).gss);
else T.update(, l, r);
}
}
「 SPOJ GSS3 」 Can you answer these queries III的更多相关文章
- GSS3 C - Can you answer these queries III
//在gss1的基础上加了修改操作,一样的做法,加一个modify函数就可以了 #include<iostream> #include<cstdio> #include< ...
- 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 3105」Power Modulo Inverted
「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自 ...
- GSS3 SPOJ 1716. Can you answer these queries III gss1的变形
gss2调了一下午,至今还在wa... 我的做法是:对于询问按右区间排序,利用splay记录最右的位置.对于重复出现的,在splay中删掉之前出现的位置所在的节点,然后在splay中插入新的节点.对于 ...
- 数据结构(线段树):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
SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] ...
- Can you answer these queries III
Can you answer these queries III 题目:洛谷 SPOJ [题目描述] 给定长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“0 x y”,把A[x]改 ...
- Can you answer these queries III(线段树)
Can you answer these queries III(luogu) Description 维护一个长度为n的序列A,进行q次询问或操作 0 x y:把Ax改为y 1 x y:询问区间[l ...
- 「SP1716」GSS3 - Can you answer these queries III
传送门 Luogu 解题思路 区间最大子段和板子题. 考虑用线段树来做. 对于一个线段树节点所包含区间,它的最大子段和有两种情况,包含中点与不包含. 不包含的情况直接从左右子树转移. 对于包含的情况: ...
随机推荐
- sql server2008配置管理工具服务显示远程过程调用失败
SQL SERVER2008配置管理工具服务显示远程过程调用失败 前两天,装了VS2012后,打开SQL2008配置管理工具,发现SQL服务名称里什么也没有,只有一个提示:(如图) 上网搜了,试了 ...
- 51单片机 HC05蓝牙模块
一.注意事项 1.烧写程序时,要把蓝牙tx,rx信号线拔掉,对烧写程序有影响. 2.执行HC05集命令时,均以\r\n结尾.串口中断若选择“发送新行”时,不用添加\r\n.原理相同. 二.准备软硬件 ...
- 解决 CentOS 7 添加用户设置家目录出现 useradd cannot set SELinux context for home directory 问题
问题描述 直接贴下代码吧~ [root@localhost ~]# useradd -d /tmp/heheda4 heheda4 useradd: cannot set SELinux contex ...
- IT兄弟连 JavaWeb教程 Servlet API
Java Servlet是运行在带有支持Java Servlet规范的解释器的web服务器上的Java类. Servlet可以使用javax.servlet和javax.servlet.http包创建 ...
- AspectCore的AOP操作
AOP实现缓存的一个例子 using AspectCore.DynamicProxy; using Microsoft.Extensions.Caching.Memory; [AttributeUsa ...
- $BREEZE'S Diary$
蒟蒻的日记没什么好看的. 2019-01-28 期末砸了. 洛谷开创小号. 开创博客园. 2019-01-29 坐标:义乌中学 咱今天又来义乌中学受虐了 感谢hjf给咱一次爆0的机会 题解 2019- ...
- java sevlet Session
* 如果浏览器支持Cookie,创建Session的时候会把SessionId保存在Cookie中 * 否则必须自己编程使用URL重写的方式实现Session:response.encodeURL()
- udp聊天交互
#****server端 import socket sk = socket.socket(type = socket.SOCK_DGRAM) sk.bind(('127.0.0.1', 8050)) ...
- 165 Compare Version Numbers 比较版本号
比较两个版本号 version1 和 version2.如果 version1 大于 version2 返回 1,如果 version1 小于 version2 返回 -1, 除此以外 返回 0.您可 ...
- FACVSPOW - Factorial vs Power 数学方法 + 二分
http://www.spoj.com/problems/FACVSPOW/ 求解n! > a^n最小的整数n 对于有n!和a^n的东西,一般是取ln 然后就是求解 (ln(1) + ln(2) ...