SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并)
标签(空格分隔): 线段树区间合并
题目链接
GSS1 - Can you answer these queries I
You are given a sequence A1, A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defined as follows:
Query(x,y) = Max { a[i]+a[i+1]+...+a[j] ; x ≤ i ≤ j ≤ y }.
Given M queries, your program must output the results of these queries.
Input
The first line of the input file contains the integer N.
In the second line, N numbers follow.
The third line contains the integer M.
M lines follow, where line i contains 2 numbers xi and yi.
Output
Your program should output the results of the M queries, one query per line.
Example
Input:
3
-1 2 3
1
1 2
Output:
2
题意:
求连续自区间最大和
题解:
线段树的区间合并可以解决有关连续子区间最值的问题,介绍一下,每一个节点都保存一个此区间的子区间最大值,那么在区间合并的时候就会遇到如何处理包含断点子区间的问题,我们用一个lv保存从左端点开始的连续子区间最值,用rv保存从到右端点结束的连续子区间最值,v表示整个区间的所有元素和,ans表示这个区间的连续子区间的最值。
那么有合并的时候v要考虑从左孩子的右端点结束加上右孩子的左端点开始,和左孩子和右孩子的ans最大值
lv要考虑的是左孩子的左端点开始或者是整个左孩子的所有值加上右孩子的从左端点开始的值
同样rv要考虑的是右孩子的右端点结束或者是整个右孩子的所有值加上左孩子的右端点结束的值
参考代码如下
void Push(int d){
st[d].v = st[lc].v+st[rc].v;
st[d].lv = max(st[lc].lv,st[lc].v+st[rc].lv);
st[d].rv = max(st[rc].rv,st[rc].v+st[lc].rv);
st[d].ans = max(max(st[lc].ans,st[rc].ans),st[lc].rv+st[rc].lv);
return;
}
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 50008;
#define mid (l+r>>1)
#define lc d<<1
#define rc d<<1|1
struct Tr{
int v,lv,rv,ans;
}st[N<<2];
void Push(int d){
st[d].v = st[lc].v+st[rc].v;
st[d].lv = max(st[lc].lv,st[lc].v+st[rc].lv);
st[d].rv = max(st[rc].rv,st[rc].v+st[lc].rv);
st[d].ans = max(max(st[lc].ans,st[rc].ans),st[lc].rv+st[rc].lv);
return;
}
void build(int l, int r, int d){
if(l==r){
scanf("%d",&st[d].ans);
st[d].v = st[d].lv = st[d].rv = st[d].ans;
return;
}
build(l,mid,lc);
build(mid+1,r,rc);
Push(d);
}
Tr query(int L, int R, int l, int r, int d)
{
if(l==L&&R==r){
return st[d];
}
else if(R<=mid) return query(L,R,l,mid,lc);
else if(L>mid) return query(L,R,mid+1,r,rc);
Tr la = query(L,mid,l,mid,lc);
Tr ra = query(mid+1,R,mid+1,r,rc);
Tr re;
re.v = la.v+ra.v;
re.lv = max(la.lv,la.v+ra.lv);
re.rv = max(ra.rv,ra.v+la.rv);
re.ans = max(max(ra.ans,la.ans),la.rv+ra.lv);
return re;
}
int main()
{
int n,m;
int ll,rr;
while(~scanf("%d",&n))
{
build(1,n,1);
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&ll,&rr);
Tr fn = query(ll,rr,1,n,1);
printf("%d\n",fn.ans);
}
}
return 0;
}
SPOJ GSS1_Can you answer these queries I(线段树区间合并)的更多相关文章
- SPOJ - GSS1-Can you answer these queries I 线段树维护区间连续和最大值
SPOJ - GSS1:https://vjudge.net/problem/SPOJ-GSS1 参考:http://www.cnblogs.com/shanyr/p/5710152.html?utm ...
- HDU 4027 Can you answer these queries?(线段树区间开方)
Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K ...
- HDU 4027—— Can you answer these queries?——————【线段树区间开方,区间求和】
Can you answer these queries? Time Limit:2000MS Memory Limit:65768KB 64bit IO Format:%I64d & ...
- HDU - 4027 Can you answer these queries?(线段树区间修改)
https://cn.vjudge.net/problem/HDU-4027 题意 给一个有初始值的数组,存在两种操作,T=0时将[L,R]的值求平方根,T=1时查询[L,R]的和. 分析 显然不符合 ...
- SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并
Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...
- hdu-3308 LCIS (线段树区间合并)
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 3308(线段树区间合并)
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 【bzoj3638】Cf172 k-Maximum Subsequence Sum 模拟费用流+线段树区间合并
题目描述 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交的不超过k个子段,最大的和是多少. 输入 The first line contains inte ...
- LCIS HDU - 3308 (线段树区间合并)
LCIS HDU - 3308 Given n integers. You have two operations: U A B: replace the Ath number by B. (inde ...
随机推荐
- 最全linux命令
arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS / DMI ...
- nova创建虚拟机源码系列分析之二 wsgi模型
openstack nova启动时首先通过命令行或者dashborad填写创建信息,然后通过restful api的方式调用openstack服务去创建虚拟机.数据信息从客户端到达openstack服 ...
- vbs的一些入门基础。。。
VBS(VBScript的进一步简写)是基于Visual Basic的脚本语言. Microsoft Visual Basic是微软公司出品的一套可视化编程工具, 语法基于Basic. 脚本语言, 就 ...
- ogg12c_静默安装
1.上传压缩包:123010_fbo_ggs_Linux_x64_shiphome.zip 2.解压: unzip 123010_fbo_ggs_Linux_x64_shiphome.zip 3.配置 ...
- css实现一行居中显示,两行靠左显示,超过两行以引号省略
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 安装两个JDK后配置环境变量没用?
在实际开发中,由于项目的需要,可能JDK的版本是不同的.比如我们前一个项目所需JDK版本是1.6的,项目完成后,下一个项目JDK版本又是需要1.7的,为了防止由于切换项目我们需要频繁的安装卸载JDK, ...
- http1.0 的哑代理问题
感觉这就和回字的四种写法一样,并无卵用 原以为http1.1的新特性是提供了keep-alive,后来才知道,keep-alive选项http1.1已经不支持了,http1.1对该功能进行了改版 关于 ...
- C#-判断Shift,Alt,Ctrl是否被按下,确定所按下的组合键
在创建接受用户击键的应用程序时,您还可能希望监视 SHIFT.ALT 和 CTRL 键等组合键.当一个组合键与其他键同时按下,或在单击鼠标的同时按下时,您的应用程序能够做出适当响应:字母 S 可能仅导 ...
- 如何用Visio画venn(维恩)图
今天需要换几个Venn(维恩)图,按照以前的套路是用画图工具来画的,但是这次不是画给自己看,并且也要很迅速的画好,那就迅速的来学习了. 参考网址:https://support.office.com/ ...
- 《Python cookbook》 “定义一个属性可由用户修改的装饰器” 笔记
看<Python cookbook>的时候,第9.5部分,"定义一个属性可由用户修改的装饰器",有个装饰器理解起来花了一些时间,做个笔记免得二刷这本书的时候忘了 完整代 ...