Time Limit: 115MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu

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

Hint

Added by: Nguyen Dinh Tu
Date: 2006-11-01
Time limit: 0.115s-0.230s
Source limit: 5000B
Memory limit: 1536MB
Cluster: Cube (Intel G860)
Languages: All except: ERL JS NODEJS PERL 6 VB.net

询问区间内和最大的连续子序列。没有修改操作。

线段树维护即可。

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define lc rt<<1
#define rc rt<<1|1
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
int data[mxn];
struct node{
int mx;
int ml,mr;
int smm;
}t[mxn<<],tmp0;
void Build(int l,int r,int rt){
if(l==r){t[rt].mx=t[rt].ml=t[rt].mr=data[l];t[rt].smm=data[l];return;}
int mid=(l+r)>>;
Build(l,mid,lc);
Build(mid+,r,rc);
t[rt].smm=t[lc].smm+t[rc].smm;
t[rt].mx=max(t[lc].mx,t[rc].mx);
t[rt].mx=max(t[lc].mr+t[rc].ml,t[rt].mx);
t[rt].ml=max(t[lc].ml,t[lc].smm+t[rc].ml);
t[rt].mr=max(t[rc].mr,t[rc].smm+t[lc].mr);
return;
}
node query(int L,int R,int l,int r,int rt){
// printf("%d %d %d %d %d\n",L,R,l,r,rt);
if(L<=l && r<=R){return t[rt];}
int mid=(l+r)>>;
node res1;
if(L<=mid)res1=query(L,R,l,mid,lc);
else res1=tmp0;
node res2;
if(R>mid)res2=query(L,R,mid+,r,rc);
else res2=tmp0;
node res={};
res.smm=res1.smm+res2.smm;
res.mx=max(res1.mx,res2.mx);
res.mx=max(res.mx,res1.mr+res2.ml);
res.ml=max(res1.ml,res1.smm+res2.ml);
res.mr=max(res2.mr,res2.smm+res1.mr);
return res;
}
int main(){
n=read();
int i,j,x,y;
for(i=;i<=n;i++)data[i]=read();
Build(,n,);
m=read();
tmp0.ml=tmp0.mr=tmp0.mx=-1e9;tmp0.smm=;
for(i=;i<=m;i++){
x=read();y=read();
printf("%d\n",query(x,y,,n,).mx);
}
return ;
}

最新文章

  1. Ignite性能测试以及对redis的对比
  2. 用C#通过正则表达式截取字符串中符合条件的子字符串
  3. json转js对象
  4. AEM - Adobe CMS 扒坑记之始
  5. 【贪心+一点小思路】Zoj - 3829 Known Notation
  6. iOS实现地图半翻页效果--老代码备用参考
  7. POJ 1721 CARDS(置换群)
  8. VS2008查看dll导出函数
  9. 30分钟学习sea.js使用指南
  10. JS弹出框
  11. 搭建eclipse+tomcat开发环境
  12. 4.1ASP.NET Core请求过程「深入浅出ASP.NET Core系列」
  13. .Net 使用爬虫下载网络图片到本地磁盘
  14. Oracle安装配置
  15. Linux Buffer/Cache 的区别
  16. Github 开源项目(二) jsmpeg-vnc
  17. Python迭代器笔记
  18. kafka-2.11-0.11集群搭建
  19. 管理开机启动:systemd
  20. python传递参数给shell

热门文章

  1. c语言 指针与数组
  2. window.history 和 DWZ 框架
  3. Struts2、Spring MVC4 框架下的ajax统一异常处理
  4. addShutdownHook的用法
  5. Vue系列:如何将百度地图包装成Vue的组件
  6. Expression Blend4经验分享:制作一个简单的文字按钮样式
  7. 如何用 Nodejs 分析一个简单页面
  8. 使用docker发布spring cloud应用
  9. matlab如何建立一个空矩阵,然后往里面赋值
  10. jqMobile中pageinit,pagecreate,pageshow等函数的执行顺序