# 题目大意

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的更多相关文章

  1. GSS3 C - Can you answer these queries III

    //在gss1的基础上加了修改操作,一样的做法,加一个modify函数就可以了 #include<iostream> #include<cstdio> #include< ...

  2. 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 ...

  3. 「SPOJ 3105」Power Modulo Inverted

    「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自 ...

  4. GSS3 SPOJ 1716. Can you answer these queries III gss1的变形

    gss2调了一下午,至今还在wa... 我的做法是:对于询问按右区间排序,利用splay记录最右的位置.对于重复出现的,在splay中删掉之前出现的位置所在的节点,然后在splay中插入新的节点.对于 ...

  5. 数据结构(线段树):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 ...

  6. 线段树 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] ...

  7. Can you answer these queries III

    Can you answer these queries III 题目:洛谷 SPOJ [题目描述] 给定长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“0 x y”,把A[x]改 ...

  8. 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 ...

  9. 「SP1716」GSS3 - Can you answer these queries III

    传送门 Luogu 解题思路 区间最大子段和板子题. 考虑用线段树来做. 对于一个线段树节点所包含区间,它的最大子段和有两种情况,包含中点与不包含. 不包含的情况直接从左右子树转移. 对于包含的情况: ...

随机推荐

  1. poj 1988 Cube Stacking【带权并查集】

    设s[x]为x所在栈里的个数,c[x]表示x下面有几个,合并的时候直接合并s,然后路径压缩的时候更新c即可 #include<iostream> #include<cstdio> ...

  2. NowCoder数列

    题目:https://www.nowcoder.com/questionTerminal/0984adf1f55a4ba18dade28f1ab15003 #include <iostream& ...

  3. Github配置SSH连接

    安装git.exe,打开Git Bash 1.检查是否已经有SSH Key. $cd /.ssh 2.生成一个新的SSH. $ ssh-keygen -t rsa -C "email@git ...

  4. C语言-------指针函数与函数指针的区别

    一. 在学习arm过程中发现这“指针函数”与“函数指针”容易搞错,所以今天,我自己想一次把它搞清楚,找了一些资料,首先它们之间的定义: 1.指针函数是指带指针的函数,即本质是一个函数.函数返回类型是某 ...

  5. New Land LightOJ - 1424

    New Land LightOJ - 1424 题意:找出01矩阵中最大的完全由0组成的矩阵. 方法: 重点在于转化. 先预处理(i,j)点向上最长能取到的连续的全0条的长度.然后枚举某一行作为矩阵的 ...

  6. Eclipse显示空白符,及使用google代码格式化

    启动Eclipse,打开Preferences对话框.菜单“window”-“Preferences”. 找到Text Editors,勾选show whitespace characters,如图: ...

  7. json字符串和字典类型的相互转换

    在开发过程中,有时候需要将json字符串转为字典类型,反之亦然,通常采用.Net的开源类库Newtonsoft.Json进行序列化,这里我也是采用这个,不过我更喜欢写扩展方法方便在项目的调用. 首先新 ...

  8. List<DTO>转 Map<String,List<DTO>> 两种写法

    List<TeamScheduleDTO> list = JSON.parseArray(response.getData().getJSONArray("list") ...

  9. Spring-bean(二)

    命名空间 自动装配 bean之间的关系:继承:依赖 使用外部属性文件 SpEL bean的生命周期 bean的后置处理器 (一)util命名空间 当用list,set等集合时,不能将集合作为独立的be ...

  10. 洛谷P2742 【模板】二维凸包

    题意 求凸包 Sol Andrew算法: 首先按照$x$为第一关键字,$y$为第二关键字从小到大排序,并删除重复的点 用栈维护凸包内的点 1.把$p_1, p_2$放入栈中 2.若$p_{i{(i & ...