题目描述

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.

输入输出格式

输入格式

  • 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 \(x_i\) and \(y_i\).

输出格式

Your program should output the results of the \(M\) queries, one query per line.

输入输出样例

输入样例#1

3
-1 2 3
1
1 2

输出样例#1

2

题意翻译

给出了序列 \(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\)个查询,程序必须输出这些查询的结果,每行一个查询。

题解

\(SPOJ\)的\(GSS\)系列一共有\(8\)题,这\(8\)道题目都是有关数据结构的,与\(Ynoi\)类似。

这是\(SPOJ\)的\(GSS\)系列的第一题,考察的是用线段树求区间最大子段和 (本蒟蒻不会猫树) 。

众所周知,线段树有以下基本的\(3\)个操作:\(pushup\)、\(bulid\)和\(getans\),这\(3\)个操作分别对应合并区间、建树的求答案。

我们尝试用这三种操作解决这道题:

首先,我们定义一个结构体:

struct Node
{
int sum, lans, rans, ans;
} t[50005 << 2];

其中,\(sum\)表示区间和,\(lans\)表示最大前缀和,\(rans\)表示最大后缀和,\(ans\)表示区间内的最大子段和,我们的目标是求出\(x\)~\(y\)区间内的\(ans\)。

然后,我们分析,如何进行\(pushup\)操作。

易知,这个区间内的区间和就是它子区间的和加上它右子区间的和。

区间最大前缀和是它左子区间最大子段和,与左子区间和加上右子区间的最大前缀和的最大值,最大后缀和同理。

考虑如何合并区间最大子段和?

经过分析,我们得出:区间最大子段和就是\(max(\)左子区间的最大子段和,右子区间的最大子段和,

左子区间的最大后缀+右子区间的最大前缀和\()\)。

综上,我们就得出了\(pushup\)的代码:

inline void pushup(int x)
{
t[x].sum = t[x << 1].sum + t[(x << 1) | 1].sum;//求出区间和
t[x].lans = max(t[x << 1].lans, t[x << 1].sum + t[(x << 1) | 1].lans);//区间最大前缀和
t[x].rans = max(t[(x << 1) | 1].rans, t[(x << 1) | 1].sum + t[x << 1].rans);//区间最大后缀和
t[x].ans = max(max(t[x << 1].ans, t[(x << 1) | 1].ans), t[x << 1].rans + t[(x << 1) | 1].lans);//区间最大子段和
}

\(build\)操作与普通线段树的\(build\)操作一模一样。

下面放出\(build\)操作的代码:

void bulid(int s, int o, int p)
{
if (s == o)//已经是叶子节点
{
t[p].sum = t[p].lans = t[p].rans = t[p].ans = gi();//输入并初始化叶子节点的成员
return;
}
int mid = (s + o) >> 1;//找出区间中点
bulid(s, mid, p << 1);//递归左子区间
bulid(mid + 1, o, (p << 1) | 1);//递归右子区间
pushup(p);//合并区间
}

\(getans\)操作同理。

下面放出\(AC\)代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype> using namespace std; inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar();}
return f * x;
} int n, m;
struct Node
{
int sum, lans, rans, ans;
} t[50005 << 2]; inline void pushup(int x)//合并操作
{
t[x].sum = t[x << 1].sum + t[(x << 1) | 1].sum;
t[x].lans = max(t[x << 1].lans, t[x << 1].sum + t[(x << 1) | 1].lans);
t[x].rans = max(t[(x << 1) | 1].rans, t[(x << 1) | 1].sum + t[x << 1].rans);
t[x].ans = max(max(t[x << 1].ans, t[(x << 1) | 1].ans), t[x << 1].rans + t[(x << 1) | 1].lans);
} void bulid(int s, int o, int p)//建树
{
if (s == o)
{
t[p].sum = t[p].lans = t[p].rans = t[p].ans = gi();
return;
}
int mid = (s + o) >> 1;
bulid(s, mid, p << 1);
bulid(mid + 1, o, (p << 1) | 1);
pushup(p);
} Node getans(int l, int r, int s, int o, int p)//求答案
{
if (l <= s && r >= o)//如果包含区间
{
return t[p];//就直接返回
}
int mid = (s + o) >> 1;//求出中点
if (l > mid) return getans(l, r, mid + 1, o, (p << 1) | 1);//如果左端点在中点右边,就递归右区间
if (r <= mid) return getans(l, r, s, mid, p << 1);//如果右端点在中点左边,就递归左区间
else
{
Node ans, a, b;
a = getans(l, r, s, mid, p << 1), b = getans(l, r, mid + 1, o, (p << 1) | 1);//求出左区间和右区间的各项参数
ans.sum = a.sum + b.sum;
ans.ans = max(max(a.ans, a.rans + b.lans), b.ans);
ans.lans = max(a.lans, a.sum + b.lans);
ans.rans = max(b.rans, b.sum + a.rans);//合并答案
return ans;//最后返回答案
}
} int main()//进入主函数
{
n = gi();//输入节点个数
bulid(1, n, 1);//建树
m = gi();//输入询问个数
for (int i = 1; i <= m; i++)
{
int x = gi(), y = gi();
printf("%d\n", getans(x, y, 1, n, 1).ans);//求出答案
}
return 0;//结束
}

题解【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. [题解] SPOJ GSS1 - Can you answer these queries I

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

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

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

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

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

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

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

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

  8. 线段树【SP1043】GSS1 - Can you answer these queries I

    Description 给出了序列\(A_1,A_2,-,A_n\). \(a_i \leq 15007,1 \leq n \leq 50000\).查询定义如下: 查询\((x,y)=max{a_i ...

  9. 「SP1043」GSS1 - Can you answer these queries I

    传送门 Luogu 解题思路 这题就是 GSS3 的一个退化版,不带修改操作的区间最大子段和,没什么好讲的. 细节注意事项 咕咕咕 参考代码 #include <algorithm> #i ...

随机推荐

  1. VNote笔记本和画图

    VNote笔记本 跨平台的,以markdown标记语言记录的文本文档.从sourceforget.org开源网址下载即可. 画图集成: 1.集成graphviz http://www.graphviz ...

  2. 常用的H5代码

    1.返回上一页第一次在手机端用到返回上一页的时候,只写了window.history.go(-1):这一句.但是只在安卓手机有效果,兼容苹果手机需要在跳转代码后加上return false:这句.跳转 ...

  3. C++记录(二)

    1.算术移位和逻辑移位. 逻辑移位是只补0,算术移位是看符号,负数补1,正数补0(讨论的是右移的情况下). 负数左移右边一样补0.如果遇到位运算的相关题目需要对int变量进行左移而且不知道正负,那么先 ...

  4. AntDesign(React)学习-3 React基础

    前面项目已经建起来了,但是没有React基础怎么办,从头学习,这个项目使用的是基于React16.X版本的几种技术集成,那么我们就从网上找一些相关的资料进行研究,我的习惯是用到哪学到哪. 一.先看一些 ...

  5. AE开发常见问题总结

    一. Arcgis10.0 组件初始化失败解决方法: ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.EngineOrDe ...

  6. C++11 新用法

    基于哈希的 map 和 set 简述 基于哈希的 map 和 set ,它们分别叫做 unordered_map, unordered_set .数据分布越平均,性能相较 map 和 set 来说提升 ...

  7. centos7下编译安装redis5.05

    准备环境: 1.一台centos7机器,配置没有什么要求(能联网) 2.下载好redis压缩包 下载redis包: 1.登录redis官网: https://redis.io/download 2.选 ...

  8. 虚拟机win7 安装过程中遇到虚拟工具安装失败

    如果遇见这样的问题 倘若你用的是这样的镜像:cn_windows_7_enterprise_x64_dvd_x15-70741.iso 换镜像源.换镜像源.换镜像源! 换成带sp1的iso镜像:cn_ ...

  9. 【PAT甲级】1116 Come on! Let's C (20分)

    题意: 输入一个正整数N(<=10000),接着依次输入N个学生的ID.输入一个正整数Q,接着询问Q次,每次输入一个学生的ID,如果这个学生的ID不出现在之前的排行榜上输出Are you kid ...

  10. D3新学习

    1. Python Web(1):建立第一个Web项目 直接将d3 index.html代码拷贝进index.html中即可. 2. D3入门教程; D3.js的v5版本入门教程