链接:

https://vjudge.net/problem/SPOJ-GSS1

题意:

You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defined as follows:

Query(x,y) = Max { a[i]+a[i+1]+...+a[j] ; x ≤ i ≤ j ≤ y }.

Given M queries, your program must output the results of these queries.

区间最大子段和

思路:

线段树维护,区间总和, 区间中间最大和, 区间以左端点为起点的最大和, 区间以有端的结束的最大和.

向上的维护代码

  1. void PushUp(int root)
  2. {
  3. Seg[root].sum = Seg[root<<1].sum+Seg[root<<1|1].sum;
  4. //区间和
  5. Seg[root].midmax = max(Seg[root<<1].rmax+Seg[root<<1|1].lmax, max(Seg[root<<1].midmax, Seg[root<<1|1].midmax));
  6. //区间中间和,左节点中间和,右节点中间和,左节点右边和加右节点左边和,三个取最大
  7. Seg[root].lmax = max(Seg[root<<1].sum+Seg[root<<1|1].lmax, Seg[root<<1].lmax);
  8. //区间左边和, 左节点左边和,左节点区间和加右节点左边和,两个取最大
  9. Seg[root].rmax = max(Seg[root<<1|1].sum+Seg[root<<1].rmax, Seg[root<<1|1].rmax);
  10. //区间右边的, 右节点右边和,右节点区间和加左节点右边和,两个最大
  11. }

查询学到了新操作,返回结构体,挺好用的

代码:

  1. /*
  2. *线段树维护区间最大子段和
  3. * 模板
  4. */
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. typedef long long LL;
  8. const int MAXN = 5e4+10;
  9. const int INF = 1e9+10;
  10. const int NINF = -1e9;
  11. struct SegmentTree
  12. {
  13. LL sum;//区间全部和
  14. LL midmax;//区间中间值最大和
  15. LL lmax;//以左端点为起点的最大和
  16. LL rmax;//以右端点为终点的最大和
  17. }Seg[MAXN*4];
  18. LL a[MAXN];
  19. int n, q;
  20. void PushUp(int root)
  21. {
  22. Seg[root].sum = Seg[root<<1].sum+Seg[root<<1|1].sum;
  23. //区间和
  24. Seg[root].midmax = max(Seg[root<<1].rmax+Seg[root<<1|1].lmax, max(Seg[root<<1].midmax, Seg[root<<1|1].midmax));
  25. //区间中间和,左节点中间和,右节点中间和,左节点右边和加右节点左边和,三个取最大
  26. Seg[root].lmax = max(Seg[root<<1].sum+Seg[root<<1|1].lmax, Seg[root<<1].lmax);
  27. //区间左边和, 左节点左边和,左节点区间和加右节点左边和,两个取最大
  28. Seg[root].rmax = max(Seg[root<<1|1].sum+Seg[root<<1].rmax, Seg[root<<1|1].rmax);
  29. //区间右边的, 右节点右边和,右节点区间和加左节点右边和,两个最大
  30. }
  31. void Build(int root, int l, int r)
  32. {
  33. if (l == r)
  34. {
  35. Seg[root].sum = Seg[root].midmax = Seg[root].lmax = Seg[root].rmax = a[l];
  36. return;
  37. }
  38. int mid = (l+r)/2;
  39. Build(root<<1, l, mid);
  40. Build(root<<1|1, mid+1, r);
  41. PushUp(root);
  42. }
  43. SegmentTree Query(int root, int l, int r, int ql, int qr)
  44. {
  45. SegmentTree lt = {NINF, NINF, NINF, NINF}, rt = {NINF, NINF, NINF, NINF}, re = {NINF, NINF, NINF, NINF};
  46. if (ql <= l && r <= qr)
  47. return Seg[root];
  48. int mid = (l+r)/2;
  49. if (ql <= mid)
  50. lt = Query(root<<1, l, mid, ql, qr);
  51. if (qr > mid)
  52. rt = Query(root<<1|1, mid+1, r, ql, qr);
  53. re.sum = lt.sum+rt.sum;
  54. re.midmax = max(lt.rmax+rt.lmax, max(lt.midmax, rt.midmax));
  55. re.lmax = max(lt.sum+rt.lmax, lt.lmax);
  56. re.rmax = max(rt.sum+lt.rmax, rt.rmax);
  57. return re;
  58. }
  59. int main()
  60. {
  61. ios::sync_with_stdio(false);
  62. cin.tie(0);
  63. cin >> n;
  64. for (int i = 1;i <= n;i++)
  65. cin >> a[i];
  66. Build(1, 1, n);
  67. cin >> q;
  68. int l, r;
  69. while (q--)
  70. {
  71. cin >> l >> r;
  72. SegmentTree res = Query(1, 1, n, l, r);
  73. cout << max(res.midmax, max(res.lmax, res.rmax)) << endl;
  74. }
  75. return 0;
  76. }

SPOJ-GSS1-Can you answer these queries 1的更多相关文章

  1. [题解] SPOJ GSS1 - Can you answer these queries I

    [题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...

  2. 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]| ≤ ...

  3. SPOJ GSS1 Can you answer these queries I[线段树]

    Description You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A q ...

  4. SPOJ GSS1 Can you answer these queries I

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

  5. SPOJ GSS1 Can you answer these queries I ——线段树

    [题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...

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

  7. GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树

    GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...

  8. 线段树 SP1043 GSS1 - Can you answer these queries I

    SP1043 GSS1 - Can you answer these queries I 题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义 ...

  9. GSS3 SPOJ 1716. Can you answer these queries III gss1的变形

    gss2调了一下午,至今还在wa... 我的做法是:对于询问按右区间排序,利用splay记录最右的位置.对于重复出现的,在splay中删掉之前出现的位置所在的节点,然后在splay中插入新的节点.对于 ...

  10. GSS1 spoj 1043 Can you answer these queries I 最大子段和

    今天下午不知道要做什么,那就把gss系列的线段树刷一下吧. Can you answer these queries I 题目:给出一个数列,询问区间[l,r]的最大子段和 分析: 线段树简单区间操作 ...

随机推荐

  1. symbol,iterator,generator

    1.symbol是在ES6中引入的一种基本数据类型,因为symbol是不重复.唯一的数据特性,symbol设计是被用来表示对象内部的私有属性的.     symbol.for与symbol.keyfo ...

  2. 【疑难杂症】Firefox 火狐浏览器 抓不到本地数据包

    日期:2019-05-17 23:28:11 介绍:火狐浏览器,如何才能够抓到本地(127.0.0.1)的数据包? 0x01.问题描述 在 Firefox 上安装了证书,浏览器也可以正常抓取互联网的 ...

  3. 虚拟化 RemoteApp 远程接入 源码 免费

    远程接入 RemoteApp 虚拟化 源码 免费 1.终端安装与配置: 此远程接入组件的运行原理与瑞友天翼.异速连.CTBS等市面上常见的远程接入产品一样,是透过Windows的终端服务来实现的,速度 ...

  4. 获取使用GitHub api和Jira api Authentication的方法

    近段时间在搭建我司的用例管理平台,有如下需求: 1.需要根据项目--版本--轮次的形式来管理项目用例,用例统一保存在git工程. 2.执行用例时,如果用例执行失败,可以通过平台在Jira上提bug. ...

  5. Vijos lxhgww的奇思妙想--求K级祖先

    给出一棵树求K级祖先.O(N*logN+Q) 更详细的讲解见:https://www.cnblogs.com/cjyyb/p/9479258.html /* 要求k级祖先,我们可以把k拆成" ...

  6. 【Linux开发】OpenCV在ARM-linux上的移植过程遇到的问题1---cvNamedWindow调用报错的问题

    问题描述: 这个实际上是最后一部的问题,将生成的共享库文件放入到了/usr/local/opencv-arm/lib下,并且设置了LD_LIBRARY_PATH中为/usr/local/opencv- ...

  7. 【Qt开发】事件循环与线程 二

    事件循环与线程 二 Qt 线程类 Qt对线程的支持已经有很多年了(发布于2000年九月22日的Qt2.2引入了QThread类),Qt 4.0版本的release则对其所有所支持平台默认地是对多线程支 ...

  8. Windows.命令行(CMD)_执行命令&环境变量

    1.CMD命令中如果 命令有换行的话,就使用 ^来连接(这就类似于 Linux命令行中 \ 的作用) 2.环境变量 2.1.显示 所有环境变量的值,命令:set 2.2.显示 某个环境变量的值,命令 ...

  9. HTTPS测试

    1.首先理解HTTPS的含义,清楚http与HTTPS的区别 2.相对于http而言,https更加安全,因 3.使用数字证书使传输更安全,数字证书使用keytool工具生成 测试准备: 创建公钥和秘 ...

  10. clearfix:after 的用法

    想要清除浮动就要在父元素上 加上 clearfix:after .clearfix:after { <----在类名为“clearfix”的元素内最后面加入内容: content: " ...