给定一个序列。查询左端点在$[x_1, y_1]$之间,且右端点在$[x_2, y_2]$之间的最大子段和,数据保证$x_1\leq x_2,y_1\leq y_2$,但是不保证端点所在的区间不重合

这题可以分为几种情况讨论

$y_1<x_2$

那么这个时候发现$[y_1+1,x_2-1]$里的数必须得选,并选出$[x1,y1]$的最大后缀和$[x2,y2]$的最大前缀。用结构体维护一下就好了

$y_1\geq x_2$

发现这个时候左右端点所在区间的情况分别如下

$l$在$[x_1,x_2]$,$r$在$[x_2,y_1]$,那么只要查询$[x_1,x_2]$的最大后缀和$[x_2,y_1]$的最大前缀

$l$在$[x_1,x_2]$,$r$在$[y_1,y_2]$,那么只要查询$[x_1,x_2]$的最大后缀和$[x_2,y_2]$的最大前缀

$l$在$[x_2,y_1]$,$r$在$[x_2,y_1]$,那么只要查询$[x_2,y_1]$的最大子段和

$l$在$[x_2,y_1]$,$r$在$[y_1,y_2]$,那么只要查询$[x_2,y_1]$的最大后缀和$[y_1,y_2]$的最大前缀

 //minamoto
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ls (p<<1)
#define rs (p<<1|1)
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,:;}
int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
char sr[<<],z[];int C=-,Z;
inline void Ot(){fwrite(sr,,C+,stdout),C=-;}
void print(int x){
if(C><<)Ot();if(x<)sr[++C]=,x=-x;
while(z[++Z]=x%+,x/=);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=1e4+;int a[N],n,m;
struct node{int pre,mid,suf,sum;}b[N<<];
node merge(node l,node r){
node ans;
ans.pre=max(l.pre,l.sum+r.pre);
ans.mid=max(max(l.mid,r.mid),l.suf+r.pre);
ans.suf=max(l.suf+r.sum,r.suf);
ans.sum=l.sum+r.sum;
return ans;
}
void upd(int p){b[p]=merge(b[ls],b[rs]);}
void build(int p,int l,int r){
if(l==r) return (void)(b[p].pre=b[p].mid=b[p].suf=b[p].sum=a[l]);
int mid=(l+r)>>;
build(ls,l,mid),build(rs,mid+,r);
upd(p);
}
node query(int p,int l,int r,int ql,int qr){
if(ql>qr) return (node){,,,};
if(ql<=l&&qr>=r) return b[p];
int mid=(l+r)>>;
if(qr<=mid) return query(ls,l,mid,ql,qr);
else if(ql>mid) return query(rs,mid+,r,ql,qr);
else return merge(query(ls,l,mid,ql,qr),query(rs,mid+,r,ql,qr));
}
int get(int l1,int r1,int l2,int r2){
if(r1<l2){
int tmp=query(,,n,l1,r1).suf;
tmp+=query(,,n,r1+,l2-).sum;
tmp+=query(,,n,l2,r2).pre;
return tmp;
}
int ans=query(,,n,l2,r1).mid;
if(l1<l2) cmax(ans,query(,,n,l1,l2).suf+query(,,n,l2,r2).pre-a[l2]);
if(r2>r1) cmax(ans,query(,,n,l1,r1).suf+query(,,n,r1,r2).pre-a[r1]);
return ans;
}
int main(){
// freopen("testdata.in","r",stdin);
int T=read();
while(T--){
n=read();
for(int i=;i<=n;++i) a[i]=read();
build(,,n);
m=read();
while(m--){
int l1=read(),r1=read(),l2=read(),r2=read();
print(get(l1,r1,l2,r2));
}
}
return Ot(),;
}

SP2916 GSS5 - Can you answer these queries V的更多相关文章

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

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

  2. SPOJ GSS5 Can you answer these queries V

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

  3. 【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 ...

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

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

  5. [GSS5] Can you answer these queries V

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

  6. SPOJ 2916 GSS5 - Can you answer these queries V

    传送门 解题思路 和GSS1相似,但需要巨恶心的分类讨论,对于x1<=y1< x2< =y2 这种情况 , 最大值应该取[x1,y1]的右端最大+[y1+1,x2-1]的和+[x2, ...

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

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

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

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

随机推荐

  1. 51nod - 1278 相离的圆 (二分)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1278 因为圆心都在x轴上,把每个圆转化成线段后,按线段的起点排序,那么对 ...

  2. Devu and Flowers lucas定理+容斥原理

    Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contain ...

  3. UVA 1347_Tour

    题意: 给定一系列按x坐标升序排列的点,一个人从左向右走到终点再从终点走回起点,要求每个点恰好经过一次,问所走过的最短路径长度. 分析: 可以看成是两个人同时从起点向终点走,且除起点终点外每个点恰有一 ...

  4. HDU——2647 Reward

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. HashMap的工作原理以及代码实现,为什么要转换成红黑树?

    原理参考:https://blog.csdn.net/striveb/article/details/84657326 总结: 为什么当桶中键值对数量大于8才转换成红黑树,数量小于6才转换成链表? 参 ...

  6. iphone的ibooks如何导入pdf?

    使用QQ把pdf文档从电脑上发到手机上,使用手机的QQ打开文档,在手机QQ上,用其他应用打开文档,选择‘拷贝’到ibooks

  7. 014 DNS

    解析主机名 Router>en Password: Router#config t Enter configuration commands, one per line.  End with C ...

  8. NA交换②

    虚拟局域网VLAN的核心目的:     将一个大的网络划分为小的网络,也称为网络分片(Segementation):一个VLAN对应着一个广播域,最好对应一个网络子网(为VLAN间的路由作准备).   ...

  9. react-native redux 操作

    1.项目目录 2.redux (1)app/redux/action/action.js /** * 步骤一 * 行为 action */ // 定义行为名称 export const CHANGE_ ...

  10. Jenkins安装与使用

    一.Jenkins简介 Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发布/测试项目. 2.监控外部调用执行的工作 二.下载与安装 下载地址 ...