链接:

https://vjudge.net/problem/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.

区间最大子段和

思路:

线段树维护,区间总和, 区间中间最大和, 区间以左端点为起点的最大和, 区间以有端的结束的最大和.

向上的维护代码

void PushUp(int root)
{
Seg[root].sum = Seg[root<<1].sum+Seg[root<<1|1].sum;
//区间和
Seg[root].midmax = max(Seg[root<<1].rmax+Seg[root<<1|1].lmax, max(Seg[root<<1].midmax, Seg[root<<1|1].midmax));
//区间中间和,左节点中间和,右节点中间和,左节点右边和加右节点左边和,三个取最大
Seg[root].lmax = max(Seg[root<<1].sum+Seg[root<<1|1].lmax, Seg[root<<1].lmax);
//区间左边和, 左节点左边和,左节点区间和加右节点左边和,两个取最大
Seg[root].rmax = max(Seg[root<<1|1].sum+Seg[root<<1].rmax, Seg[root<<1|1].rmax);
//区间右边的, 右节点右边和,右节点区间和加左节点右边和,两个最大
}

查询学到了新操作,返回结构体,挺好用的

代码:

/*
*线段树维护区间最大子段和
* 模板
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int MAXN = 5e4+10;
const int INF = 1e9+10;
const int NINF = -1e9; struct SegmentTree
{
LL sum;//区间全部和
LL midmax;//区间中间值最大和
LL lmax;//以左端点为起点的最大和
LL rmax;//以右端点为终点的最大和
}Seg[MAXN*4];
LL a[MAXN];
int n, q; void PushUp(int root)
{
Seg[root].sum = Seg[root<<1].sum+Seg[root<<1|1].sum;
//区间和
Seg[root].midmax = max(Seg[root<<1].rmax+Seg[root<<1|1].lmax, max(Seg[root<<1].midmax, Seg[root<<1|1].midmax));
//区间中间和,左节点中间和,右节点中间和,左节点右边和加右节点左边和,三个取最大
Seg[root].lmax = max(Seg[root<<1].sum+Seg[root<<1|1].lmax, Seg[root<<1].lmax);
//区间左边和, 左节点左边和,左节点区间和加右节点左边和,两个取最大
Seg[root].rmax = max(Seg[root<<1|1].sum+Seg[root<<1].rmax, Seg[root<<1|1].rmax);
//区间右边的, 右节点右边和,右节点区间和加左节点右边和,两个最大
} void Build(int root, int l, int r)
{
if (l == r)
{
Seg[root].sum = Seg[root].midmax = Seg[root].lmax = Seg[root].rmax = a[l];
return;
}
int mid = (l+r)/2;
Build(root<<1, l, mid);
Build(root<<1|1, mid+1, r);
PushUp(root);
} SegmentTree Query(int root, int l, int r, int ql, int qr)
{
SegmentTree lt = {NINF, NINF, NINF, NINF}, rt = {NINF, NINF, NINF, NINF}, re = {NINF, NINF, NINF, NINF};
if (ql <= l && r <= qr)
return Seg[root];
int mid = (l+r)/2;
if (ql <= mid)
lt = Query(root<<1, l, mid, ql, qr);
if (qr > mid)
rt = Query(root<<1|1, mid+1, r, ql, qr);
re.sum = lt.sum+rt.sum;
re.midmax = max(lt.rmax+rt.lmax, max(lt.midmax, rt.midmax));
re.lmax = max(lt.sum+rt.lmax, lt.lmax);
re.rmax = max(rt.sum+lt.rmax, rt.rmax);
return re;
} int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1;i <= n;i++)
cin >> a[i];
Build(1, 1, n);
cin >> q;
int l, r;
while (q--)
{
cin >> l >> r;
SegmentTree res = Query(1, 1, n, l, r);
cout << max(res.midmax, max(res.lmax, res.rmax)) << endl;
} return 0;
}

SPOJ-GSS1-Can you answer these queries 1的更多相关文章

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

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

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

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

  4. SPOJ GSS1 Can you answer these queries I

    Time Limit: 115MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are g ...

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

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

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

  7. GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树

    GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...

  8. 线段树 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).查询定义 ...

  9. GSS3 SPOJ 1716. Can you answer these queries III gss1的变形

    gss2调了一下午,至今还在wa... 我的做法是:对于询问按右区间排序,利用splay记录最右的位置.对于重复出现的,在splay中删掉之前出现的位置所在的节点,然后在splay中插入新的节点.对于 ...

  10. GSS1 spoj 1043 Can you answer these queries I 最大子段和

    今天下午不知道要做什么,那就把gss系列的线段树刷一下吧. Can you answer these queries I 题目:给出一个数列,询问区间[l,r]的最大子段和 分析: 线段树简单区间操作 ...

随机推荐

  1. Vue 渲染函数

    Vue 推荐在绝大多数情况下使用模板来创建你的 HTML.然而在一些场景中,你真的需要 JavaScript 的完全编程的能力.这时你可以用渲染函数,它比模板更接近编译器. 一 项目结构 二 App组 ...

  2. linux 运行时加载不上动态库 解决方法(转)

    1. 连接和运行时库文件搜索路径到设置     库文件在连接(静态库和共享库)和运行(仅限于使用共享库的程序)时被使用,其搜索路径是在系统中进行设置的.一般 Linux 系统把 /lib 和 /usr ...

  3. 关于golang的label

    1 label所在的代码段在没有跳转的时候按照所在的位置按顺序执行 2 break label和continue label可以一次性从多重循环中跳出 3 goto label的用法和c/c++中的一 ...

  4. [19/06/08-星期六] CSS基础_表格&表单

    一.表格 如生活中的Excel表格,用途就是同来表示一些格式化的数据,如课程表.工资条.成绩单. 在网页中也可以创建出不同的表格,在HTML中使用table标签来创建一个表格.table是个块元素. ...

  5. 循环结构 :for

    循环结构 :for 循环四要素: 1.初始化条件 2.循环条件 3.循环体 4.迭代条件 格式: for(初始化条件;循环条件;迭代条件){ 循环体; } 执行顺序 :1 -> 2 -> ...

  6. HDU-4857 逃生(反向拓扑排序 + 逆向输出)

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  7. 高德地图搜索功能以及清除搜索结果maker

    第一次写文章,写得不好各位看官见谅~ (pσ_σ)P首先这是一个vue里面的项目,高德地图api是直接CDN进来的,所以使用了global来调用,默认已经初始化了一个地图,为了实现一个输入框搜索功能和 ...

  8. TApplication,TForm,TControl,TComponent,TWinControl研究(博客索引)good

    TApplication,TForm,TControl,TComponent,TWinControl研究 http://blog.csdn.net/suiyunonghen/article/detai ...

  9. Linux安装 jdk&maven

    JDK安装 1. 下载JDK压缩包http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...

  10. python基本语法学习

    前言: 1.python是解释型语言 2.尽量使用python3.2014年的时候python官方宣布2.7支持到2020年,以后不会再发行2.8版本,尽快把程序迁移到3.x版本 基本语法: 1.变量 ...