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.

Sample Input

3

-1 2 3

1

1 2

Sample Output

2

题意:

裸题,没有更新,只有查询。

思路:

区间中维护一下值:

从左端点开始的连续的最大值lm

从右端点开始的连续最大值rm

区间的和sum。

区间中的连续最大值num。

那么更新操作为:

lm=max(左儿子的lm,左儿子的sum+右儿子的lm)

rm=max(右儿子的rm,右儿子的sum+左儿子的rm)

num=max(左儿子的num,右儿子的num,左儿子的rm+右儿子的lm)

询问操作也返回区间,对区间进行合并处理操作。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 50000+7;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct node
{
int l;
int r;
ll num;
ll lm;
ll sum;
ll rm;
}segment_tree[maxn<<2];
int n;
void pushup(int rt)
{
segment_tree[rt].sum=segment_tree[rt<<1].sum+segment_tree[rt<<1|1].sum;
segment_tree[rt].lm=max(segment_tree[rt<<1].lm,segment_tree[rt<<1].sum+segment_tree[rt<<1|1].lm);
segment_tree[rt].rm=max(segment_tree[rt<<1|1].rm,segment_tree[rt<<1|1].sum+segment_tree[rt<<1].rm);
segment_tree[rt].num=max(segment_tree[rt<<1].num,segment_tree[rt<<1|1].num);
segment_tree[rt].num=max(segment_tree[rt].num,segment_tree[rt<<1].rm+segment_tree[rt<<1|1].lm);
} void build(int rt,int l,int r)
{
segment_tree[rt].l=l;
segment_tree[rt].r=r;
if(l==r)
{
scanf("%lld",&segment_tree[rt].num);
segment_tree[rt].lm=segment_tree[rt].rm=segment_tree[rt].num;
segment_tree[rt].sum=segment_tree[rt].num;
return ;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
pushup(rt);
} node ask(int rt,int l,int r)
{
if(segment_tree[rt].l==l&&segment_tree[rt].r==r)
{
return segment_tree[rt];
}
int mid=(segment_tree[rt].r+segment_tree[rt].l)>>1;
if(l>mid)
{
return ask(rt<<1|1,l,r);
}else if(r<=mid)
{
return ask(rt<<1,l,r);
}else
{
node res1=ask(rt<<1,l,mid);
node res2=ask(rt<<1|1,mid+1,r);
node res;
res.sum=res1.sum+res2.sum;
res.lm=max(res1.lm,res1.sum+res2.lm);
res.rm=max(res2.rm,res2.sum+res1.rm);
res.num=max(res1.num,res2.num);
res.num=max(res.num,res1.rm+res2.lm);
return res;
}
} int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
scanf("%d",&n);
build(1,1,n);
int m;
scanf("%d",&m);
while(m--)
{
int x,y;
scanf("%d %d",&x,&y);
printf("%lld\n",ask(1,x,y).num);
}
return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Can you answer these queries I SPOJ - GSS1 (线段树维护区间连续最大值/最大连续子段和)的更多相关文章

  1. Can you answer these queries? HDU - 4027 (线段树,区间开平方,区间求和)

    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...

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

  3. SPOJ - GSS1 —— 线段树 (结点信息合并)

    题目链接:https://vjudge.net/problem/SPOJ-GSS1 GSS1 - Can you answer these queries I #tree You are given ...

  4. Can you answer these queries V SPOJ - GSS5 (分类讨论+线段树维护区间最大子段和)

    recursion有一个整数序列a[n].现在recursion有m次询问,每次她想知道Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 &l ...

  5. Can you answer these queries? HDU - 4027(线段树+技巧)

    题意:给一个数组序列, 数组长度为100000 两种操作: 一种操作是将某一个固定区间所有数开方(向下取整) 另一种操作是询问某个区间的所有数字之和. 由于数不超过263,因此开个七八次就变成1,由于 ...

  6. Can you answer these queries?(HDU4027+势能线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 题目: 题意:n个数,每次区间更新将其数值变成它的根号倍(向下取整),区间查询数值和. 思路:易 ...

  7. SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树

    GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) ...

  8. 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]| ≤ ...

  9. [Codeforces]817F. MEX Queries 离散化+线段树维护

    [Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...

随机推荐

  1. 状态管理之 Flux、Redux、Vuex、MobX(概念篇)

    本文是对 Flux.Redux.Vuex.MobX 几种常用状态管理模式的总结,偏向于概念层面,不涉及过多代码. 状态管理 什么是状态管理? 状态管理就是,把组件之间需要共享的状态抽取出来,遵循特定的 ...

  2. 纹理特征描述之灰度差分统计特征(平均值 对比度 熵) 计算和比较两幅纹理图像的灰度差分统计特征 matlab代码实现

    灰度差分统计特征有: 平均值:​ 对比度:​ 熵:​ i表示某一灰度值,p(i)表示图像取这一灰度值的概率 close all;clear all;clc; % 纹理图像的灰度差分统计特征 J = i ...

  3. Django 1.x版本与2.x版本 区别

    django 1.x版本与2.x版本 URL区别 在django 1.x中的方式 导入的模块是'from django.conf.urls import url',urlpatterns中url对应的 ...

  4. 启动hive,提示ls: 无法访问/home/software/spark-2.0.1-bin-hadoop2.7/lib/spark-assembly-*.jar: 没有那个文件或目录

    原因是:spark升级到spark2以后,原有lib目录下的大JAR包被分散成多个小JAR包,原来的spark-assembly-*.jar已经不存在,所以hive没有办法找到这个JAR包. 解决办法 ...

  5. dfs入门-cogs1640[黑白图像]

    题目链接:http://cogs.pro:8081/cogs/problem/problem.php?pid=vxSmxkeqa [题目描述] 输入一个n×n的黑白图像(1表示黑色,0表示白色),任务 ...

  6. python之理解装饰器

    装饰器是修改其他函数的函数.好处是可以让你的函数更简洁. 一步步理解这个概念: 一.一切皆对象. def hi(name="yasoob"): return "hi &q ...

  7. C#常用处理数据类型转换、数据源转换、数制转换、编码转换相关的扩展

    public static class ConvertExtensions { #region 数据类型转换扩展方法 /// <summary> /// object 转换成string ...

  8. python界面编程

    这是一个简单的加法计算器 首先,要先对这个简易计算器进行布局设计,需要两个输入框,还有一个输入框用于存放就算的结果,还需要两个table,一个是"+"一个是"=" ...

  9. 【OpenCV入门教程之一】 OpenCV 2.4.8 +VS2010的开发环境配置

    目录(?)[-] 因为读研期间的研究方向是图像处理所以浅墨这段时间闭门研究了很多OpenCV和图像处理相关的知识与内容眼看自己积累到一定的程度了于是决定开始开设这个OpenCV系列专栏总结自己所学也分 ...

  10. Oracle-DQL 2- 限定和排序

    1.where子句--查询30号部门员工的姓名,职位和工资SELECT ename,job,sal,deptno FROM empWHERE deptno = 30; --查询职位是manager的员 ...