传送门

解题思路

和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的更多相关文章

  1. SPOJ GSS5 Can you answer these queries V

    Time Limit: 132MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are g ...

  2. SPOJ GSS5 Can you answer these queries V ——线段树

    [题目分析] GSS1上增加区间左右端点的限制. 直接分类讨论就好了. [代码] #include <cstdio> #include <cstring> #include & ...

  3. [GSS5] Can you answer these queries V

    大力讨论. luogu上交spoj的题卡的一比... 难受 wa了好几次,原因大概首先求的是非空区间,不能乱和0取max,第二点是求无相交的解时,在两段求lmx和rmx的时候可以取max(0). 区间 ...

  4. 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$,但是不保证端点所在的区间不重合 ...

  5. 题解 SP2916 【GSS5 - Can you answer these queries V】

    前言 最近沉迷于数据结构,感觉数据结构很有意思. 正文 分析 先来分类讨论一下 1. \(x2<y1\) 如果 \(y1<x2\) 的话,答案 \(=\max \limits_{ y1 \ ...

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

  7. 「 SPOJ GSS3 」 Can you answer these queries III

    # 题目大意 GSS3 - Can you answer these queries III 需要你维护一种数据结构,支持两种操作: 单点修改 求一个区间的最大子段和 # 解题思路 一个区间的最大子段 ...

  8. SPOJ 2916 Can you answer these queries V(线段树-分类讨论)

    题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...

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

随机推荐

  1. Python 字符串切片(slice)

    切片操作(slice)可以从一个字符串中获取子字符串(字符串的一部分).我们使用一对方括号.起始偏移量start.终止偏移量end 以及可选的步长step 来定义一个分片. 格式: [start:en ...

  2. 【笔记篇】Ubuntu一日游

    今天做数据的时候在Windows下出问题了(好像是爆栈了QAQ) 于是乎就打开了自己的Ubuntu虚拟机… 然而沉迷Windows的我已经忘记自己对这台虚拟机做过什么(比如装残了一个ycm自己都不知道 ...

  3. 记录openSUSE 源码安装node.js

    openSUSE版本: 42.2 目标:安装好 Node.js v6.10.3 在终端中可以使用 "su" 命令,切换到root用户. 1. 安装 gcc,gcc-c++ zypp ...

  4. idea-----使用相关快捷键

    1.快速格式化代码:Ctrl+Alt+L 2.快速引入get.set方法:ALT+insert 3.win 10锁屏:win+L 4.查找接口实现类的快捷键:ctrl+alt+b

  5. Apache服务器中运行CGI程序的方法,文中以Perl脚本作为示例

    关于apache与CGI在这里就不解释了. 1.apache下面以2.0.63为例介绍运行CGI程序的配置.(http://www.nklsyy.com) 2.下载Windows下的Perl解释器Ac ...

  6. LUOGU P1337 [JSOI2004]平衡点 / 吊打XXX(模拟退火)

    传送门 解题思路 学习了一下玄学算法--模拟退火,首先要求平衡处,也就是求势能最小的地方,就是求这个点到所有点的距离*重量最小.剩下的几乎是模拟退火的板子了. #include<iostream ...

  7. poj 3660 Cow Contest (bitset+floyd传递闭包)

    传送门 解题思路 考试题,想到传递闭包了,写了个O(n^3)的,T了7个点...后来看题解是tm的bitset优化???以前好像没听过诶(我太菜了),其实也不难,时间复杂度O(n^3/32) #inc ...

  8. Linux RHEL7(CentOS7源) 安装 Nginx

    安装步骤 1.添加 Nginx 源地址 CentOS7 默认没有提供 Nginx 的源,但 Nginx 自己提供了 sudo rpm -Uvh http://nginx.org/packages/ce ...

  9. 一次读懂mybatis中的缓存机制

    缓存功能针对于查询(没听说果UPDATE,INSERT语句要缓存什么,都是直接执行的) 默认情况下,mybatis会启用一级缓存. 如果使用同一个session对象调用了相同的SELECT语句,则直接 ...

  10. 02_Spring Bean的装配模式_基于XML配置方式

    一.三种实例化Bean的方式 1.使用类构造器实例化(默认无参数) <bean id="bean1" class="com.demo1.Bean1"> ...