Description

You are given a sequence A[1], 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

动态最大子段和
维护区间和sum,最大连续和mx,最大前缀和pre,最大后缀和suf
用了三个查询,子段,前缀和后缀
考虑最大的起点和终点位置然后更新
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define m ((l+r)>>1)
#define lson o<<1,l,m
#define rson o<<1|1,m+1,r
#define lc o<<1
#define rc o<<1|1
using namespace std;
typedef long long ll;
const int N=5e5+,INF=2e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,q,x,y;
struct node{
int sum,mx,pre,suf;
}t[N<<];
void merge(int o){
t[o].sum=t[lc].sum+t[rc].sum;
t[o].mx=max(t[lc].suf+t[rc].pre,max(t[lc].mx,t[rc].mx));
t[o].pre=max(t[lc].pre,t[lc].sum+t[rc].pre);
t[o].suf=max(t[rc].suf,t[rc].sum+t[lc].suf);
}
void build(int o,int l,int r){
if(l==r) t[o].sum=t[o].mx=t[o].pre=t[o].suf=read();
else{
build(lson);
build(rson);
merge(o);
}
}
int qpre(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return t[o].pre;
else if(qr<=m) return qpre(lson,ql,qr);
else return max(qpre(lson,ql,qr),t[lc].sum+qpre(rson,ql,qr));
}
int qsuf(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return t[o].suf;
else if(m<ql) return qsuf(rson,ql,qr);
else return max(t[rc].suf,t[rc].sum+qsuf(lson,ql,qr));
}
int qmx(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return t[o].mx;
else{
int ans=-INF;
if(ql<=m) ans=max(ans,qmx(lson,ql,qr));
if(m<qr) ans=max(ans,qmx(rson,ql,qr));
if(ql<=m&&m<qr) ans=max(ans,qsuf(lson,ql,qr)+qpre(rson,ql,qr));
return ans;
}
} int main(){
n=read();
build(,,n);
q=read();
for(int i=;i<=q;i++){
x=read();y=read();
printf("%d\n",qmx(,,n,x,y));
}
}
 

SPOJ GSS1 Can you answer these queries I[线段树]的更多相关文章

  1. 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]| ≤ ...

  2. SPOJ GSS1 Can you answer these queries I ——线段树

    [题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...

  3. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  4. GSS5 spoj 2916. Can you answer these queries V 线段树

    gss5 Can you answer these queries V 给出数列a1...an,询问时给出: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[ ...

  5. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  6. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  7. SPOJ 2916 Can you answer these queries V(线段树-分类讨论)

    题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...

  8. SPOJ GSS3 Can you answer these queries III ——线段树

    [题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...

  9. SPOJ GSS5 Can you answer these queries V ——线段树

    [题目分析] GSS1上增加区间左右端点的限制. 直接分类讨论就好了. [代码] #include <cstdio> #include <cstring> #include & ...

随机推荐

  1. Ehcache 缓存使用

    在开发高并发量,高性能的网站应用系统时,缓存Cache起到了非常重要的作用.本文主要介绍EHCache的使用,以及使用EHCache的实践经验. 笔者使用过多种基于Java的开源Cache组件,其中包 ...

  2. linux 共享内存 shmat,shmget,shmdt,shmctl

    shmget int shmget(key_t key, size_t size, int flag);//开辟一段共享内存 key_t key :标识符的规则() size_t size :共享内存 ...

  3. 使用jenkins配置.net mvc网站进行持续集成一

    最近好久没有更新文章了,因为好久没有写代码了,以至于我不知道同大家分享些什么,刚好,今天突然叫我学习下jenkins每日构建,我就把今天的学习笔记记录下来,这其中很多东西都是公司同事之前调研总结的,我 ...

  4. CSS中越界问题经典解决方案

    8.CSS相关知识 (1)如何解决父元素的第一个子元素的margin-top越界问题 1)为父元素加border-top: 1px;——有副作用 2)为父元素指定padding-top: 1px;—— ...

  5. sass初级语法

    github地址:https://github.com/lily1010/sass/tree/master/course01 用到的sass语法是: sass --watch test.scss:te ...

  6. 有一个小效果而引出的appendTo()函数。

    在公司做项目的时候,始终不了解信息逐条向上滚的动画效果,后来查阅相关资料,终于明白了,在这个过程中,让我对appendTo这个函数有了一个全新的认识.话不多说,首先是demo: <!DOCTYP ...

  7. ArcGIS JS 学习笔记4 实现地图联动

    1.开篇 守望屁股实在太好玩了,所以最近有点懒,这次就先写个简单的来凑一下数.这次我的模仿目标是天地图的地图联动. 天地的地图联动不仅地图有联动,而且鼠标也有联动,我就照着这个目标进行山寨. 2.准备 ...

  8. iOS - Json解析精度丢失处理(NSString, Double, Float)

    开发中处理处理价格金额问题, 后台经常返回float类型, 打印或转成NSString都会有精度丢失问题, 因此使用系统自带的NSDecimalNumber做处理, 能解决这问题:经过测试其实系统NS ...

  9. 获取本机IP地址

    这里有两种方法: //获取本机IP - (NSString *)localIPAddress { NSString *localIP = nil; struct ifaddrs *addrs; ) { ...

  10. Echarts xAxis boundaryGap

    Echarts  xAxis----->boundaryGap: false 坐标轴两边留白策略,类目轴和非类目轴的设置和表现不一样. 类目轴中 boundaryGap 可以配置为 true 和 ...