题目链接:https://cn.vjudge.net/problem/UVALive-3938

参考刘汝佳书上说的:

题意:

给出一个长度为n的序列, 再给出m个询问, 每个询问是在序列 $[a,b]$ 之间的最大连续和. 要你计算出这个这个区间内最大连续和的区间 $[x,y](a \le x \le y \le b)$;

思路:

1、构造线段树,其中每个结点维护3个值,最大连续子列和 $max\_sub$,最大前缀和 $max\_prefix$,最大后缀和 $max\_suffix$。

具体来说,

$max\_sub(a,b)$ 是满足 $a \le x \le y \le b$ 且 $D_x+D_{x+1}+…+D_y$ 最大的二元组 $(x,y)$;

$max\_prefix(a,b)$ 是满足 $a \le x \le b$ 且 $D_a+D_{a+1}+…+D_x$ 最大的整数 $x$;

$max\_suffix(a,b)$ 是满足 $a \le x \le b$ 且 $D_x+D_{x+1}+…+D_b$ 最大的整数 $x$;

例如 $n=64$,询问为 $(20,50)$,则线段 $[20,50]$ 在线段树的根结点处被分成了两条线段 $[20,32]$ 和 $[33,50]$。则 $max\_sub(20, 50)$ 的起点和终点有3种情况:

情况一:起点和终点都在 $[20,32]$ 中,则 $max\_sub(20,50)=max\_sub(20,32)$。

情况二:起点和终点都在 $[33,50]$ 中,则 $max\_sub(20,50)=max\_sub(33,50)$。

情况三:起点在 $[20,32]$ 中,终点在 $[33,50]$ 中,则 $max\_sub(20,50)=max\_suffix(20, 32) + max\_prefix(33,50)$。

类似地 $max\_suffix$ 和 $max\_prefix$ 也可以这样递推,建树的时间复杂度为 $O(n)$,单组查询的时间复杂度为 $O(\log n)$。

当然,我们实际上做的话,没这么简单:

我们每个线段树节点都维护以下几个值:

max_sub:最大连续子列和
max_pre:最大前缀和
max_suf:最大后缀和
pre_i:最大前缀的结束位置
suf_i:最大后缀的开始位置
sum:区间总和

根据上面刘汝佳书上的三种情况可以写出pushup()函数(前缀和、后缀和的更新也在包含里面),然后后面的build()和query()两个函数都可以用pushup()函数。

更多详细的情况,都在代码里体现了。

AC代码:

 #include<cstdio>
#include<algorithm>
#define MAXN 500000+5
typedef long long ll;
using namespace std;
struct Node{
int l,r;
ll sum,max_sub,max_pre,max_suf;//区间和,最大连续子列和,最大前缀和,最大后缀和
int sub_l,sub_r,pre_i,suf_i;//最大连续子列和的边界,最大前缀和的边界,最大后缀和的边界
}node[*MAXN];
int n,m,a[MAXN];
void pushup(Node& root,Node& lchild,Node& rchild)
{
root.sum = lchild.sum + rchild.sum; if(lchild.max_pre >= lchild.sum + rchild.max_pre)
{
root.max_pre = lchild.max_pre;
root.pre_i = lchild.pre_i;
}
else
{
root.max_pre = lchild.sum + rchild.max_pre;
root.pre_i = rchild.pre_i;
} if(lchild.max_suf + rchild.sum >= rchild.max_suf)
{
root.max_suf = lchild.max_suf + rchild.sum;
root.suf_i = lchild.suf_i;
}
else
{
root.max_suf = rchild.max_suf;
root.suf_i = rchild.suf_i;
} root.max_sub = lchild.max_sub;
root.sub_l = lchild.sub_l;
root.sub_r = lchild.sub_r;
if(lchild.max_suf + rchild.max_pre > root.max_sub || (lchild.max_suf + rchild.max_pre == root.max_sub && lchild.suf_i < root.sub_l))
{
root.max_sub = lchild.max_suf + rchild.max_pre;
root.sub_l = lchild.suf_i;
root.sub_r = rchild.pre_i;
}
if(rchild.max_sub > root.max_sub)
{
root.max_sub = rchild.max_sub;
root.sub_l = rchild.sub_l;
root.sub_r = rchild.sub_r;
}
}
void build(int root,int l,int r)
{
node[root].l=l;
node[root].r=r;
if(l==r)
{
node[root].sum=a[l];
node[root].max_sub=a[l]; node[root].sub_l=l; node[root].sub_r=l;
node[root].max_pre=a[l]; node[root].pre_i=l;
node[root].max_suf=a[l]; node[root].suf_i=l;
}
else
{
int mid=l+(r-l)/;
build(root*,l,mid);
build(root*+,mid+,r);
pushup(node[root],node[root*],node[root*+]);
}
}
Node query(int root,int st,int ed)
{
if(st==node[root].l && node[root].r==ed) return node[root]; int mid=(node[root].l+node[root].r)/;
if(ed<=mid) return query(root*,st,ed);
else if(st>mid) return query(root*+,st,ed);
else
{
Node r1=query(root*,st,mid);
Node r2=query(root*+,mid+,ed);
Node ans; ans.l = st, ans.r=ed;
pushup(ans,r1,r2); return ans;
}
}
int main()
{
int kase=;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++) scanf("%d",&a[i]);
build(,,n);
printf("Case %d:\n",++kase);
for(int i=,l,r;i<=m;i++)
{
scanf("%d%d",&l,&r);
Node ans=query(,l,r);
printf("%d %d\n",ans.sub_l,ans.sub_r);
}
}
}

PS.这道题可以说要对普通线段树模板进行巨大的改动,是一道可以加深对线段树理解的好题。

UVALive 3938 - "Ray, Pass me the dishes!" - [最大连续子列和+线段树]的更多相关文章

  1. uvalive 3938 "Ray, Pass me the dishes!" 线段树 区间合并

    题意:求q次询问的静态区间连续最大和起始位置和终止位置 输出字典序最小的解. 思路:刘汝佳白书 每个节点维护三个值 pre, sub, suf 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的 ...

  2. UVALive 3938 Ray, Pass me the dishes! (动态最大连续和)

    题意:求一个动态区间的最大连续和. 静态版本的O(n)算法显示不适用了,但是可以用线段树分治,因为一个连续和要在两边的区间,要么跨越两边,对于一个结点维护最大前缀和,后缀和,子区间连续和. 题目要求输 ...

  3. UvaLA 3938 "Ray, Pass me the dishes!"

                            "Ray, Pass me the dishes!" Time Limit: 3000MS   Memory Limit: Unkn ...

  4. UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)

    "Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...

  5. UVA 1400 1400 - &quot;Ray, Pass me the dishes!&quot;(线段树)

    UVA 1400 - "Ray, Pass me the dishes!" option=com_onlinejudge&Itemid=8&page=show_pr ...

  6. 【LA3938】"Ray, Pass me the dishes!"

    原题链接 Description After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hun ...

  7. UVALive - 3938:"Ray, Pass me the dishes!"

    优美的线段树 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring& ...

  8. 线段树(区间合并) LA 3989 "Ray, Pass me the dishes!"

    题目传送门 题意:动态最大连续子序列和,静态的题目 分析:nlogn的归并思想.线段树维护结点的三个信息,最大前缀和,最大后缀和,该区间的最大和的两个端点,然后答案是三个的better.书上用pair ...

  9. UVa 1400 (线段树) "Ray, Pass me the dishes!"

    求一个区间的最大连续子序列,基本想法就是分治,这段子序列可能在区间的左半边,也可能在区间的右半边,也有可能是横跨区间中点,这样就是左子区间的最大后缀加上右子区间的最大前缀之和. 线段树维护三个信息:区 ...

随机推荐

  1. 2、一、Introduction(入门):1、Application Fundamentals(应用程序基础)

    一.Introduction(入门) 1.Application Fundamentals(应用程序基础) Android apps are written in the Java programmi ...

  2. Dubbo -- 系统学习 笔记 -- 配置参考手册

    Dubbo -- 系统学习 笔记 -- 目录 配置参考手册 <dubbo:service/> <dubbo:reference/> <dubbo:protocol/> ...

  3. wcf中的使用全双工通信(转)

    wcf中的使用全双工通信   wcf中的契约通信默认是请求恢复的方式,当客户端发出请求后,一直到服务端回复时,才可以继续执行下面的代码. 除了使用请求应答方式的通信外,还可以使用全双工.下面给出例子: ...

  4. 【GIS】ArcGIS JS 4.X

    require(["esri/Map", "esri/views/SceneView", "esri/TileLayer/TdtMapLayer/Td ...

  5. java的对象锁和类锁

    在java编程中,经常需要用到同步,而用得最多的也许是synchronized关键字了,下面看看这个关键字的用法. 因为synchronized关键字涉及到锁的概念,所以先来了解一些相关的锁知识. j ...

  6. Robot Framework进行web ui自动化测试,浏览器配置说明

    转载请注明出处,谢谢: chrome浏览器: 1.从如下地址下载与本地浏览器版本号一致的chromedriver.exe驱动文件: http://chromedriver.storage.google ...

  7. Install VMware Workstation as a Service

    Under default conditions, VMware Workstation does not support the ability to run virtual machines as ...

  8. iOS开发-VFL初窥

    VFL是苹果为了简化Autolayout的编码而推出的抽象语言,在上一篇博客中我们发现如果使用NSLayoutConstraint来添加约束是非常繁琐的. 一个简单的Frame需要添加四个NSLayo ...

  9. Memcached 运行状态

    memcached-tool 命令用于查看 Memcached 运行状态,用法如下: Usage: memcached-tool <host[:port] | /path/to/socket&g ...

  10. [PyCharm] 设置自动启动时自动打开项目

    设置启动PyCharm时自动打开(或不打开)上次进行的项目: 选择 “Settings - General - Reopen last project on startup”,勾选该选项则启动时自动打 ...