题面

You are given a sequence \(a_1,a_2,...,a_n\). (\(|A[i]| \leq 10000 , 1 \leq N \leq 10000\)). A query is defined as follows: Query(x1,y1,x2,y2) = \(Max{a_i+a_{i+1}+...+a_j;x_1 \leq i \leq y_1 , x_2 \leq j \leq y_2}\) and \(x_1 \leq x_2 , y_1 \leq y_2\). Given \(m\) queries (\(1 \leq M \leq 10000\)), your program must output the results of these queries.

题意

求所有左右端点分别在区间 \([x_1,y_1]\) 与 \([x_2,y_2]\) 的区间的最大连续子段和的最大值

思路

1° 两个区间不相交



答案显然是左边区间的 rmax+中间不重叠部分的 sum+右边区间的 lmax,即:\([x_1,y_1].rmax+[y_1,x_2].sum+[x_2,y_2].lmax\)

2° 两个区间相交



答案就会有三种情况

·① 答案区间为区间相交部分,即:\([x_2,y_1].max\)

·② 答案区间的左端点在相交部分左部,取相交部分左边的 rmax 和剩下区间的 lmax,再减掉加了两次的左边相交节点

即:\([x_1,x_2].rmax+[x_2,y_2].lmax-a_{x_2}\)

·③ 答案区间的右端点在相交部分右部,取相交部分右边的 lmax 和剩下区间的 rmax,再减掉加了两次的右边相交节点

即:\([y_1,y_2].lmax+[x_1,y_1].rmax-a_{y_1}\)

代码

/************************************************
*Author : lrj124
*Created Time : 2019.09.27.21:55
*Mail : 1584634848@qq.com
*Problem : spoj2916
************************************************/
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 10000 + 10;
struct seg { int l,r,sum,max; } tree[maxn<<2];
int T,n,q,a[maxn];
inline void pushup(int root) {
tree[root].sum = tree[root<<1].sum+tree[root<<1|1].sum;
tree[root].l = max(tree[root<<1].l,tree[root<<1|1].l+tree[root<<1].sum);
tree[root].r = max(tree[root<<1|1].r,tree[root<<1].r+tree[root<<1|1].sum);
tree[root].max = max(tree[root<<1].r+tree[root<<1|1].l,max(tree[root<<1].max,tree[root<<1|1].max));
}
inline void build(int l,int r,int root) {
if (l == r) {
tree[root] = { a[l],a[l],a[l],a[l] };
return;
}
int mid = l+r>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
pushup(root);
}
inline seg query(int l,int r,int ql,int qr,int root) {
if (ql > qr) return {0,0,0,0};
if (ql <= l && r <= qr) return tree[root];
int mid = l+r>>1;
if (mid >= qr) return query(l,mid,ql,qr,root<<1);
if (ql > mid) return query(mid+1,r,ql,qr,root<<1|1);
seg lson = query(l,mid,ql,qr,root<<1),rson = query(mid+1,r,ql,qr,root<<1|1),ans;
ans = { max(lson.l,rson.l+lson.sum),max(rson.r,lson.r+rson.sum),rson.sum+lson.sum,max(lson.r+rson.l,max(lson.max,rson.max)) };
return ans;
}
inline int solve(int l1,int r1,int l2,int r2) {
if (r1 < l2) return query(1,n,l1,r1,1).r+query(1,n,r1+1,l2-1,1).sum+query(1,n,l2,r2,1).l;
int ans = query(1,n,l2,r1,1).max;
if (l1 < l2) ans = max(ans,query(1,n,l1,l2,1).r+query(1,n,l2,r2,1).l-a[l2]);
if (r2 > r1) ans = max(ans,query(1,n,l1,r1,1).r+query(1,n,r1,r2,1).l-a[r1]);
return ans;
}
int main() {
for (scanf("%d",&T);T--;) {
memset(tree,0,sizeof(tree));
scanf("%d",&n);
for (int i = 1;i <= n;i++) scanf("%d",&a[i]);
build(1,n,1);
for (scanf("%d",&q);q--;) {
int l1,r1,l2,r2; scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
printf("%d\n",solve(l1,r1,l2,r2));
}
}
return 0;
}

【SP2916】Can you answer these queries V - 线段树的更多相关文章

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

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

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

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

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

  4. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

  5. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  6. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  7. 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树

    [BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...

  8. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  9. GSS4 2713. Can you answer these queries IV 线段树

    GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...

随机推荐

  1. Spring Boot使用AOP的正确姿势

    一.为什么需要面向切面编程? 面向对象编程(OOP)的好处是显而易见的,缺点也同样明显.当需要为多个不具有继承关系的对象添加一个公共的方法的时候,例如日志记录.性能监控等,如果采用面向对象编程的方法, ...

  2. DJANGO-天天生鲜项目从0到1-007-首页静态化与缓存

    本项目基于B站UP主‘神奇的老黄’的教学视频‘天天生鲜Django项目’,视频讲的非常好,推荐新手观看学习 https://www.bilibili.com/video/BV1vt41147K8?p= ...

  3. lambda之美

    github源码 大前提:jdk8  允许lambda表达式  最好在maven中加入 <properties> <java.version>1.8</java.vers ...

  4. VS Code小白使用教程

    本文来自作者:你不知道的巨蟹 原文链接 https://www.cnblogs.com/tu-0718/p/10935910.html,如有侵权,则可删除. 前言 现在使用Vscode编码的人越来越多 ...

  5. nginx location proxy_pass 后面的url 加与不加/的区别

    在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走. 首先是l ...

  6. Nginx与Apache简单对比

    Nginx 1.轻量级,采用C进行编写,同样的 web 服务,会占用更少的内存及资源 2.抗并发,处理请求是异步非阻塞的,负载能力比apache高很多,而 apache 则是阻塞型的.在高并发下 ng ...

  7. Python简单的语句组

    Python简单的语句组: ''' if 条件1: 条件1满足时,需要运行的内容 ''' num = 10 if num % 6 == 4: print("num 对 6 的取模结果是 4& ...

  8. PHP 魔术常量介绍

    PHP 魔术常量 PHP 向它运行的任何脚本提供了大量的预定义常量. 不过很多常量都是由不同的扩展库定义的,只有在加载了这些扩展库时才会出现,或者动态加载后,或者在编译时已经包括进去了. 有八个魔术常 ...

  9. PHP jewishtojd() 函数

    ------------恢复内容开始------------ 实例 把犹太历法的日期转换为儒略日计数: <?php$jd=jewishtojd(6,20,2007);echo $jd;?> ...

  10. PHP ftp_ssl_connect() 函数

    定义和用法 ftp_ssl_connect() 函数打开一个安全的 SSL-FTP 连接. 当连接打开,您就可以在服务器运行 FTP 函数. 语法 ftp_ssl_connect(host,port, ...