SPOJ GSS1 Can you answer these queries I
| 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 ;
}
SPOJ 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\) 次询问,每次 ...
- 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 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 ——线段树
[题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...
- 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 ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
- 线段树 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).查询定义 ...
- GSS3 SPOJ 1716. Can you answer these queries III gss1的变形
gss2调了一下午,至今还在wa... 我的做法是:对于询问按右区间排序,利用splay记录最右的位置.对于重复出现的,在splay中删掉之前出现的位置所在的节点,然后在splay中插入新的节点.对于 ...
- GSS1 spoj 1043 Can you answer these queries I 最大子段和
今天下午不知道要做什么,那就把gss系列的线段树刷一下吧. Can you answer these queries I 题目:给出一个数列,询问区间[l,r]的最大子段和 分析: 线段树简单区间操作 ...
随机推荐
- sudo su权限案例
一 控制sudo: 允许执行所有命令,排除某几个命令(带参数) lanny ALL=(ALL) NOPASSWD:ALL, !/bin/su - root, !/usr/sbin/visudo 如果需 ...
- Android设置按钮为透明
设置一个按钮为透明, (1)修改配置文件 <Button android:id="@+id/btnAppMore" android:layout_width=&quo ...
- font和lineheight冲突。
font:14px bold arial; line-height:40px; 这样写font的话line-height不会有效,只要把font拆分写就有效,chrome ie ff下都是.
- react native中的欢迎页(解决首加载白屏)
参照网页: http://blog.csdn.net/fengyuzhengfan/article/details/52712829 首先是在原生中写一些方法,然后通过react native中js去 ...
- checkbox页面全选
http://pan.baidu.com/s/1tfzSa
- 【分布式协调器】Paxos的工程实现-cocklebur简介(二)
Cocklebur集群的工作原理 在集群正常工作时,整个集群只会有一个Leader,其他都是Follower.Client可以注册到某个Follower,当然也可以注册到Leader,为了减轻Lead ...
- js 判断一组日期是否是连续的
文章同步自个人博客:http://www.52cik.com/2016/07/10/consecutive-dates.html 这是群里一朋友问的问题,当时我说判断下 day 是否相邻即可,后来细想 ...
- background-size对background-position的影响
CSS3中提出了background-size属性,该属性可以设置背景图片的大小,该属性的值设置为绝对数值或者百分比时对background-position没有任何影响,当设置为contain/co ...
- C#,JavaScript两种语言 2048小游戏
<html> <head> <style type="text/css"> .haha { border-width: 2; font-size ...
- 68 id -显示用户的id
Linux id命令用于显示用户的ID,以及所属群组的ID. id会显示用户以及所属群组的实际与有效ID.若两个ID相同,则仅显示实际ID.若仅指定用户名称,则显示目前用户的ID. 语法 id [-g ...