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个查询,程序必须输出这些查询的结果。
输入输出格式
输入格式:
- 输入文件的第一行包含整数N。
- 在第二行,N个数字跟随。
- 第三行包含整数M。
- M行跟在后面,其中第1行包含两个数字xi和yi。
输出格式:
您的程序应该输出M查询的结果,每一行一个查询。
思路:
我们做这道题首先应该想的,是两个区间如何合并
很显然,合并后最大子段和,要么是原来左儿子的,要么是原来右儿子的
还有一种可能是左右两个儿子合并后在中间新生成的
所以,每个节点维护四个元素
分别表示他所管辖的区间和,他所管辖的区间中的最大连续子段和
从左起最大连续子段和(意思是子段的左端点一定是区间的左端点)
从右起最大连续子段和(意思同上)
此时我们可以这样转移:
sum(和)就不用说了
这一层的从左起最大连续子段和=max(左儿子的从左起最大连续子段和,左儿子的和+右儿子的从左起最大连续子段和)
这一层的从左起最大连续子段和方法同上
这一层的最大连续子段和=max(左儿子的最大连续子段和,右儿子的最大连续子段和,(左儿子从右起的最大连续字段和+右儿子从左起的最大连续子段和(也就是合并后生成的中间最大子段)))
OK,任务完成
查询同理
代码:
//这里我将建树当成更改节点值
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define rii register int i
#define rij register int j
#define inf 1073741824
#define rs 65536
using namespace std;
struct nod{
int lm,rm,maxn,sum;
}x[];
int n,q,cz,x1,y1;
void add(int wz,int l,int r,int val,int bh)
{
if(l==r&&l==wz)
{
x[bh].maxn=val;
x[bh].lm=val;
x[bh].rm=val;
x[bh].sum=val;
return;
}
int ltt=(l+r)/;
if(wz<=ltt)
{
add(wz,l,ltt,val,bh*);
}
else
{
add(wz,ltt+,r,val,bh*+);
}
x[bh].sum=x[bh*].sum+x[bh*+].sum;
x[bh].lm=max(x[bh*].lm,x[bh*].sum+x[bh*+].lm);
x[bh].rm=max(x[bh*+].rm,x[bh*+].sum+x[bh*].rm);
x[bh].maxn=max(x[bh*].maxn,max(x[bh*+].maxn,x[bh*].rm+x[bh*+].lm));
}
nod query(int l,int r,int nl,int nr,int bh)
{
nod an,bn;
if(l<nl)
{
l=nl;
}
if(r>nr)
{
r=nr;
}
if(nl==l&&nr==r)
{
an=x[bh];
return an;
}
int ltt=(nl+nr)/;
if(l<=ltt&&r<=ltt)
{
return an=query(l,r,nl,ltt,bh*);
}
if(r>ltt&&l>ltt)
{
return bn=query(l,r,ltt+,nr,bh*+);
}
else
{
an=query(l,r,nl,ltt,bh*);
bn=query(l,r,ltt+,nr,bh*+);
an.maxn=max(an.maxn,max(bn.maxn,an.rm+bn.lm));
an.lm=max(an.lm,an.sum+bn.lm);
an.rm=max(bn.rm,bn.sum+an.rm);
an.sum=an.sum+bn.sum;
return an;
} }
int main()
{
// freopen("brs.in","r",stdin);
// freopen("brs.out","w",stdout);
for(rii=;i<=;i++)
{
x[i].lm=-inf;
x[i].rm=-inf;
x[i].maxn=-inf;
}
scanf("%d",&n);
for(rii=;i<=n;i++)
{
int ltt;
scanf("%d",<t);
add(i,,rs,ltt,);
}
scanf("%d",&q);
for(rii=;i<=q;i++)
{
scanf("%d%d",&x1,&y1);
nod ans=query(x1,y1,,rs,);
printf("%d\n",ans.maxn);
}
}
SP1043 GSS1 - Can you answer these queries I(线段树,区间最大子段和(静态))的更多相关文章
- SP1043 GSS1 - Can you answer these queries I 线段树
问题描述 LG-SP1043 题解 GSS 系列第一题. \(q\) 个询问,求 \([x,y]\) 的最大字段和. 线段树,维护 \([x,y]\) 的 \(lmax,rmax,sum,val\) ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
- 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]| ≤ ...
- 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?(线段树区间开方)
Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K ...
- SPOJ GSS1 Can you answer these queries I[线段树]
Description You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A q ...
- SPOJ GSS1 Can you answer these queries I ——线段树
[题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...
- HDU - 4027 Can you answer these queries?(线段树区间修改)
https://cn.vjudge.net/problem/HDU-4027 题意 给一个有初始值的数组,存在两种操作,T=0时将[L,R]的值求平方根,T=1时查询[L,R]的和. 分析 显然不符合 ...
- 线段树 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).查询定义 ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
随机推荐
- C# ADO.NET 面向对象
ADO.NET跟面向对象的结合 把面向对象跟数据库连接用 在项目里面创建一个新的文件夹 名字为App_Code 在这个App_Code里面创建几个类 主要为拆分问题,标上序号,先干什么在干什么 实 ...
- 弹出框layer插件
有时候我们在网页制作中需要引用各种弹出框,弹出框的展现形式多种多样.可以是弹出图片,视频,文字,也可以是弹出图片轮播等形式: 弹出框插件——layer使用方法(其实官方文档中已经介绍的很详细): 下载 ...
- scss-@media
首先回顾下css3中的@media 定义和使用: 使用 @media 查询,你可以针对不同的媒体类型定义不同的样式. @media 可以针对不同的屏幕尺寸设置不同的样式,特别是如果你需要设置设计响应式 ...
- alter table fx.pet modify column `species` varchar(20) binary;
alter table fx.pet modify column `species` varchar(20) binary;
- 使用 yield生成迭代对象函数
https://www.cnblogs.com/python-life/articles/4549996.html https://www.liaoxuefeng.com/wiki/001431608 ...
- solidity语言1
合约(contract)由变量(variable).函数(functions).函数修饰符(function modifiers).事件(events).结构体类型(struct type).枚举类型 ...
- 【Leetcode】【Easy】Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 使用sqlloader向oracle导入文本数据
文本文件如下,注意文件名必须有后缀,文本行首也需要|分隔符:[oracle@ycr test]$ more person.txt|aaa|123|m|aaa|123|m|aaa|123|m|aaa|1 ...
- MySQL30条规范解读
转载自:https://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=2651959906&idx=1&sn=2cbdc66cfb ...
- Android(java)学习笔记25:Android 手机拨号
1. 手机拨号程序:(只有程序代码) package cn.itcast.phone; import android.app.Activity; import android.content.Inte ...