# 题目大意

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);
}
}

最新文章

  1. sscanf_强大的数据读取-转换
  2. Dynamics AX7 materials
  3. Foundation框架—字符串
  4. CSS3 filter:drop-shadow滤镜与box-shadow区别应用 抄的
  5. TCP及IP报头及协议
  6. linux下php扩展curl的安装
  7. HBASE完全分布式模式的安装
  8. Ubuntu中MongoDB安装
  9. MySQL索引之B+树
  10. ubuntu 卸载从源码安装的 emacs
  11. 神奇的namespace使用
  12. linux下C获取文件的大小
  13. Angular ngRepea
  14. Ceph常规操作及常见问题梳理
  15. would you please...could you please...两句区别是什么?
  16. SQL按分隔符拆分字段串
  17. hdu1837 看病要排队(优先队列)
  18. ArrayList,LinkList,HashMap
  19. POJ 3734 Blocks(矩阵快速幂+矩阵递推式)
  20. Struts详解

热门文章

  1. ARC和MRC混合使用
  2. &lt;TLE&gt;奇偶剪枝hdoj1010
  3. hdoj1078【DP&#183;记忆化搜索】
  4. 洛谷 P2578 [ZJOI2005]九数码游戏【bfs+康托展开】
  5. curl:出现SSL错误提示
  6. keepalived+nginx高可用实现
  7. Hdu 4725 The Shortest Path in Nya Graph (spfa)
  8. LightOj 1076 - Get the Containers (折半枚举好题)
  9. _bzoj1191 [HNOI2006]超级英雄Hero【构图 并查集】
  10. 洛谷1387(基础二维dp)