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 ...
随机推荐
- 查看windows、linux的SN
gwmi win32_bios [root@live-al-ops-pxe-2 ~]# dmidecode | grep Number | sed -n '1p' Serial Number: ...
- 【实验手册】使用Visual Studio Code 开发.NET Core应用程序
.NET Core with Visual Studio Code 目录 概述... 2 先决条件... 2 练习1: 安装和配置.NET Core以及Visual Studio Code 扩展... ...
- iOS通用链接(Universal Links)突然点击无效的解决方案
接上文<微信中通过页面(H5)直接打开本地app的解决方案>已经把iOS搞定并且已经正常能跑了,突然就再也用不了了... 问题描述 测试告诉我,如果从微信打开App之后,点击App右上角的 ...
- Java8函数之旅 (八) - 组合式异步编程
前言 随着多核处理器的出现,如何轻松高效的进行异步编程变得愈发重要,我们看看在java8之前,使用java语言完成异步编程有哪些方案. JAVA8之前的异步编程 继承Thead类,重写run方法 实现 ...
- web网站嵌入QQ临时会话代码 ----转载----小技巧
第一种 <img style="CURSOR: pointer" onclick="javascript:window.open('tencent://messag ...
- lesson - 6 Linux下磁盘管理
1. 查看磁盘或者目录的容量df 查看磁盘各分区使用情况 不加参数以k为单位 df -i inode数,df -h 以G或者T或者M df -m 以M单位显示 du 查看目录或者文 ...
- tomcat部署项目时省略项目名
大家也许知道在eclipse上通过新建server来部署项目到tomcat,并且通过server来管理项目的启动配置.server会自动创建启动该项目的xml 如: <Context docBa ...
- Xamarin.android 重写axml控件
https://www.cnblogs.com/lonelyxmas/p/5632694.html <Laco: 用来用引指定的控件 android:layout_widt ...
- 房上的猫:switch选择结构,与选择结构总结
switch选择结构: 一.定义: switch选择结构,可以方便地解决等值判断问题二.语法: switch(表达式){ case 常量1: //代码块1; break; c ...
- PHP使用ueditor上传配置
引入 按照ueditor官网demo, 引入好ueditor之后, 默认是不能进行上传操作的 在上传时,在上传时会有如下图提示 配置上传 在editor/php目录下,有一个config.json文件 ...