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]| ≤ 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
/*
一开始W.
不知道为啥.
拍了好多组数据都OK.
原来case更新的时候错了.
考虑三种情况.
分别维护GSS,LGSS,RGSS.
分为两种形态:跨区间和不跨区间.
case 1,2:左右段的GSS.
case 3:左段右端与右段左端的GSS和.
一开始更新的时候更新成了该段的左端GSS 右端GSS case3.
画了画图不对吖.
如果跨区间的话这两种情况是包含在case3里边的.
然后这样就忽略了case1,2.
*/
#include<iostream>
#include<cstdio>
#define MAXN 50001
using namespace std;
int n,m,cut;
struct data{
int l,r,lg,rg,g,sum,size;
data *lc,*rc;
}tree[MAXN*4];
int read()
{
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*10+ch-48,ch=getchar();
return x*f;
}
void build(data *k,int l,int r,int now)
{
k->l=l,k->r=r;
if(l==r) {k->g=k->lg=k->rg=k->sum=read();return ;}
int mid=(l+r)>>1;
k->size=now;
k->lc=&tree[now*2];
k->rc=&tree[now*2+1];
k->lc->size=now*2;
k->rc->size=now*2+1;
build(k->lc,l,mid,now*2);
build(k->rc,mid+1,r,now*2+1);
k->lg=max(k->lc->lg,k->lc->sum+k->rc->lg);
k->rg=max(k->rc->rg,k->rc->sum+k->lc->rg);
k->sum=k->lc->sum+k->rc->sum;
k->g=max(k->lc->g,max(k->lc->rg+k->rc->lg,k->rc->g));
return ;
}
data query(data *k,int l,int r,int num)
{
data xx;
if(l<=k->l&&k->r<=r) return tree[num];
int mid=(k->l+k->r)>>1;
if(l>mid) return query(k->rc,l,r,k->rc->size);
else if(r<=mid) return query(k->lc,l,r,k->lc->size);
else {
data ll=query(k->lc,l,mid,k->lc->size);
data rr=query(k->rc,mid+1,r,k->rc->size);
xx.sum=ll.sum+rr.sum;
xx.lg=max(ll.lg,ll.sum+rr.lg);
xx.rg=max(rr.rg,rr.sum+ll.rg);
xx.g=max(ll.g,max(rr.g,ll.rg+rr.lg));
}
return xx;
}
int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int x,y;
n=read();
build(tree+1,1,n,1);
m=read();
while(m--)
{
x=read(),y=read();
data xx=query(tree+1,x,y,1);
printf("%d\n",xx.g);
}
return 0;
}
SPOJ GSS1 - Can you answer these queries I(线段树维护GSS)的更多相关文章
- 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 ...
- 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[ ...
- 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 ...
- 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 ...
- SPOJ - GSS1-Can you answer these queries I 线段树维护区间连续和最大值
SPOJ - GSS1:https://vjudge.net/problem/SPOJ-GSS1 参考:http://www.cnblogs.com/shanyr/p/5710152.html?utm ...
- SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
随机推荐
- hdu 6208 上一个kmp模板
#include <cstdio> #include <cstring> #include <iostream> #include <queue> #i ...
- 入手线段树 hdu1754
今天学习了线段树的三个基本操作 建树 更新 查找 先理解下什么是线段树就这个题目而言 如果我们用普通的数组去存放 然后依次遍历访问的话 时间太多了线段树利用了二分的思想 把数据以段的形式进行储存 这样 ...
- jvm垃圾回收器介绍
上篇文章中我们讨论了jvm的内存区域,这篇文章我们来讨论针对的内存区域的垃圾回收机制. 其实针对垃圾回收我们通常考虑三个问题:1.哪些内存需要回收?2.什么时候回收?3.如何回收?下面我们针对这三个问 ...
- 写给Web开发人员看的Nginx介绍
译者注:不知道其他开发者是否和我一样,参与或者写了很多Web项目,但是却没有真正的去完整的部署应用,很多时候都是交给ops即运维的同学帮忙来做.而作为一个有节操的开发者,我认为了解一些服务器方面的知识 ...
- C# Unix时间戳和DateTime类型相互转换
/// <summary> /// 将Unix时间戳转换为DateTime类型时间 /// </summary> /// <param name="d" ...
- [Vuex系列] - 初尝Vuex第一个例子
Vuex是什么? Vuex是一个专为vue.js应用程序开发的状态管理库.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 通过定义和隔离状态管理中的各种概 ...
- SVN配置使用及移植
使用svn作为配置管理工具及其普遍的用于项目开发中,网上有很多关于svn的原理介绍及命令行管理教程.这里仅仅分享下个人配置及使用的过程,不通过命令行,可简单的上手操作.如有遗漏欢迎留言交流. 配置及使 ...
- # URL异常检测
(Isolation Forest无监督)这个算法是随机森林的推广. iTree树构造:随机选一个属性,再随机选该特征的一个值,对样本进行二叉划分,重复以上操作. iTree构建好了后,就可以对数据进 ...
- char str = '1.2.';问题
偶然看到群里老哥问道这个问题 #include <iostream> using namespace std; int main() { char str = '1.2.'; ; } 什么 ...
- 【Hibernate】 二级缓存及查询缓存
一.Hibernate的二级缓存 1.1 类缓存区特点 缓存的是对象的散装的数据. 图一 Hibernate的二级缓存的散装数据 1.2 集合缓存区的特点: 缓存的是对象的id.需要依赖类缓冲区的配置 ...