「 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 解题思路 区间最大子段和板子题. 考虑用线段树来做. 对于一个线段树节点所包含区间,它的最大子段和有两种情况,包含中点与不包含. 不包含的情况直接从左右子树转移. 对于包含的情况: ...
随机推荐
- 洛谷 - P2045 - 方格取数加强版 - 费用流
原来这种题的解法是费用流. 从一个方格的左上走到右下,最多走k次,每个数最多拿走一次. 每次走动的流量设为1,起始点拆点成限制流量k. 每个点拆成两条路,一条路限制流量1,费用为价值相反数.另一条路无 ...
- Android Studio提示忽略大小写
Android Studio的自动提示功能非常之强大,但是,如果你要输入“String”,你输入“string”,这个是不会提示的,也就是大小写敏感的,不爽是吗? 选择大小写不敏感就ok了!这样你想怎 ...
- C++开发工程师面试题库 200~250道
199 MFC中SendMessage和PostMessage的区别?答:PostMessage 和SendMessage的区别主要在于是否等待应用程序做出消息处理.PostMessage只是把消息 ...
- 洛谷 P4719 【模板】动态dp【动态dp】
是动态dp的板子 大致思想就是用g[u]来表示不包含重链转移的dp值,然后用线段树维护重链,这样线段树的根就相当于这条重链的top的真实dp值 每次修改的时候,修改x点会影响到x到根的真实dp值,但是 ...
- locate的基本用法
一.工作原理 1. locate是通过读取一个或多个由updatedb命令生成的数据库来查找文件的,而updatedb由计划任务程序cron每天运行来更新缺省的数据库(/var/lib/mlocate ...
- USACO Training3.2 01串 By cellur925
题目传送门 一句话题意:求长度为n的有m个1的大小为第k个的01串. 暑假我做的时候是真·大暴力,用二进制枚举,55分,成功T掉无数点. 正解:开始可以用计数类dp来“预处理”,状态和转移都比较好想. ...
- poj1273 Drainage Ditches 基础网络流
#include <iostream> #include <queue> using namespace std; ][]; ]; //路径上每个节点的前驱节点 ]; int ...
- AtCoder Grand Contest 018 D - Tree and Hamilton Path
题目传送门:https://agc018.contest.atcoder.jp/tasks/agc018_d 题目大意: 给定一棵\(N\)个点的带权树,求最长哈密顿路径(不重不漏经过每个点一次,两点 ...
- codeforces 615 D. Multipliers (数论 + 小费马定理 + 素数)
题目链接: codeforces 615 D. Multipliers 题目描述: 给出n个素数,这n个素数的乘积等于s,问p的所有因子相乘等于多少? 解题思路: 需要求出每一个素数的贡献值,设定在这 ...
- angular 2 angular quick start Could not find HammerJS
Angular2 的material中 引用了 hammerjs,遇到Could not find HammerJS错误,正确的步骤如下: 需要在如下位置增加 对material 和 hammerjs ...