SPOJ 1043 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查询的结果,每一行一个查询。
题解
查询区间内的最大子段和,我们用线段树来维护,lx表示从左往右最大的,
rx表示从右往左最大的,mx表示区间最大,用结构体更加方便
代码
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
inline int rd(){
int x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
struct Node{
int sum,rx,lx,mx;
}node[MAXN*2];
int n,m,a[MAXN];
inline void pushup(int x){
node[x].sum=node[x<<1].sum+node[x<<1|1].sum;
node[x].lx=max(node[x<<1].lx,node[x<<1].sum+node[x<<1|1].lx);
node[x].rx=max(node[x<<1|1].rx,node[x<<1|1].sum+node[x<<1].rx);
node[x].mx=max(node[x<<1].rx+node[x<<1|1].lx,
max(node[x<<1].mx,node[x<<1|1].mx));
}
inline void build(int x,int l,int r){
if(l==r){
node[x].rx=node[x].lx=node[x].mx=a[l];
node[x].sum=a[l];
return;
}
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
pushup(x);
}
inline Node query(int x,int L,int R,int l,int r){
if(l<=L && R<=r) return node[x];
int mid=(L+R)>>1;
if(mid<l) return query(x<<1|1,mid+1,R,l,r);
if(mid>=r) return query(x<<1,L,mid,l,r);
else{
Node ans,a,b;
a=query(x<<1,L,mid,l,r);b=query(x<<1|1,mid+1,R,l,r);
ans.sum=a.sum+b.sum;
ans.mx=max(a.mx,a.rx+b.lx),ans.mx=max(ans.mx,b.mx);
ans.lx=max(a.lx,a.sum+b.lx);
ans.rx=max(b.rx,b.sum+a.rx);
return ans;
}
}
int main(){
n=rd();
for(register int i=1;i<=n;i++) a[i]=rd();
build(1,1,n);
m=rd();
while(m--){
int l,r;
l=rd();r=rd();
printf("%d\n",query(1,1,n,l,r).mx);
}
return 0;
}
SPOJ 1043 GSS1 - Can you answer these queries I的更多相关文章
- [题解] 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
SP1043 GSS1 - Can you answer these queries I 题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义 ...
- 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]| ≤ ...
- 「 SPOJ GSS3 」 Can you answer these queries III
# 题目大意 GSS3 - Can you answer these queries III 需要你维护一种数据结构,支持两种操作: 单点修改 求一个区间的最大子段和 # 解题思路 一个区间的最大子段 ...
- 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
Time Limit: 115MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- SPOJ GSS1 Can you answer these queries I ——线段树
[题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...
- 题解【SP1043】 GSS1 - Can you answer these queries I
题目描述 You are given a sequence \(A_1, A_2, ..., A_n(|A_i|≤15007,1≤N≤50000)\). A query is defined as f ...
- SPOJ 2916 GSS5 - Can you answer these queries V
传送门 解题思路 和GSS1相似,但需要巨恶心的分类讨论,对于x1<=y1< x2< =y2 这种情况 , 最大值应该取[x1,y1]的右端最大+[y1+1,x2-1]的和+[x2, ...
随机推荐
- element-ui的layout将24等分换为48等分
按住ctr箭点击element-ui/packages/theme-chalk/src/index";,再按住ctr贱点击col.scss跳转,将跳转到的col.scss中的24换为48(c ...
- python3 enum模块
枚举是绑定到唯一的常量值的一组符号名称(成员).在枚举中,成员可以通过身份进行比较,枚举本身可以迭代. 1.Enum模块 该模块定义了四个枚举类,可用于定义唯一的名称和值集:Enum,IntEnum, ...
- day04 - 02 linux简单的操作命令
man ls:查看ls的帮助文档 ls --help:查看ls的帮助文档,简单查看 help cd: 查看内置命令(man)不可以查看内置命令 touch [filename]:创建一个文件 pwd: ...
- 几个 GetHashCode 函数
几个 GetHashCode 函数: DBTables.pas Delphi/Pascal code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- POJ 1696 /// 凸包
题目大意: 不能向左拐 不能重复走 就是求一个螺旋凸包 把已经是凸包内的点标记一下就行 因为凸包的性质 所有点都能走到 注意起点的选择 还有 反复求凸包的过程中边界的改变 #include <c ...
- USACO 2008 November Gold Cheering up the Cows /// MST oj24381
题目大意: 输入n,p:n个点,p条路 接下来n行输入c[]:在各个点需要花费的时间 接下来p行输入u,v,w:u点到v点的路需要花费时间w 求经过所有点且最后回到起点的最少花费时间 https:// ...
- VMware Workstation 10 配置Ubuntu环境
分享到 一键分享 QQ空间 新浪微博 百度云收藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ好友 和讯微博 更多... 百度分享 VMware Work ...
- 大型SQL文件导入mysql方案
一. 场景 现有俩个体积较大的单表sql文件,一个为8G,一个为4G,要在一天内完整导入到阿里云的mysql中,需要同时蛮子时间和空间这俩种要求. 二. 思路 搜索了网上一堆的方案,总结了如下几个: ...
- java自定义注解代码练习
/** * 自定义注解:校验非空字段 * */ @Documented @Inherited // 接口.类.枚举.注解 @Target(ElementType.FIELD) //只是在运行时通过反射 ...
- Linux-c对一个十六进制数的某一位取反
enum SWITCH_FLAG { SWITCH_ALL_FLAG = , SWITCH_WEB_FLAG = , …… } unsigned int switch_by_bit_value = 0 ...