题解 SP2916 【GSS5 - Can you answer these queries V】
前言
最近沉迷于数据结构,感觉数据结构很有意思。
正文
分析
先来分类讨论一下
1. \(x2<y1\)

如果 \(y1<x2\) 的话,答案 \(=\max \limits_{ y1 \leq x \leq y2} \{ s_i \} - \min \limits_{x1 \leq x \leq x2} \{ s_i \}\)
其中 \(s_i\) 表示 \(\sum\limits_{j=1}^{i} a_j\) ,就是俗称的前缀和

这个东西可以用线段树维护。
2. \(x2>=y1\)

这个怎么处理呢
答案 \(=\) \(\max \begin{cases}\max \limits_{ y1 \leq x \leq y2} \{ s_i \} - \min \limits_{x1 \leq x \leq y1} \{ s_i \}\\ \max \limits_{ x2 \leq x \leq y2} \{ s_i \} - \min \limits_{x1 \leq x \leq x2} \{ s_i \}\\ f(x,y)\end{cases}\)
这里的 \(f\) 函数就是最大子段和。
不会用线段树求最大子段和的可以看这里
最上面的 \(2\) 个也是可以用线段树来维护的。
代码
最后要注意的一点是前缀和,我们知道区间 \([i,j]\) 的和 $ = s_j-s_{i-1}$,而不是 $ = s_j-s_{i}$
这个怎么处理呢,我们发现我们的题目全部都是用 \(Max\) 函数的答案减去 \(Min\) 函数的答案,所以,我们可以把线段树维护的 \(\min\) 全部减去 \(a_{i}\)
现在这份代码就能 \(A\) 了
#include <bits/stdc++.h>
#define ls num<<1
#define rs num<<1|1
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &FF){
T RR=1;FF=0;char CH=getchar();
for(;!isdigit(CH);CH=getchar())if(CH=='-')RR=-1;
for(;isdigit(CH);CH=getchar())FF=(FF<<1)+(FF<<3)+(CH^48);
FF*=RR;
}
template<typename T>void write(T x){
if(x<0)putchar('-'),x*=-1;
if(x>9)write(x/10);
putchar(x%10+48);
}
template<typename T>void writen(T x){
write(x);
puts("");
}
const int MAXN=5e4+10;
struct Tree{
int l,r;
ll lans,rans,sum,ans,max,min;
}t[MAXN*4];
int a[MAXN],x,y,n,T,T_,l1,r1,l2,r2;
ll s[MAXN];
Tree pushup(Tree L,Tree R){
Tree z;
z.sum=L.sum+R.sum;
z.lans=max(L.lans,L.sum+R.lans);
z.rans=max(R.rans,R.sum+L.rans);
z.ans=max(max(L.ans,R.ans),L.rans+R.lans);
z.max=max(L.max,R.max);
z.min=min(L.min,R.min);
z.l=L.l;z.r=R.r;
return z;
}
void build(int l,int r,int num){
if(l==r){
t[num].l=l;
t[num].r=r;
t[num].sum=a[l];
t[num].lans=a[l];
t[num].rans=a[l];
t[num].ans=a[l];
t[num].max=s[l];
t[num].min=s[l]-a[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,ls);
build(mid+1,r,rs);
t[num]=pushup(t[ls],t[rs]);
}
Tree query(int num){
if(x<=t[num].l&&t[num].r<=y)return t[num];
if(t[rs].l>y)return query(ls);
if(t[ls].r<x)return query(rs);
return pushup(query(ls),query(rs));
}
ll Max(int num){
if(t[num].l>=x&&t[num].r<=y)return t[num].max;
if(t[ls].r<x)return Max(rs);
if(t[rs].l>y)return Max(ls);
return max(Max(ls),Max(rs));
}
ll Min(int num){
if(t[num].l>=x&&t[num].r<=y)return t[num].min;
if(t[ls].r<x)return Min(rs);
if(t[rs].l>y)return Min(ls);
return min(Min(ls),Min(rs));
}
ll Mx(int a,int b){
x=a;y=b;
return Max(1);
}
ll Mn(int a,int b){
x=a;y=b;
return Min(1);
}
ll qy(int a,int b){
x=a;y=b;
return query(1).ans;
}
int main(){
read(T);
while(T--){
read(n);
for(int i=1;i<=n;i++)read(a[i]);
for(int i=1;i<=n;i++)s[i]=s[i-1]+a[i];
build(1,n,1);
read(T_);
while(T_--){
read(l1);read(r1);read(l2);read(r2);
if(r1>l2)writen(max(qy(l2,r1),max(Mx(l2,r2)-Mn(l1,l2),Mx(r1,r2)-Mn(l1,r1))));
else writen(Mx(l2,r2)-Mn(l1,r1));
}
}
return 0;
}
后记
这篇题解如果有问题可以私信或评论,告诉我,一起完善
题解 SP2916 【GSS5 - Can you answer these queries V】的更多相关文章
- SP2916 GSS5 - Can you answer these queries V
给定一个序列.查询左端点在$[x_1, y_1]$之间,且右端点在$[x_2, y_2]$之间的最大子段和,数据保证$x_1\leq x_2,y_1\leq y_2$,但是不保证端点所在的区间不重合 ...
- SPOJ GSS5 Can you answer these queries V
Time Limit: 132MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- 【SP2916】Can you answer these queries V - 线段树
题面 You are given a sequence \(a_1,a_2,...,a_n\). (\(|A[i]| \leq 10000 , 1 \leq N \leq 10000\)). A qu ...
- SPOJ GSS5 Can you answer these queries V ——线段树
[题目分析] GSS1上增加区间左右端点的限制. 直接分类讨论就好了. [代码] #include <cstdio> #include <cstring> #include & ...
- [GSS5] Can you answer these queries V
大力讨论. luogu上交spoj的题卡的一比... 难受 wa了好几次,原因大概首先求的是非空区间,不能乱和0取max,第二点是求无相交的解时,在两段求lmx和rmx的时候可以取max(0). 区间 ...
- SPOJ 2916 GSS5 - Can you answer these queries V
传送门 解题思路 和GSS1相似,但需要巨恶心的分类讨论,对于x1<=y1< x2< =y2 这种情况 , 最大值应该取[x1,y1]的右端最大+[y1+1,x2-1]的和+[x2, ...
- GSS5 spoj 2916. Can you answer these queries V 线段树
gss5 Can you answer these queries V 给出数列a1...an,询问时给出: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[ ...
- [题解] SPOJ GSS1 - Can you answer these queries I
[题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...
- Can you answer these queries V SPOJ - GSS5 (分类讨论+线段树维护区间最大子段和)
recursion有一个整数序列a[n].现在recursion有m次询问,每次她想知道Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 &l ...
随机推荐
- Archlinux 自动挂载移动硬盘,开机自动启动smb服务
Archlinux + Raspberry 打造NAS: samba篇 树莓派自动挂载硬盘,并开启smb服务. 开机自动挂在移动硬盘ntfs 安装ntfs-3g sudo pacman -S ntfs ...
- yii框架通过控制台命令创建定时任务
假设Yii项目路径为 /home/apps 1. 创建文件 /home/apps/web/protected/commands/console.php $yii = '/home/apps/frame ...
- Vue数据绑定(一)
Contents Vue作为当下炙手可热的前端三大框架之一,一直都想深入研究一下其内部的实现原理,去学习MVVM模式的精髓.如果说MVVM是当下最流行的图形用户界面开发模式,那么数据绑定则是这一模式的 ...
- Activity源码解析 - 读书笔记
1. Activity启动 Activity是一个比较好的模板方法模式.在Android系统启动时,第一个启动的进程是zygote进程,然后由zygote启动SystemServer,再后就是启动AW ...
- Redis(3)——分布式锁深入探究
一.分布式锁简介 锁 是一种用来解决多个执行线程 访问共享资源 错误或数据不一致问题的工具. 如果 把一台服务器比作一个房子,那么 线程就好比里面的住户,当他们想要共同访问一个共享资源,例如厕所的时候 ...
- Samtec与Neoconix达成合作并和II-VI推出新产品
序言:Samtec近日动作不断, 近日Samtec与Neoconix达成合作并和II-VI推出新产品,以下是详细内容. Samtec与Neoconix签订Neoconix PCBeam 技术授权协议, ...
- OpenCV读一张图片并显示
Java 版本: JavaCV 用OpenCV读一张图片并显示.只需将程序运行时的截图回复.如何安装配置创建项目编写OpenCV代码,可参考何东健课件和源代码或其他资源. package com.gi ...
- 2020年春招面试必备Spring系列面试题129道(附答案解析)
前言 关于Spring的知识总结了个思维导图分享给大家 1.不同版本的 Spring Framework 有哪些主要功能? 2.什么是 Spring Framework? Spring 是一个 ...
- Javascript中String()和new String()的区别——JS的包装对象
最近在看Symbol不能使用new操作符,然后类比到Number,String,Boolean,因为它们同属于基本类型,但是有有所差异:Number,String,Boolean是可以使用new操作符 ...
- 304 Not Modified
304 Not Modified,不是服务器发出的错误,是服务器所承载的业务系统在开发时为了节省带宽和提升浏览器的体验,对GET/js,css,image等执行了缓存机制.客户端第一次对服务器发出GE ...