SPOJ 2916 GSS5 - Can you answer these queries V
解题思路
和GSS1相似,但需要巨恶心的分类讨论,对于x1<=y1< x2< =y2 这种情况 , 最大值应该取[x1,y1]的右端最大+[y1+1,x2-1]的和+[x2,y2]的左端最大。对于x1< =x2< =y1<=y2,用四种情况,第一种是[x1,x2-1]的右端最大+[x2,y2]的左端最大,第二种是[x1,y1]的右端最大+[y1+1,y2]的左端最大,第三种是[x2,y1]的最大值,第四种是[x1,x2-1]的右端最大+[x2,y1]的和+[y1+1,y2]的左端最大。这四种情况取max即为答案。可以画图帮助理解。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 10005;
inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
int n,a[MAXN],T,m;
struct Node{
int lx,rx,sum,mx;
Node(){
lx=rx=sum=mx=0;
}
}node[MAXN<<2];
inline void pushup(int x){
node[x].sum=node[x<<1].sum+node[x<<1|1].sum;
node[x].lx=max(node[x<<1].lx,node[x<<1].sum+node[x<<1|1].lx);
node[x].rx=max(node[x<<1|1].rx,node[x<<1].rx+node[x<<1|1].sum);
node[x].mx=max(max(node[x<<1].mx,node[x<<1|1].mx),node[x<<1].rx+node[x<<1|1].lx);
}
inline void build(int x,int l,int r){
if(l==r){
node[x].sum=node[x].lx=node[x].mx=node[x].rx=a[l];
return;
}
int mid=l+r>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
pushup(x);
}
inline Node query(int x,int l,int r,int L,int R){
if(L<=l && r<=R) return node[x];
int mid=l+r>>1;
if(mid<L) return query(x<<1|1,mid+1,r,L,R);
else if(mid>=R) return query(x<<1,l,mid,L,R);
else {
Node A=query(x<<1,l,mid,L,R);
Node B=query(x<<1|1,mid+1,r,L,R);
Node ans;
ans.sum=A.sum+B.sum;
ans.lx=max(A.lx,A.sum+B.lx);
ans.rx=max(B.rx,B.sum+A.rx);
ans.mx=max(max(A.mx,B.mx),A.rx+B.lx);
return ans;
}
}
inline Node solve(int x1,int y1,int x2,int y2){
Node A=query(1,1,n,x1,y1);
Node B=query(1,1,n,x2,y2);
Node ans;
if(y1<x2) {
if(x2-1>=y1+1){
Node C=query(1,1,n,y1+1,x2-1);
ans.mx=A.rx+C.sum+B.lx;
}
else ans.mx=A.rx+B.lx;
}
else{
Node C,D,E;
if(x1<=x2-1) C=query(1,1,n,x1,x2-1);
if(y1+1<=y2) D=query(1,1,n,y1+1,y2);
E=query(1,1,n,x2,y1);
ans.mx=max(max(E.mx,C.rx+E.sum+D.lx),max(C.rx+B.lx,A.rx+D.lx));
}
return ans;
}
int main(){
T=rd();
while(T--){
n=rd();
for(register int i=1;i<=n;i++) a[i]=rd();
build(1,1,n);
m=rd();
while(m--){
int x1=rd(),y1=rd(),x2=rd(),y2=rd();
printf("%d\n",solve(x1,y1,x2,y2).mx);
}
}
return 0;
}
SPOJ 2916 GSS5 - Can you answer these queries V的更多相关文章
- SPOJ GSS5 Can you answer these queries V
Time Limit: 132MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- 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). 区间 ...
- 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$,但是不保证端点所在的区间不重合 ...
- 题解 SP2916 【GSS5 - Can you answer these queries V】
前言 最近沉迷于数据结构,感觉数据结构很有意思. 正文 分析 先来分类讨论一下 1. \(x2<y1\) 如果 \(y1<x2\) 的话,答案 \(=\max \limits_{ y1 \ ...
- 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 GSS3 」 Can you answer these queries III
# 题目大意 GSS3 - Can you answer these queries III 需要你维护一种数据结构,支持两种操作: 单点修改 求一个区间的最大子段和 # 解题思路 一个区间的最大子段 ...
- SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...
- 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 ...
随机推荐
- springcloud Finchley 版本hystrix 和 hystrix Dashboard
hystrix的断路功能 引用上个项目,创建新的model ,cloud-hystrix pom.xml <?xml version="1.0" encoding=" ...
- loj2509 hnoi2018排列
题意:对于a数组,求它的一个合法排列的最大权值.合法排列:对于任意j,k,如果a[p[j]]=p[k],那么k<j. 权值:sigma(a[p[i]]*i).n<=50W. 标程: #in ...
- linux 服务 启动 关闭 列表
##查看服务在每个级别的运行状态 chkconfig --list httpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:启用 6:关闭 bluetooth ...
- Java系列笔记(4) - JVM监控与调优【转】
Java系列笔记(4) - JVM监控与调优[转] 目录 参数设置收集器搭配启动内存分配监控工具和方法调优方法调优实例 光说不练假把式,学习Java GC机制的目的是为了实用,也就是为了在 ...
- DataTime 和 时间转化
如果知道tostring 的字符串格式那么可以根据字符串格式转化成 DateTime string timeText = DateTime.Now.ToString("yy/MM/dd HH ...
- 【自家测试】2017-12-16 FJOI2016 d1
1. 所有公共子序列问题(allcs.pas/c/cpp)★问题描述:一个给定序列的子序列是在该序列中删去若干元素后得到的序列.确切地说,若给定序列X= x 1 x 2 ... x m ,则另一序列Z ...
- 清空标签间的内容(innerHTML)和 value
jquery 方式: 清空标签的innerHTML: $("#divId").html(""); 清空标签的value: $("#divId" ...
- Java之io nio aio 的区别
这个问题最近面试总是遇到,作为一个只会写流水代码的程序员,一脸懵逼.看了网上的解释,看的还是很模糊,说下我对这个的理解. 先引出一个话题,两个大水缸,一个空一个满,让你把一个缸里面的水弄到另一个里面. ...
- Python学习day26-面向对象之小结
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Ubuntu中使用Nginx+rtmp搭建流媒体直播服务
一.背景 本篇文章是继上一篇文章<Ubuntu中使用Nginx+rtmp模块搭建流媒体视频点播服务>文章而写,在上一篇文章中我们搭建了一个点播服务器,在此基础上我们再搭建一个直播服务器, ...