[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 ...
随机推荐
- [Javascript Crocks] Apply a function in a Maybe context to Maybe inputs (curry & ap & liftA2)
Functions are first class in JavaScript. This means we can treat a function like any other data. Thi ...
- 网络编程----堵塞、非堵塞和同步、异步IO
我是学渣.但我想进步. 本文是面试我的牛人问我的.你知道什么是堵塞.非堵塞和同步.异步IO么?自觉得是分布式系统程序猿的我居然不知道.学习吧. 首先介绍堵塞IO和非堵塞IO: 堵塞IO:是指说程序等待 ...
- 利用scrapy抓取网易新闻并将其存储在mongoDB
好久没有写爬虫了,写一个scrapy的小爬爬来抓取网易新闻,代码原型是github上的一个爬虫,近期也看了一点mongoDB.顺便小用一下.体验一下NoSQL是什么感觉.言归正传啊.scrapy爬虫主 ...
- 根据图片url地址获取图片的宽高
/** * 根据img获取图片的宽高 * @param img 图片地址 * @return 图片的对象,对象中图片的真实宽高 */ public BufferedImage getBufferedI ...
- 【NOIP 2004】 虫食算
[题目链接] https://www.luogu.org/problemnew/show/P1092 [算法] 搜索 + 剪枝 直接搜索显然会超时,考虑剪枝 1 : 优化搜索顺序 2 : 假设我们已经 ...
- VM虚拟机-Windows
前提:安装了vm虚拟机 一.下载win10原版镜像文件 一定要是原版,修改版的不能用. 推荐下载网址:http://www.xitongtiandi.net/win10yuanban/ 下载后放在D盘 ...
- B - Soldier and Bananas
Problem description A soldier wants to buy w bananas in the shop. He has to pay k dollars for the fi ...
- 深入分析setContentView
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...
- Vue-router记录
一.嵌套路由默认选中第一个子路由 可以给主路由加一个重定向的属性,把路径指向相应的子路由. { path: '/home', name: 'Home', //重定向 redirect: '/home/ ...
- WordPress的wordfence插件的设置方法