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)的更多相关文章

  1. 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 ...

  2. SPOJ GSS1 Can you answer these queries I ——线段树

    [题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...

  3. 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 ...

  4. 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[ ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. SPOJ 2916 Can you answer these queries V(线段树-分类讨论)

    题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...

  9. SPOJ GSS3 Can you answer these queries III ——线段树

    [题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...

随机推荐

  1. 创客课堂——Scratch实例演示

    大家好,这里是蓝精灵创客公益课堂,我是蓝老师. 前两期我们认识了Scratch的界面和菜单功能,本期我们就可以根据提示的步骤,学习一些Scratch的基本操作. 下面就开始今天学习内容 一.开始移动 ...

  2. Idea中一个服务按多个端口同时启动

    1.勾选并行启动 2.-Dserver.port=9018

  3. maftools | 从头开始绘制发表级oncoplot(瀑布图)

    本文首发于微信公众号 **“ 生信补给站 ”** ,期待您的关注!!! 原文链接:https://mp.weixin.qq.com/s/G-0PtaoO6bYuhx_D_Rlrlw 对于组学数据的分析 ...

  4. js原生方法的重写

    讲干货,不啰嗦,通过prototype可以获取到JavaScript的原型对象,进而可以在对象原型上添加新的属性和方法,当该方法与原方法名称一样时会覆盖原方法既:重写,当不一样时既:添加 如:实现数组 ...

  5. /sockjs-node/info 报错问题

    首先 sockjs-node 确实是维持全双工通信用的,关键在于为什么要有这个东西,其实其作用就是保证我们在改完代码重新编译之后,能够通知浏览器重新加载变更结果(我也是因为之前都可以改完代码之后浏览器 ...

  6. CVE-2019-0213: Apache Archiva Stored XSS

    CVE-2019-0213: Apache Archiva Stored XSS Severity: Low Vendor:The Apache Software Foundation Version ...

  7. Oracle创建上下文 SYS_CONTEXT

    Oracle创建上下文 SYS_CONTEXT 1. 系统默认的一些参数 set heading offselect SYS_CONTEXT('USERENV', 'TERMINAL') termin ...

  8. springboot学习链接

    https://github.com/wuyouzhuguli/SpringAll

  9. Linux脑洞——怎么知道我这个Linux是啥时候开的机?

    生产环境上,Linux服务器肯定不能随便重启,到底啥时候开的机,这个问题真没啥讨论价值.但是自己在WMWare Workstation下装虚拟机不一样 who -b

  10. 深度学习—从LeNet到DenseNet

    CNN从90年代的LeNet开始,21世纪初沉寂了10年,直到12年AlexNet开始又再焕发第二春,从ZF Net到VGG,GoogLeNet再到ResNet和最近的DenseNet,网络越来越深, ...