线段树 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)。查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j];x≤i≤j≤y}。 给定M个查询,程序必须输出这些查询的结果。
输入输出格式
输入格式:
- 输入文件的第一行包含整数N。
- 在第二行,N个数字跟随。
- 第三行包含整数M。
- M行跟在后面,其中第1行包含两个数字xi和yi。
输出格式:
您的程序应该输出M查询的结果,每一行一个查询。
不带修改的维护最大子段和,挺裸的,维护四个量就行了。具体参考小白逛公园https://www.cnblogs.com/wangxiaodai/p/9744081.html
code:
#include<iostream>
#include<cstdio>
#define ls(o) o<<1
#define rs(o) o<<1|1
using namespace std;
const int wx=100017;
inline int read(){
int sum=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0';ch=getchar();}
return sum*f;
}
struct val_tree{
int l,r,sum,lsum,rsum,tot;
#define sum(o) t[o].sum
#define lsum(o) t[o].lsum
#define rsum(o) t[o].rsum
#define tot(o) t[o].tot
}t[wx*4];
int n,m;
int a[wx];
void up(int o){
tot(o)=tot(ls(o))+tot(rs(o));
lsum(o)=max(lsum(ls(o)),tot(ls(o))+lsum(rs(o)));
rsum(o)=max(rsum(rs(o)),tot(rs(o))+rsum(ls(o)));
sum(o)=max(sum(ls(o)),max(sum(rs(o)),rsum(ls(o))+lsum(rs(o))));
}
void build(int o,int l,int r){
t[o].l=l;t[o].r=r;
if(l==r){sum(o)=tot(o)=lsum(o)=rsum(o)=a[l];return;}
int mid=t[o].l+t[o].r>>1;
if(l<=mid)build(ls(o),l,mid);
if(r>mid)build(rs(o),mid+1,r);
up(o);
}
val_tree query(int o,int l,int r){
if(l<=t[o].l&&t[o].r<=r){
return t[o];
}
int mid=t[o].l+t[o].r>>1;
val_tree tmp,tmp1,tmp2;
if(r<=mid)return query(ls(o),l,r);
if(l>mid)return query(rs(o),l,r);
tmp1=query(ls(o),l,r);
tmp2=query(rs(o),l,r);
tmp.tot=tmp1.tot+tmp2.tot;
tmp.lsum=max(tmp1.lsum,tmp1.tot+tmp2.lsum);
tmp.rsum=max(tmp2.rsum,tmp2.tot+tmp1.rsum);
tmp.sum=max(max(tmp1.sum,tmp2.sum),tmp1.rsum+tmp2.lsum);
return tmp;
}
int main(){
n=read();
for(int i=1;i<=n;i++)a[i]=read();
build(1,1,n);
m=read();
for(int i=1;i<=m;i++){
int x,y;
x=read();y=read();
printf("%d\n",query(1,x,y).sum);
}
return 0;
}
线段树 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\) ...
- 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 ...
- [SP1043] GSS1 - Can you answer these queries I
传送门:>Here< 题意:求区间最大子段和 $N \leq 50000$ 包括多组询问(不需要支持修改) 解题思路 线段树的一道好题 我们可以考虑,如果一组数据全部都是正数,那么问题等同 ...
- 线段树 SP1716 GSS3 - Can you answer these queries III
SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] ...
- 线段树 SP2713 GSS4 - Can you answer these queries IV暨 【洛谷P4145】 上帝造题的七分钟2 / 花神游历各国
SP2713 GSS4 - Can you answer these queries IV 「题意」: n 个数,每个数在\(10^{18}\) 范围内. 现在有「两种」操作 0 x y把区间\([x ...
- 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} ...
- [题解] SPOJ GSS1 - Can you answer these queries I
[题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...
- 线段树【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 ...
- GSS1 - Can you answer these queries I(线段树)
前言 线段树菜鸡报告,stO ZCDHJ Orz,GSS基本上都切完了. Solution 考虑一下用线段树维护一段区间左边连续的Max,右边的连续Max,中间的连续Max还有总和,发现这些东西可以相 ...
随机推荐
- PowerDesigner中CDM和PDM如何定义外键关系
有A.B两张表(实体),各自有id作为主键,两表是一一对应关系.但略有不同: A表一条记录可以对应0或1条B表记录,B表一条记录必须对应唯一条A表记录. 这样的关系如何在CDM或PDM中定义? 在最后 ...
- springmvc下的省市县三级联动
转自:https://blog.csdn.net/rentian1/article/details/77662635
- Java 对密码MD5
MD5,即"Message-Digest Algorithm 5(信息-摘要算法)": MD5将任意长度的“字节串”变换成一个128bit的大整数,并且它是一个不可逆的字符串变换算 ...
- EF事务封装
public class EFTransaction:ITransaction { DbContextTransaction originalTransaction = null; MyDbConte ...
- LookupError: unknown encoding: cp65001解决办法
一.之前手上做的一个web项目,漏洞频发,服务器用的是菜鸟云服务器,那个应急响应中心不错,想不到乌云倒了,白帽子竟然被阿里系养了,题外话了,首先感谢白帽子提的漏洞,同时也感慨自己安全知识,以及意识的薄 ...
- oracle -sqlplus 调用存储过程
sp.batsqlplus user/passwd/sid @.\sp.sql >sp.sqlexit; sp.sqlexex xxxx()quit;
- ie7下z-index失效问题解决方法
绝对定位元素的“有定位属性(relative或absolute)的父元素”在渲染层次时起到了主要作用,前面的被后面的覆盖了.解决办法就是给有定位属性的父元素设置z-index 解决办法: 父级元素加上 ...
- IDEA创建的Maven项目中 解决编写pom.xml没有提示
问题如下 没有提示信息 解决方案 把Repositories中的配置更新成本地仓库 问题解决
- 一行代码搞定所有屏幕适配AbViewUtil
适配原理:抛弃google提供的dip理论与多套图片与布局方案,采用与UI设计师通用的px作为标准单位,原理是将UI设计师的设计图与当前查看的手机或其他设备的屏幕像素尺寸进行换算,得到缩放比例,在Ac ...
- 【总结整理】IFeatureBuffer
IFeatureBuffer pRowBuffer = objTabWYDCQ_Tar.CreateFeatureBuffer(); pRowBuffer.Shape = SourceRow.Shap ...