Description

给出了序列\(A_1,A_2,…,A_n\)。 \(a_i \leq 15007,1 \leq n \leq 50000\)。查询定义如下: 查询\((x,y)=max{a_i+a{i+1}+...+a_j;x \leq i \leq j \leq y }\)。 给定M个查询,程序必须输出这些查询的结果。

Input

  • 输入文件的第一行包含整数\(n\)。
  • 在第二行,\(n\)个数字跟随。
  • 第三行包含整数\(m\)。
  • \(m\)行跟在后面,其中第\(1\)行包含两个数字\(x_i\)和\(y_i\)。

Output

您的程序应该输出\(m\)查询的结果,每一行一个查询。

线段树维护最大子段和裸题.

直接把我的另一篇博客粘过来

数组定义

\(lsum[ ]\)代表 该区间左端点开始的最大连续和.

\(rsum[ ]\)代表 该区间右端点开始的最大连续和.

\(ssum[ ]\)代表 区间内最大连续和.

\(sum[ ]\) 代表区间和.

Que and A

Q:已知一个区间的左右区间的最大连续和,如何合并?

A:这个区间的最大连续和要么是左子区间的最大连续和,要么是右子区间的最大连续和.

要么是左子区间的最大右起子段和+右子区间的最大左起字段和.

code:\(ssum[o]=max(max(ssum[lson],ssum[rson]),rsum[lson]+lsum[rson])\)

Q:如何更新区间最大左起子段和.

A:新区间的最大左起子段和.要么是其左子区间最大连续和,要么是其左子区间和+右子区间的左起子段和.

最大右起子段和同理

code:\(lsum[o]=max(lsum[lson],sum[lson]+lsum[rson])\)

     \(rsum[o]=max(rsum[rson],sum[rson]+rsum[lson])\)

更新操作类似单点修改

代码中是结构体写法.

当两端不在左子节点或者右子节点的话,需要考虑合并的

代码

#include<cstdio>
#include<iostream>
#include<cctype>
#define ls o<<1
#define rs o<<1|1
#define R register
using namespace std;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
struct cod{int l,r,lsum,rsum,sum,ssum;}tr[50008*40];
inline void up(int o)
{
tr[o].sum=tr[ls].sum+tr[rs].sum;
tr[o].ssum=max(max(tr[ls].ssum,tr[rs].ssum),tr[ls].rsum+tr[rs].lsum);
tr[o].lsum=max(tr[ls].lsum,tr[ls].sum+tr[rs].lsum);
tr[o].rsum=max(tr[rs].rsum,tr[ls].rsum+tr[rs].sum);
}
void build(int o,int l,int r)
{
tr[o].l=l;tr[o].r=r;
if(l==r)
{
in(tr[o].sum);
tr[o].lsum=tr[o].rsum=tr[o].ssum=tr[o].sum;
return;
}
int mid=(l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
up(o);
}
cod query(int o,int x,int y)
{
if(tr[o].l>=x and y>=tr[o].r) return tr[o];
int mid=(tr[o].l+tr[o].r)>>1;
if(y<=mid) return query(ls,x,y);
if(x>mid) return query(rs,x,y);
else
{
cod t,t1=query(ls,x,y),t2=query(rs,x,y);
t.lsum=max(t1.lsum,t1.sum+t2.lsum);
t.rsum=max(t2.rsum,t2.sum+t1.rsum);
t.ssum=max(max(t1.ssum,t2.ssum),t1.rsum+t2.lsum);
return t;
}
}
int n,m;
int main()
{
in(n);
build(1,1,n);
in(m);
for(R int l,r;m;m--)
{
in(l),in(r);
if(l>r)swap(l,r);
printf("%d\n",query(1,l,r).ssum);
}
}

线段树【SP1043】GSS1 - Can you answer these queries I的更多相关文章

  1. 线段树 SP1043 GSS1 - Can you answer these queries I

    SP1043 GSS1 - Can you answer these queries I 题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义 ...

  2. SP1043 GSS1 - Can you answer these queries I 线段树

    问题描述 LG-SP1043 题解 GSS 系列第一题. \(q\) 个询问,求 \([x,y]\) 的最大字段和. 线段树,维护 \([x,y]\) 的 \(lmax,rmax,sum,val\) ...

  3. SP1043 GSS1 - Can you answer these queries I(猫树)

    给出了序列A[1],A[2],…,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y}. 给定M ...

  4. [SP1043] GSS1 - Can you answer these queries I

    传送门:>Here< 题意:求区间最大子段和 $N \leq 50000$ 包括多组询问(不需要支持修改) 解题思路 线段树的一道好题 我们可以考虑,如果一组数据全部都是正数,那么问题等同 ...

  5. 线段树 SP1716 GSS3 - Can you answer these queries III

    SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] ...

  6. 线段树 SP2713 GSS4 - Can you answer these queries IV暨 【洛谷P4145】 上帝造题的七分钟2 / 花神游历各国

    SP2713 GSS4 - Can you answer these queries IV 「题意」: n 个数,每个数在\(10^{18}\) 范围内. 现在有「两种」操作 0 x y把区间\([x ...

  7. SP1043 GSS1 - Can you answer these queries I(线段树,区间最大子段和(静态))

    题目描述 给出了序列A[1],A[2],…,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y} ...

  8. [题解] SPOJ GSS1 - Can you answer these queries I

    [题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...

  9. GSS1 - Can you answer these queries I(线段树)

    前言 线段树菜鸡报告,stO ZCDHJ Orz,GSS基本上都切完了. Solution 考虑一下用线段树维护一段区间左边连续的Max,右边的连续Max,中间的连续Max还有总和,发现这些东西可以相 ...

随机推荐

  1. Nuget 异常引用记录

    事件描述 Nuget未能将packages.config中的dll成功引入项目中 解决办法 从Nuget中删除对NewtonSoft.Json的引用并重新对NewtonSoft.Json 4.5.0. ...

  2. PHP excel 设置参数

    $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(-1); <?php error_ ...

  3. UVa 1326 - Jurassic Remains(枚举子集+中途相遇法)

    训练指南p.59 #include <cstdio> #include <cstring> #include <cstdlib> #include <map& ...

  4. Java 实现二叉树的构建以及3种遍历方法

    转载自http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历一直耿 ...

  5. atom插件之less-autocompile

    less-autocompile package Auto compile LESS file on save. Add the parameters on the first line of the ...

  6. hdu 1211 RSA (逆元)

    RSA Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  7. 【转】去掉HTML5中number类型input字段的小箭头

    第一种方案: 在chrome下: input::-webkit-outer-spin-button,input::-webkit-inner-spin-button{    -webkit-appea ...

  8. 2013 ACMICPC 杭州现场赛 I题

    #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...

  9. 【CF Round 439 A. The Artful Expedient】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  10. C中堆和栈的区别

    C++中堆和栈的完全解析 内存分配方面: 堆: 操作系统有一个记录空闲内存地址的链表,当系统收到程序的申请时,会遍历该链表,寻找第一个空间大于所申请空间的堆结点,然后将该结点从空闲结点链表中删 除,并 ...