[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 ...
随机推荐
- OSX: 安装打印机的有用命令行
事实上非常easy.就是有用lpadmin命令,以下给出一个样例: printername="YOUR_PRINTER_NAME" location="LOCATION ...
- SUSAN算子
- 一个使用命令行编译Android项目的工具类
一个使用命令行编译Android项目的工具类 简单介绍 编译apk项目须要使用的几个工具,基本都在sdk中,它们各自是(Windows系统): 1.aapt.exe 资源打包工具 2.android. ...
- HDU 2604 Queuing,矩阵高速幂
题目地址:HDU 2604 Queuing 题意: 略 分析: 易推出: f(n)=f(n-1)+f(n-3)+f(n-4) 构造一个矩阵: 然后直接上板子: /* f[i] = f[i-1] ...
- DataGridView导出数据到Excel及单元格格式的改动
在软件开发过程中,时常会遇到把一些数据信息从DataGridView中导出到Excel表格中的情况.假设写的多了就会发现挺简单的,我们最好还是来写一写,留作备用,毕竟有时候Ctrl+C和Ctrl+V还 ...
- Intel 的 MKL是可以用来训练的——官方的实验也提到了训练
TensorFlow如何充分使用所有CPU核数,提高TensorFlow的CPU使用率,以及Intel的MKL加速 转载 2017年09月07日 16:34:58 标签: cpu / gpu 转载 ...
- 【转】寻找最好的笔记软件:海选篇 (v1.0)
原文网址:http://blog.sina.com.cn/s/blog_46dac66f01000b55.html 序言: 我见过的多数软件爱好者,无论是资深用户,还是初级用户,都有一个梦想:找到 ...
- Kaggle之泰坦尼克号幸存预测估计
上次已经讲了怎么下载数据,这次就不说废话了,直接开始.首先导入相应的模块,然后检视一下数据情况.对数据有一个大致的了解之后,开始进行下一步操作. 一.分析数据 1.Survived 的情况 train ...
- Ubuntu下安装sublime text3并汉化
转载请注明出处:果冻栋吖 通过ppa安装,打开终端,输入以下命令: sudo add-apt-repository ppa:webupd8team/sublime-text- sudo apt-get ...
- POJ 3620 DFS
题意: 给你n*m的矩形,有k个坏点 问最大坏点连通块的坏点数. 一发水题.. 裸的DFS // by SiriusRen #include <cstdio> #include <a ...