题目描述

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. 从servlet向jsp中传数据用Java接收js调用

    servlet: response.sendRedirect("showMessage.jsp?ValueA=1"); jsp: var a=<%=request.getPa ...

  2. 启动Hive时报错(com.mysql.jdbc.Driver") was not found in the CLASSPATH)

    这是因为没有mysql-connector的jar包.需要把jar包复制到hive目录lib文件夹中. 参考博客:https://blog.csdn.net/Realoyou/article/deta ...

  3. spring项目启动报错

    个人博客 地址:http://www.wenhaofan.com/article/20180921134534 错误信息 ERROR [localhost-startStop-1] - Context ...

  4. Eclipse设置代码模板

    个人博客 地址:http://www.wenhaofan.com/article/20180904173808 根据下列路径打开配置窗口 Window->Preferences->Java ...

  5. gulp打包js多个文件夹并压缩混淆,编译ES6语法,及多个import依赖由一个入口打包成一个cdn

    感觉和webpack的步骤差不多 首先安装gulp:参考上一篇 安装完之后 新建一个文件目录起名 在当前目录下打开cmd 执行:npm init 创建package.json文件 然后安装第一个插件g ...

  6. 一个vue的日历组件

    说明: 1.基于element-ui开发的vue日历组件. 地址 更新: 1.增加value-format指定返回值的格式2.增加头部插槽自定义头部 <ele-calendar > < ...

  7. HTML连载63-a标签的伪类选择器

    一.a标签的伪类选择器 1.通过观察可以发现a标签存在一定状态 (1)默认状态,从未被访问过 (2)被访问过的状态 (3)鼠标长按的状态 (4)鼠标悬停在a标签上的演示 2.什么是a标签的伪类选择器? ...

  8. Ehcache缓存框架与 Shiro 框架 出现出现验证错误 && Tomcat 缓存清除的问题

    当一个项目使用久了以后就会出现各种问题,下面是我遇到的一个权限验证错误的问题 我的项目是   Ehcache 结合 Shiro  一起使用的,项目用用久了出现   Token验证错误,Cookie之类 ...

  9. C语言预处理学习记录

    #include<stdio.h> #define LOCAL //无参宏 //条件编译 #ifdef LOCAL int a=1; #else int a=2; #endif #ifnd ...

  10. 操作系统-多用户如何理解(Linux)

    单用户.多用户.单任务.多任务,这么多种操作系统容易让人迷糊.其实这种初看你会觉得理解了一点,但其实你仔细研究会发现,多用户到底讲的是什么鬼? 多任务比较简单,就是应用程序都要放置到内存上去给CPU调 ...