给定一个序列。查询左端点在$[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. 经典算法入门 列表C/C++

    排序:插入排序.选择排序.冒泡排序.归并排序.快速排序.基数排序.计数排序.桶排序 查找:二分查找 树:先根.中根.后跟遍历 图:深度优先.广度优先.最小生成树.单元最短路径.全成对最短路径 动态规划 ...

  2. 开启POP3/SMTP服务

    实现发送邮件时需要先启用POP3/SMTP服务(以qq邮箱和网易邮箱启用为例) 一 qq邮箱启用 二.网易邮箱开启POP3/SMTP服务 至此:服务已开启

  3. xcap发包工具的简单使用3(报文描述)

    之前详细介绍了如何构造以及发送报文,现在简单对报文描述一下 1.Ethernet MAC:填写报文目的mac和源mac地址 Vlan:支持单vlan,QinQ,如果有更多的vlan,请填写在“More ...

  4. Nginx配置文件nginx.conf 详解

    #定义Nginx运行的用户和用户组 user www www;   #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8;   #全局错误日志定义类型,[ debu ...

  5. [bzoj1014][JSOI2008]火星人prefix_非旋转Treap_hash_二分

    火星人prefix bzoj-1014 JSOI-2004 题目大意:给定一个字符串,支持三种操作:1.查询:两个后缀之间的$LCP$:2.单点修改:3.插入一个字符. 注释:$1\le n\le 1 ...

  6. 【CV论文阅读】Image Captioning 总结

    初次接触Captioning的问题,第一印象就是Andrej Karpathy好聪明.主要从他的两篇文章开始入门,<Deep Fragment Embeddings for Bidirectio ...

  7. ubuntu下vi的使用

    ubuntu下vi的使用 ssh之后对于server的文件,我习惯用gedit,可是不好改动,于是就用vi. 1.vi的基本概念 基本上vi能够分为三种状态,各自是命令模式(command mode) ...

  8. HDU 4950 Monster(公式)

    HDU 4950 Monster 题目链接 题意:给定怪兽血量h,你攻击力a.怪物回血力b,你攻击k次要歇息一次,问是否能杀死怪兽 思路:签到题,注意最后一下假设打死了怪,那么怪就不会回血了 思路: ...

  9. C# PDF Page操作——设置页面切换按钮 C# 添加、读取Word脚注尾注 C#为什么不能像C/C++一样的支持函数只读传参 web 给大家分享一个好玩的东西,也许你那块就用的到

    C# PDF Page操作——设置页面切换按钮   概述 在以下示例中,将介绍在PDF文档页面设置页面切换按钮的方法.示例中将页面切换按钮的添加分为了两种情况,一种是设置按钮跳转到首页.下页.上页或者 ...

  10. 演练:我的第一个 WPF 桌面应用程序 https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/getting-started/walkthrough-my-first-wpf-desktop-application

    这篇文章演示如何开发简单的 Windows Presentation Foundation (WPF) 应用程序包括元素所共有的大多数 WPF 应用程序: 可扩展应用程序标记语言 (XAML) 标记. ...