链接:

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. Git-Runoob:Git 工作流程

    ylbtech-Git-Runoob:Git 工作流程 1.返回顶部 1. Git 工作流程 本章节我们将为大家介绍 Git 的工作流程. 一般工作流程如下: 克隆 Git 资源作为工作目录. 在克隆 ...

  2. 【MAC】 命令行解压缩 rar 文件

    使用Mac解压rar文件很简单,总共分2步.step1:安装解压工具,step2:解压 1.使用Homebrew安装unrar brew install unrar 2.cd到rar文件的目录下,然后 ...

  3. 阶段3 1.Mybatis_03.自定义Mybatis框架_4.自定义mybatis的编码-解析XML的工具类介绍

    导入xml操作的类和用到的相关包 创建util包,然后把提供好的XMLConfigBuilder.java文件复制3过来 复制过来,里面用到了很多dom4j的东西 打开pom.xml 输入depend ...

  4. Django开启https(不用nginx)

    首先安装需要用到的包 pip install django-extensions pip install django-werkzeug-debugger-runserver pip install ...

  5. QA的工作职责是什么?

    目前不知道,后续一点一点查资料补充吧 QA不管做什么的类型的测试,最基础的功能测试,需要搭建测试环境:进阶部分的性能压力测试,对搭建环境的要求更高:接口功能测试,搭建测试环境,和功能测试的差不多: 测 ...

  6. jmeter线程组基本设置

    线程组基本设置 在线程组界面中可以设置以下数据,进行控制线程组: 1.取样器错误后要执行的动作: 继续:忽略错误,继续执行 Start Next Thread Loop: 忽略错误,线程当前循环终止, ...

  7. win10家庭版安装Docker (Docker Toolbox)

    开启CPU的虚拟化功能(自行百度)之前安装了VM所以这部跳过.具体查看可以打开任务管理器->性能->cpu 查看是否有 虚拟化:已启用字样 下载Docker Toolbox  下载地址 h ...

  8. DataAdapter的Fill方法(转)

    使用DataAdapter填充DataSet(1) 在选择了DataAdapter的类型(SqlDataAdapter或OleDbDataAdapter)并配置了DataAdapter来执行所需的任务 ...

  9. 右键windows terminal here无法进入当前目录

    很久没写水笔了,简单记一水 使用windows terminal的基本上都自己改过注册表,添加到右键windows terminal here吧,用着很方便,哪里不会点哪里. 我起初删除掉starti ...

  10. MySQL-快速入门(1)基本数据库、表操作语句

    1.创建数据库 create database db_name;show create database db_name\G; //查看数据创建语句show databases; //查看当前创建的数 ...