传送门:>Here<

题意:求区间最大子段和 $N \leq 50000$ 包括多组询问(不需要支持修改)

解题思路

线段树的一道好题

我们可以考虑,如果一组数据全部都是正数,那么问题等同于是查询区间和。然而如果有负数的存在,问题就不一样了

考虑对于每一个节点,维护四个信息:ls(代表当前区间一定顶着左端点的最大子段和),rs(同理,一定顶着右端点的),sum(区间和),val(最大子段和,也就是答案)

考虑进行转移——一个节点的信息由它的两个子节点转移而来

$ls[rt] = Max(ls[rt*2], sum[rt*2] + ls[rt*2+1])$。子段和之所以不包括整段区间是由于右端有负数。因此再往右扩展不会更优

rs同理转移。sum就不说了

$val[rt] = Max\{ val[rt*1], val[rt*1+1], ls[rt], rs[rt], rs[rt*2]+ls[rt*2+1] \}$. 最难理解的是最后一部分。

想象一下,当前区间的最大子段和要么有一头顶住端点,要么两头都不碰到端点。

对于有一头一定碰到的情况,直接用$ls[rt]和rs[rt]$转移即可。(注意,这里所说的是一定碰到,当然最大子段也有可能碰到,但是不一定)

对于都不碰到的情况,如果其不跨过中间,那么分别用两个子节点的val转移。如果恰好跨过中间,那我们需要把它拼接起来——为了使答案最优,我们考虑拼接$rs[rt*2]和ls[rt*2+1]$ (仔细思考)

查询的时候也一样,还是通过递归来完成转移。这里需要对线段树的query有一个较为深刻的理解——不同于build,query(l,r)表示的是区间$[l, r]$中包含在查询区间的那一部分,而不是真的$[l, r]$。因为在递归的时候我们会判断超界。另外,这里的转移需要刚才的四个参数,因此query的返回值应当是一个结构体,而不是单单一个数值。我暂时还没有想出非结构体的做法……

Code

/*By DennyQi*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define r read()
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
using namespace std;
typedef long long ll;
const int MAXN = ;
const int MAXM = ;
const int INF = ;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x << ) + (x << ) + c - '', c = getchar(); return x * w;
}
int N,M,x,y,opt,a[MAXN];
struct Data{ int ls,rs,sum,val; };
struct SegmentTree{
int ls[MAXN<<], rs[MAXN<<], sum[MAXN<<], val[MAXN<<];
inline void Pushup(int rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
ls[rt] = Max(ls[rt<<], sum[rt<<]+ls[rt<<|]);
rs[rt] = Max(rs[rt<<|], sum[rt<<|]+rs[rt<<]);
val[rt] = Max(Max(val[rt<<], val[rt<<|]), Max(Max(ls[rt], rs[rt]), rs[rt<<] + ls[rt<<|]));
}
void build(int L, int R, int rt){
if(L >= R){
val[rt] = sum[rt] = ls[rt] = rs[rt] = a[L];
return;
}
int Mid = (L + R) >> ;
build(L, Mid, rt<<);
build(Mid+, R, rt<<|);
Pushup(rt);
}
Data query(int L, int R, int rt, int x, int y){
if(x<=L && R<=y) return (Data){ls[rt],rs[rt],sum[rt],val[rt]};
int Mid = (L + R) >> ;
if(y <= Mid) return query(L, Mid, rt<<, x, y);
if(x >= Mid+) return query(Mid+, R, rt<<|, x, y);
Data res, t_1 = query(L, Mid, rt<<, x, y), t_2 = query(Mid+, R, rt<<|, x, y);
res.sum = t_1.sum + t_2.sum;
res.ls = Max(t_1.ls, t_1.sum + t_2.ls);
res.rs = Max(t_2.rs, t_1.rs + t_2.sum);
res.val = Max(Max(t_1.val, t_2.val), Max(Max(res.ls, res.rs), t_1.rs + t_2.ls));
return res;
}
}qxz;
int main(){
N=r;
for(int i = ; i <= N; ++i) a[i] = r;
qxz.build(, N, );
M=r;
for(int i = ; i <= M; ++i){
x = r, y = r;
printf("%d\n", qxz.query(, N, , x, y).val);
}
return ;
}

[SP1043] GSS1 - Can you answer these queries I的更多相关文章

  1. 线段树 SP1043 GSS1 - Can you answer these queries I

    SP1043 GSS1 - Can you answer these queries I 题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义 ...

  2. SP1043 GSS1 - Can you answer these queries I(线段树,区间最大子段和(静态))

    题目描述 给出了序列A[1],A[2],…,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y} ...

  3. SP1043 GSS1 - Can you answer these queries I(猫树)

    给出了序列A[1],A[2],…,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y}. 给定M ...

  4. SP1043 GSS1 - Can you answer these queries I 线段树

    问题描述 LG-SP1043 题解 GSS 系列第一题. \(q\) 个询问,求 \([x,y]\) 的最大字段和. 线段树,维护 \([x,y]\) 的 \(lmax,rmax,sum,val\) ...

  5. [题解] SPOJ GSS1 - Can you answer these queries I

    [题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...

  6. SPOJ GSS1 - Can you answer these queries I(线段树维护GSS)

    Can you answer these queries I SPOJ - GSS1 You are given a sequence A[1], A[2], -, A[N] . ( |A[i]| ≤ ...

  7. 题解【SP1043】 GSS1 - Can you answer these queries I

    题目描述 You are given a sequence \(A_1, A_2, ..., A_n(|A_i|≤15007,1≤N≤50000)\). A query is defined as f ...

  8. 线段树【SP1043】GSS1 - Can you answer these queries I

    Description 给出了序列\(A_1,A_2,-,A_n\). \(a_i \leq 15007,1 \leq n \leq 50000\).查询定义如下: 查询\((x,y)=max{a_i ...

  9. 「SP1043」GSS1 - Can you answer these queries I

    传送门 Luogu 解题思路 这题就是 GSS3 的一个退化版,不带修改操作的区间最大子段和,没什么好讲的. 细节注意事项 咕咕咕 参考代码 #include <algorithm> #i ...

随机推荐

  1. abbix通过JMX监控Tomcat(被监控端安装Tomat的服务器防火墙策略iptables配置)

    原文地址:http://jaychang.iteye.com/blog/2214830 一.目前的环境 被监控端192.168.153.191 /usr/local/tomcat 下载了catalin ...

  2. redis的应用场景 为什么用redis

    一.不是万能的菲关系系数据库redis 在面试的时候,常被问比较下Redis与Memcache的优缺点,个人觉得这二者并不适合一起比较,redis:是非关系型数据库不仅可以做缓存还能干其它事情,Mem ...

  3. 解决ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

    解决办法:

  4. Jenkins Installing and migration

    JAVA_Zookeeper_hadoop - CSDN博客https://blog.csdn.net/wangmuming Installing Jenkins on Red Hat distrib ...

  5. Windows Docker 安装

    win7.win8 .win10等需要利用 docker toolbox 来安装,国内可以使用阿里云的镜像来下载,下载地址:http://mirrors.aliyun.com/docker-toolb ...

  6. Python3练习题 021:递归方法求阶乘

    利用递归方法求5!. 方法一 f = 1 for i in range(1,6):     f = f * i print(f)   方法二 import functools print(functo ...

  7. 移动端tap事件,消除300毫秒延迟

    引用这个之前,要讲一下首先我是用了webpack技术,所以你的项目如果没有用到这个的话,最好不要用这个技术,当然想用也可以,改下代码也可以用. 下面的代码直接复制就可以用啦. ( function(e ...

  8. 通过event记录sql

    providers EventServiceProvider.php 添加 protected $listen = [ 'Illuminate\Database\Events\QueryExecute ...

  9. [转帖]震惊,用了这么多年的 CPU 利用率,其实是错的

    震惊,用了这么多年的 CPU 利用率,其实是错的 2018年12月22日 08:43:09 Linuxer_ 阅读数:50 https://blog.csdn.net/juS3Ve/article/d ...

  10. JS中的<a>标签

    <a>标签可定义锚.一个锚有两种用法: 通过使用 href 属性,创建一个到另外一个文档的链接 通过使用 name 或 id 属性,创建一个文档内部的书签 如果是在 HTML 5 中,它定 ...