传送门

线段树经典题。

就是让你求左端点在[l1,r1][l1,r1][l1,r1]之间,右端点在[l2,r2][l2,r2][l2,r2]之间且满足l1≤l2,r1≤r2l1\le l2,r1 \le r2l1≤l2,r1≤r2的最大子段和。


直接分类讨论就行了。

如果两个区间不相交的话,答案就是rmax(l1,l2)+sum(l2+1,l2−1)+lmax(l2,r2)rmax(l1,l2)+sum(l2+1,l2-1)+lmax(l2,r2)rmax(l1,l2)+sum(l2+1,l2−1)+lmax(l2,r2)。

如果相交的话,讨论一下就是max(max(rmax(l1,l2)+lmax(l2,r2)−val[l2],rmax(l1,r1)+lmax(r1,r2)−val[r1]),midmax(l2,r1))max(max(rmax(l1,l2)+lmax(l2,r2)-val[l2],rmax(l1,r1)+lmax(r1,r2)-val[r1]),midmax(l2,r1))max(max(rmax(l1,l2)+lmax(l2,r2)−val[l2],rmax(l1,r1)+lmax(r1,r2)−val[r1]),midmax(l2,r1))

代码:

  1. #include<bits/stdc++.h>
  2. #define lc (p<<1)
  3. #define rc (p<<1|1)
  4. #define mid (T[p].l+T[p].r>>1)
  5. #define N 10005
  6. using namespace std;
  7. inline int read(){
  8. int ans=0,w=1;
  9. char ch=getchar();
  10. while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
  11. while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
  12. return ans*w;
  13. }
  14. int n,m,a[N],T_T;
  15. struct Node{int l,r,ls,rs,ms,sum;}T[N<<2];
  16. inline Node operator+(const Node&a,const Node&b){
  17. Node ret;
  18. ret.l=a.l,ret.r=b.r,ret.sum=a.sum+b.sum;
  19. ret.ls=max(a.ls,a.sum+b.ls);
  20. ret.rs=max(b.rs,b.sum+a.rs);
  21. ret.ms=max(max(a.ms,b.ms),a.rs+b.ls);
  22. return ret;
  23. }
  24. inline void build(int p,int l,int r){
  25. T[p].l=l,T[p].r=r;
  26. if(l==r){T[p].ls=T[p].rs=T[p].ms=T[p].sum=a[l];return;}
  27. build(lc,l,mid),build(rc,mid+1,r),T[p]=T[lc]+T[rc];
  28. }
  29. inline Node query(int p,int ql,int qr){
  30. if(ql>T[p].r||qr<T[p].l)return (Node){T[p].l,T[p].r,0,0,0,0};
  31. if(ql<=T[p].l&&T[p].r<=qr)return T[p];
  32. if(qr<=mid)return query(lc,ql,qr);
  33. if(ql>mid)return query(rc,ql,qr);
  34. return query(lc,ql,mid)+query(rc,mid+1,qr);
  35. }
  36. int main(){
  37. T_T=read();
  38. while(T_T--){
  39. n=read();
  40. for(int i=1;i<=n;++i)a[i]=read();
  41. build(1,1,n),m=read();
  42. while(m--){
  43. int l1=read(),r1=read(),l2=read(),r2=read();
  44. if(r1<l2){
  45. Node ansl=query(1,l1,r1),ansm=query(1,r1+1,l2-1),ansr=query(1,l2,r2);
  46. printf("%d\n",ansl.rs+ansm.sum+ansr.ls);
  47. }
  48. else{
  49. int ans=query(1,l2,r1).ms;
  50. if(l1<l2)ans=max(ans,query(1,l1,l2).rs+query(1,l2,r2).ls-a[l2]);
  51. if(r2>r1)ans=max(ans,query(1,l1,r1).rs+query(1,r1,r2).ls-a[r1]);
  52. printf("%d\n",ans);
  53. }
  54. }
  55. }
  56. return 0;
  57. }

2018.10.16 spoj Can you answer these queries V(线段树)的更多相关文章

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

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

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

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

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

  4. SPOJ - GSS1-Can you answer these queries I 线段树维护区间连续和最大值

    SPOJ - GSS1:https://vjudge.net/problem/SPOJ-GSS1 参考:http://www.cnblogs.com/shanyr/p/5710152.html?utm ...

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

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

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

  7. SPOJ GSS1 - Can you answer these queries I(线段树维护GSS)

    Can you answer these queries I SPOJ - GSS1 You are given a sequence A[1], A[2], -, A[N] . ( |A[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. 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 ...

随机推荐

  1. zabbix配合脚本监控Kafka

    简介: Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据.Kafka如下特性,受到诸多公司的青睐. 1.高吞吐量:即使是非常普通的硬件Kafka也可以支持 ...

  2. HTML5 通过文件输入框读取文件为base64文件, 并借助canvas压缩 FileReader, files, drawImage

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. 4.Spring中使用Log4j

    转自:https://blog.csdn.net/luohai859/article/details/52250807 这里要实现web项目中利用Spring来使用Log4j (1)接上面的工程,然后 ...

  4. 下载google code中源码的几个工具

    Google code 一般以三种命令行方式提供源代码,格式如下: hg clone https://code.google.com/p/xxx/ git clone https://code.goo ...

  5. Vue.js路由详解

    有时候,我们在用vue的时候会有这样的需求,比如一个管理系统,点了左边的菜单栏,右边跳转到一个新的页面中,而且刷新的时候还会停留在原来打开的页面. 又或者,一个页面中几个不同的画面来回点击切换,这两种 ...

  6. 使用robotium对android应用进行自动化测试

    所需要的环境: 1.eclipse 2.android development tools(ADT) 3.software develoment kit(SDK) 4.JDK 5.robotium 1 ...

  7. Hadoop 3.0.0-alpha1几个值得关注的特性

    1.支持纠删码:意味着更灵活的存储策略,即经常使用的数据利用备份方式存储(3倍存储消耗),冷数据利用纠删码容错(1.4倍存储消耗,但会造成额外的IO及CPU消耗): 2.MapReduce任务支持本地 ...

  8. Mycat入门及简单规则

    以下都来自网络: 官网: http://www.mycat.io/ 官网配置文件解析: https://github.com/MyCATApache/Mycat-Server/wiki/9.0-%E6 ...

  9. Spring @Configuration

    下面是一个典型的spring配置文件(application-config.xml): <beans> <bean id="orderService" class ...

  10. HTTP协议图示详解

    一.概念 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器. ...