[GSS5] Can you answer these queries V
大力讨论。
luogu上交spoj的题卡的一比...
难受
wa了好几次,原因大概首先求的是非空区间,不能乱和0取max,第二点是求无相交的解时,在两段求lmx和rmx的时候可以取max(0)。
区间相交的有四种讨论,大概就是讨论一下左右端点在左/右/公共区间即可。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N=10005;
int n,q,a[N];
struct Segtree {
int lmx,rmx,mx,sum;
Segtree() {lmx=rmx=mx=sum=0;}
} t[N<<2];
Segtree pushup(Segtree x,Segtree y) {
Segtree ans;
ans.sum=x.sum+y.sum;
ans.lmx=max(x.sum+y.lmx,x.lmx);
ans.rmx=max(y.sum+x.rmx,y.rmx);
ans.mx=max(x.mx,max(y.mx,x.rmx+y.lmx));
return ans;
}
void build(int cur,int l,int r) {
if(l==r) {
t[cur].sum=t[cur].rmx=t[cur].lmx=t[cur].mx=a[l];
return;
}
int mid=l+r>>1;
build(cur<<1,l,mid);
build(cur<<1|1,mid+1,r);
t[cur]=pushup(t[cur<<1],t[cur<<1|1]);
}
Segtree query(int ql,int qr,int l,int r,int cur) {
if(ql>qr) { Segtree x;return x;}
if(ql<=l&&r<=qr) return t[cur];
int mid=l+r>>1;
if(mid<ql) return query(ql,qr,mid+1,r,cur<<1|1);
else if(qr<=mid) return query(ql,qr,l,mid,cur<<1);
else return pushup(query(ql,qr,l,mid,cur<<1),query(ql,qr,mid+1,r,cur<<1|1));
}
int T;
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(int i=1; i<=n; i++) scanf("%d",&a[i]);
build(1,1,n);
scanf("%d",&q);
int xa,xb,ya,yb;
while(q--) {
scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
int ans=0;
if(ya<xb) {
ans+=query(ya,xb,1,n,1).sum;
ans+=max(0,query(xa,ya-1,1,n,1).rmx);
ans+=max(0,query(xb+1,yb,1,n,1).lmx);
} else {
Segtree res1,res2,res3;
res1=query(xa,xb-1,1,n,1);
res2=query(xb,ya,1,n,1);
res3=query(ya+1,yb,1,n,1);
ans=max(max(res2.mx,res1.rmx+res2.lmx),max(res2.rmx+res3.lmx,res1.rmx+res2.sum+res3.lmx));
}
printf("%d\n",ans);
}
}
return 0;
}
[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 & ...
- 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 2916 GSS5 - Can you answer these queries V
传送门 解题思路 和GSS1相似,但需要巨恶心的分类讨论,对于x1<=y1< x2< =y2 这种情况 , 最大值应该取[x1,y1]的右端最大+[y1+1,x2-1]的和+[x2, ...
- 题解 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[ ...
- 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 ...
- SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...
- 【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 ...
随机推荐
- java反射并不是什么高深技术,面向对象语言都有这个功能,而且功能也很简单,就是利用jvm动态加载时生成的class对象
java反射并不是什么高深技术,面向对象语言都有这个功能. 面向对象语言都有这个功能,而且功能也很简单,就是利用jvm动态加载时生成的class对象,去获取类相关的信息 2.利用java反射可以调用类 ...
- netstat命令介绍-要用熟
这篇文章写的不错: http://www.cnblogs.com/CheeseZH/p/5169498.html 关注Linux的系统状态,主要从两个角度出发,一个角度是系统正在运行什么服务(ps命令 ...
- 不错的题目-n个数连接得到的最大值
这道题目还是很不错的 <[字符串排序]n个数连接得到最小或最大的多位整数> 题目 描述:设有n个正整数,将它们依次连成在一排,组成一个多位数,现在要求可能组成的多位数中最大的多位数是什么? ...
- java异常 之 异常的层次结构
一:起因 (1)近期在用java处理分析各种数据,碰到了一些异常,如parse()异常 ParseException,valueOf()NumberFormatException IllegalAr ...
- 使用enca进行字符集转码
在linux进行开发与运维的时候,我们常常遇到字符编码的问题,系统字符设置.vimrc fileencoding设置.终端设置往往搞的晕头转向,当一个文件出现乱码的时候,我们通常不能识别它是什么编码的 ...
- hdu1829 A Bug's Life(并查集)
开两个并查集.然后合并的时候要合并两次.这样在合并之前推断是否冲突,假设不冲突就进行合并,否则不须要继续合并. #include<cstdio> #include<cstdlib&g ...
- Linux - 控制台界面,虚拟界面,字符界面
tty控制台终端. pts虚拟终端. tty1 图形界面. tty2 字符界面. Ctrl+Alt+F2-6 在字符界面下,通过Alt+F2 切换回来.或者切换到其他的字符界面. Alt+F2 pts ...
- CodeForces 486B
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set { ...
- (Go)03.go类型
1.1 变量Go 是静态类型语⾔言,不能在运⾏行期改变变量类型.使⽤用关键字 var 定义变量,⾃自动初始化为零值.如果提供初始化值,可省略变量类型,由编译器⾃自动推断. var x int var ...
- shp系列(二)——利用C++进行shp文件的读(打开)
1.各数据类型及其字节数 BYTE 1; char 1; short 2; int 4; double 8; 2.位序big和little及其转换 对于位序是big的 ...