题解 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 ...
随机推荐
- 机器学习算法的基本知识(使用Python和R代码)
本篇文章是原文的译文,然后自己对其中做了一些修改和添加内容(随机森林和降维算法).文章简洁地介绍了机器学习的主要算法和一些伪代码,对于初学者有很大帮助,是一篇不错的总结文章,后期可以通过文中提到的算法 ...
- JDK_Packages_java_utils
utils包需要关注的主要有 集合框架.并发包.函数式编程.观察者模式@see PropertyChangeSupport java.util(集合框架) Contains the collect ...
- Python scan查找Redis集群中的key
import redis import sys from rediscluster import StrictRedisCluster #host = "172.17.155.118&quo ...
- 安全测试——利用Burpsuite密码爆破(Intruder入侵)
本文章仅供学习参考,技术大蛙请绕过. 最近一直在想逛了这么多博客.论坛了,总能收获一堆干货,也从没有给博主个好评什么的,想想着实有些不妥.所以最近就一直想,有时间的时候自己也撒两把小米,就当作是和大家 ...
- 每日背单词 - Jun.
6月1日裸辞,计划休息到端午节后,这段时间玩的确实很开心,每天和朋友一起吹灯拔蜡:好不自在,可惜假期马上结束了,从今天开始恢复学习状态. 2018年6月1日 - 2018年6月14日 辞职休假 201 ...
- android activity 启动过程分析(source code 4.4)
说实话,android source code从2.3到4.4变化是蛮多的,尤其是media部分,虽然总的框架是没有多大变化,但是找起代码来看还是挺麻烦的.在android里面最受伤的是使用了java ...
- 94-datetmie模块
目录 datetmie模块 1 返回当前时间 2 当前时间+3天 3 当前时间-3天 4 当前时间-3小时 5 当前时间+30分钟 6 时间替换 datetmie模块 datetime模块可以看成是时 ...
- pip安装psycopg2失败解决
pip install psycopg2==2.8.4报错ERROR: Command "python setup.py egg_info" failed with error c ...
- 前阿里数据库专家总结的MySQL里的各种锁(上篇)
0.前言 MySQL按照加锁的范围,分为全局锁.表级锁.行级锁. 本文作为上篇,主要介绍MySQL的全局锁 和 表级锁. 重要的实战总结为,如何安全地变更一个表的表结构. 1.全局锁 定义: 全局锁就 ...
- PostgreSQL与MySQL对比
都属于开放源码的一员,性能和功能都在高速地提高和增强.MySQL AB的人们和PostgreSQL的开发者们都在尽可能地把各自的数据库改得越来越好,所以对于任何商业数据库使用其中的任何一个都不能算是错 ...