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