大力讨论。

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的更多相关文章

  1. SPOJ GSS5 Can you answer these queries V

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

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

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

  3. 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$,但是不保证端点所在的区间不重合 ...

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

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

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

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

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

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

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

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

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

随机推荐

  1. [bzoj4127]Abs_树链剖分_线段树

    Abs bzoj-4127 题目大意:给定一棵数,支持链加和链上权值的绝对值的和. 注释:$1\le n,m \le 10^5$,$\delta \ge 0$,$|a_i|\le 10^8$. 想法: ...

  2. djanjo ORM 一对多、多对多

    一.表操作之一对多 定义表结构:定义了UserType.两张表UserInfo,如下: from django.db import models class UserType(models.Model ...

  3. 金融扫盲-资本市场从天使轮、ABCD轮、风投、到上市圈钱、借壳上市。

    转载请标明出处:http://blog.csdn.net/hu948162999/article/details/47777859 对于金融知识零基础的人进行扫盲,故事浅显易懂.趣味性强. 来之知乎. ...

  4. POJ3185 The Water Bowls 反转(开关)

    Description The cows have a line of 20 water bowls from which they drink. The bowls can be either ri ...

  5. linux下怎样将sheduler绑定到制定的cpu核上

    作者:张昌昌   1.顺序绑定 erl +sbt db 是按从前到后的顺序来绑定调度器的,如: erl +sbt db +S 3含义是启动erlang虚拟机,开启3个调度器,按顺序绑定在0,1.2号核 ...

  6. vue.js 父组件如何触发子组件中的方法

    组件 什么是组件? 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊 ...

  7. <% %> in html

    $(document).on('click', '.invoiceNumber', function () { var string = <%= StaticHelper.GetCurrentC ...

  8. Configure environment variables for different tools in jenkins

    安装以下的工具,并在Jenkins中的Manage Jenkins-->Configure System-->Global Properties-->Environment Vari ...

  9. Date and time types

    https://docs.microsoft.com/en-us/sql/t-sql/data-types/date-and-time-types date (Transact-SQL)datetim ...

  10. html转义字符换行以及回车等的使用

    欢迎加入前端交流群交流知识&&获取视频资料:749539640 html换行回车转义字符 换行Line feed   回车Carriage Return  html中换行转义字符 的使 ...