「 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服务名称里什么也没有,只有一个提示:(如图) 上网搜了,试了 ...
- XTU1266:Parentheses(贪心+优先队列)
传送门 题意 从左到右有n个连续的组,每一组有Li个括号,要么全是左括号,要么全是右括号,以及该组的每一个左括号翻成右括号, 或者右括号翻成左括号的花费Di.可以对这n个组的括号进行翻转,每一个括号都 ...
- MapperException: 无法获取实体类xxxxx对应的表名! 三种解决方法,总有一款适合你。
先把自动重启关一下 devtools: restart: #热部署生效 enabled: false 把devtools给删除了,说是这个jar包导致 ApplyApplication里面的@Mapp ...
- 键值观察 KVO
http://www.cnblogs.com/dyf520/p/3805297.html Key-Value Observing Programming Guide 1,注册Key-Value Obs ...
- Hdu 4513 吉哥系列故事——完美队形II (manacher变形)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4513 题目描述: 打完题目描述了,点开题目,发现题目是中文,orz.jpg.果断又删掉了,习惯真可怕 ...
- Educational Codeforces Round 20 B
Description You are given the array of integer numbers a0, a1, ..., an - 1. For each element find th ...
- windows clone 迁移数据库
windows clone 迁移数据库可行.(c 盘底成复制)
- C语言精确微秒级的延时
//----------------------------------------------------------------------------- // Delay_us //------ ...
- 启动azkaban时出现User xml file conf/azkaban-users.xml doesn't exist问题解决(图文详解)
问题详情 [hadoop@master azkaban]$ ll total drwxrwxr-x hadoop hadoop May : azkaban- drwxrwxr-x hadoop h ...
- Apache Cordova
http://cordova.apache.org/ Apache Cordova is a platformfor building native mobile applications using ...