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 ...
随机推荐
- vue 中使用rem布局
在使用vue-cli搭建好项目框架后,在目录结构的index.html文件中添加一段js代码: fnResize(); window.onresize = function () { fnResize ...
- SpringCloud Hystrix 参数
hystrix.command.default和hystrix.threadpool.default中的default为默认CommandKey Command PropertiesExecution ...
- (十二)Activitivi5之流程控制网关:排他
一.概念 所谓排他网关 顾名思义 执行到该网关,根据条件只能走一条执行线 二.案例 当流程走到“学生请假”任务节点的时候, /** * 完成任务 */ @Test public void test_c ...
- [APB VNext 笔记] UI
一直想给我做的服务写UI.但苦于现在ABPVNext框架对SPA支持不好.只好先放弃VUE.先弄个UI在说.ABPVNext中的框架都是用Helper封装好的Label.不知道怎么使用,于是翻源代码. ...
- [JZOJ5279]香港记者题解--最短路图
[JZOJ5279]香港记者题解--最短路图 题目链接 过 于 暴 力 分析 有一个naiive的想法就是从1到n跑最短路,中途建图,然后在图上按字典序最小走一遍,然而·这是不行的,你这样跳不一定能跳 ...
- 基于【 Docker】三 || Docker的基本操作
一.Docker常用命令 1.搜索镜像:docker search 镜像名称 2.下载镜像:docker pull 镜像名称:版本号 3.查看镜像:docker images 4.删除镜像:docke ...
- VBA Excel宏(二)
在本章中,我们来学习如何逐步编写一个简单的宏. 第1步 - 首先,在Excel 2016中启用“开发者”菜单.要完成这个设置,请点击左上角菜单:文件 -> 选项.如下图所示 - 第2步 - 点击 ...
- Vue2.0+elementUI使用echarts插件
1.npm安装echarts: $ npm install echarts -S 2.html代码: <template> <div id="chartColumn ...
- 记录在苹果X手机上运行遇到的代码Dom被阻塞不更新的一个坑
一.问题产生背景: 开发支付功能,代码逻辑如下:点击支付后,请求后台接口得到流水号以及第三方支付台链接,跳转支付台(在苹果手机则是弹出支付台层):支付完毕后返回支付页面,或中途退出支付台返回支付页面: ...
- 对SPI进行参数化结构设计
前言 为了避免每次SPI驱动重写,直接参数化,尽量一劳永逸. SPI master有啥用呢,你发现各种外围芯片的配置一般都是通过SPI配置的,只不过有3线和四线. SPI slave有啥用呢,当外部主 ...