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 ...
随机推荐
- (二)XML基础(2)
三.解析 服务端解析 JDK: DOM SAX JAXB java and xml Binding 开源(一般都是用开源的) ...
- 【原创】大叔经验分享(87)marathon重启应用过程服务不可用
marathon提供多种健康检查方式 常用的有TCP和HTTP, TCP检查端口是否存在,存在则认为实例健康: HTTP检查指定URL的HTTP返回码,返回码正常(2xx.3xx)则认为实例健康: 这 ...
- SSL Virtual Servers
SSL Virtual Servers 来源 https://www.carlstalhood.com/ssl-virtual-servers-netscaler-12/ SSL Virtual Se ...
- @PostConstruct注解原理解析
所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 正文 @PostConstruct注解使用简介 在了解一个东西的原理之前,我们得初步的懂得如何 ...
- 现有项目springmvc 小结
1. 接口接收json数据 @RequestBody JSONObject param 2.返回json数据封装 DataPacket.jsonResult
- iOS有哪些数据类型/基本数据类型?
简述 本文主要探究使用OC作为iOS开发语言时,我们能使用哪些数据类型. 一切类型始于C. C语言的类型 基本数据类型: 基本数据类型(fundamental data types)也叫原始数据类型( ...
- 程序员与数据库打交道的JDBC知识概要
1.JDBC全称:Java database connectivity,Java数据库连接. (1) JDBC是一种用于执行SQL语句的Java API,为多种关系数据库提供多种统 ...
- Flutter——AppBar组件(顶部导航组件)
AppBar组件的常用属性如下: 属性 描述 leading 在标题前面显示的一个控件,在首页通常显示应用的 logo:在其他界面通常显示为返回按钮 title 标题,通常显示为当前界面的标题文字,可 ...
- Django基础目录
Django基础(一):基础引用 Django基础(二):环境配置 Django—model系统:ORM基础 Django—model系统:ORM字段和参数 Django—model系统:ORM对数据 ...
- 搭建KVM环境——06 创建虚拟机
若转载请于明显处标明出处:https://www.cnblogs.com/kelamoyujuzhen/p/9071181.html 虚拟机磁盘格式 创建一个raw格式的虚拟机磁盘 [root@Cen ...