今天下午不知道要做什么,那就把gss系列的线段树刷一下吧。

Can you answer these queries I

题目:给出一个数列,询问区间[l,r]的最大子段和

分析:
线段树简单区间操作。
线段树中记录lx,rx,mx,分别表示:最大前驱连续和,最大后继连续和,区间最大子段和。
在合并时时只需要合并两个区间即可,具体可以看代码的Union。
从队友jingo那里学到了这种合并的写法,发现比网上大部分代码简单很多。

  1. #include <set>
  2. #include <map>
  3. #include <list>
  4. #include <cmath>
  5. #include <queue>
  6. #include <stack>
  7. #include <string>
  8. #include <vector>
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <iostream>
  12. #include <algorithm>
  13.  
  14. using namespace std;
  15.  
  16. typedef long long ll;
  17. typedef unsigned long long ull;
  18.  
  19. #define debug puts("here")
  20. #define rep(i,n) for(int i=0;i<n;i++)
  21. #define rep1(i,n) for(int i=1;i<=n;i++)
  22. #define REP(i,a,b) for(int i=a;i<=b;i++)
  23. #define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
  24. #define pb push_back
  25. #define RD(n) scanf("%d",&n)
  26. #define RD2(x,y) scanf("%d%d",&x,&y)
  27. #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
  28. #define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
  29. #define All(vec) vec.begin(),vec.end()
  30. #define MP make_pair
  31. #define PII pair<int,int>
  32. #define PQ priority_queue
  33. #define cmax(x,y) x = max(x,y)
  34. #define cmin(x,y) x = min(x,y)
  35. #define Clear(x) memset(x,0,sizeof(x))
  36. /*
  37.  
  38. #pragma comment(linker, "/STACK:1024000000,1024000000")
  39.  
  40. int size = 256 << 20; // 256MB
  41. char *p = (char*)malloc(size) + size;
  42. __asm__("movl %0, %%esp\n" :: "r"(p) );
  43.  
  44. */
  45.  
  46. /******** program ********************/
  47.  
  48. const int MAXN = 100005;
  49.  
  50. int a[MAXN];
  51.  
  52. struct segTree{
  53. int l,r,lx,rx,mx,sum;
  54. inline int mid(){
  55. return (l+r)>>1;
  56. }
  57. }tree[MAXN<<2];
  58.  
  59. inline void Union(segTree& now,segTree l,segTree r){
  60. now.lx = max( l.lx , l.sum+max(0,r.lx) );
  61. now.rx = max( r.rx , r.sum+max(0,l.rx) );
  62. now.mx = max( l.rx+r.lx , max(l.mx,r.mx) );
  63. now.sum = l.sum+r.sum;
  64. }
  65.  
  66. void build(int l,int r,int rt){
  67. tree[rt].l = l;
  68. tree[rt].r = r;
  69. if(l==r){
  70. tree[rt].lx = tree[rt].rx = tree[rt].sum = tree[rt].mx = a[l];
  71. return;
  72. }
  73. int mid = tree[rt].mid();
  74. build(l,mid,rt<<1);
  75. build(mid+1,r,rt<<1|1);
  76. Union(tree[rt],tree[rt<<1],tree[rt<<1|1]);
  77. }
  78.  
  79. segTree ask(int l,int r,int rt){
  80. if(l<=tree[rt].l&&r>=tree[rt].r)
  81. return tree[rt];
  82. int mid = tree[rt].mid();
  83. segTree ans;
  84. if(r<=mid) ans = ask(l,r,rt<<1);
  85. else if(l>mid) ans = ask(l,r,rt<<1|1);
  86. else{
  87. segTree a = ask(l,r,rt<<1);
  88. segTree b = ask(l,r,rt<<1|1);
  89. Union( ans,a,b );
  90. }
  91. Union(tree[rt],tree[rt<<1],tree[rt<<1|1]);
  92. return ans;
  93. }
  94.  
  95. int main(){
  96.  
  97. #ifndef ONLINE_JUDGE
  98. freopen("sum.in","r",stdin);
  99. //freopen("sum.out","w",stdout);
  100. #endif
  101.  
  102. int m,n,x,y;
  103. while(~RD(n)){
  104. rep1(i,n)
  105. RD(a[i]);
  106. build(1,n,1);
  107. RD(m);
  108. while(m--){
  109. RD2(x,y);
  110. segTree now = ask(x,y,1);
  111. printf("%d\n",now.mx);
  112. }
  113. }
  114.  
  115. return 0;
  116. }

  

GSS1 spoj 1043 Can you answer these queries I 最大子段和的更多相关文章

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

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

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

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

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

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

  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. spoj 4487. Can you answer these queries VI (gss6) splay 常数优化

    4487. Can you answer these queries VI Problem code: GSS6 Given a sequence A of N (N <= 100000) in ...

  8. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

  9. SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)

    GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...

随机推荐

  1. android 绘图之Canvas,Paint类

    Canvas,Paint 1.在android 绘图但中经常要用到Canvas和Paint类,Canvas好比是一张画布,上面已经有你想绘制图画的轮廓了,而Paint就好比是画笔,就要给Canvas进 ...

  2. 消息系统Kafka介绍 - 董的博客

    1.  概述 Kafka是Linkedin于2010年12月份开源的消息系统,它主要用于处理活跃的流式数据.活跃的流式数据在web网站应用中非常常见,这些数据包括网站的pv.用户访问了什么内容,搜索了 ...

  3. 数据结构之hash表

    哈希表是种数据结构,它可以提供快速的插入操作和查找操作.hash定义了一种将字符组成的字符串转换为固定长度(一般是更短长度)的数值或索引值的方法,称为散列法,也叫哈希法.由于通过更短的哈希值比用原始值 ...

  4. hadoop学习笔记之倒排索引

    开发工具:eclipse 目标:对下面文档phone_numbers进行倒排索引: 13599999999 1008613899999999 12013944444444 13800138000137 ...

  5. Objective-C的对象模型和runtime机制

    内容列表 对象模型(结构定义,类对象.元类和实例对象的关系) 消息传递和转发机制 runtime系统功能理解 对象模型 结构定义 对象(Object): OC中基本构造单元 (building blo ...

  6. Fork/Join框架之双端队列

    简介 ForkJoinPool管理着ForkJoinWorkerThread线程,ForkJoinWorkerThread线程内部有一个双端队列,这个双端队列主要由一个数组queue.数组下标queu ...

  7. 如何在 iOS 8 中使用 Swift 实现本地通知(下)

    在上集中,我们已经构建了一个简单的待办列表应用(to-do list app),这个应用可以在待办项过期时通过本地通知提醒用户.现在,我们要在之前的基础上添加以下功能:应用图标角标上显示过期待办项的数 ...

  8. 在Android上使用ZXing识别条形码/二维码

    越来越多的手机具备自动对焦的拍摄功能,这也意味着这些手机可以具备条码扫描的功能.......手机具备条码扫描的功能,可以优化购物流程,快速存储电子名片(二维码)等. 本文使用ZXing 1.6实现条码 ...

  9. MySQL · 特性分析 · 内部临时表

    http://mysql.taobao.org/monthly/2016/06/07/#rd MySQL中的两种临时表 外部临时表 通过CREATE TEMPORARY TABLE 创建的临时表,这种 ...

  10. 性能监视器(SSAS)

    使用性能监视器,您可以通过性能计数器监视 Microsoft SQL Server Analysis Services (SSAS) 实例的性能. 性能监视器是用于跟踪资源使用情况的 Microsof ...