题目链接:

      TP

题解:

    我数据结构真心是弱啊= =。

  线段树好厉害啊,一直不会区间最大连续和,今天刚学习了一下233。

  维护前缀最大和后缀最大,越界最大(?),再维护一个区间最大,瞎搞搞就好了,RE了一遍233。

代码: 

  

  1. #define Troy
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. inline int read(){
  8. int s=,k=;char ch=getchar();
  9. while(ch<''|ch>'') ch=='-'?k=-:,ch=getchar();
  10. while(ch>&ch<='') s=s*+(ch^),ch=getchar();
  11. return s*k;
  12. }
  13.  
  14. const int N=;
  15.  
  16. int n,m,a[N];
  17.  
  18. struct node {
  19. int l,r,val;
  20. friend bool operator <(node x,node y){
  21. return x.val!=y.val?x.val<y.val:(x.l!=y.l?x.l>y.l:x.r>y.r);
  22. }
  23. friend node operator +(node x,node y){
  24. node z;
  25. z.l=min(x.l,y.l);z.r=max(x.r,y.r);
  26. z.val=x.val+y.val;return z;
  27. }
  28. inline void out(){printf("%d %d %d\n",l,r,val);}
  29. };
  30.  
  31. struct Tree{
  32. node prefix,suffix,middle,section;
  33. Tree *lc,*rc;
  34. }*root,tree[N*],*ans;int cnt;
  35.  
  36. inline void update( Tree *u){
  37. u->middle=u->lc->suffix+u->rc->prefix;
  38. u->prefix=max(u->lc->prefix,u->lc->section+u->rc->prefix);
  39. u->suffix=max(u->rc->suffix,u->rc->section+u->lc->suffix);
  40. u->section=u->lc->section+u->rc->section;
  41. u->middle=max(u->middle,max(u->lc->middle,max(u->rc->middle,max(u->prefix,u->suffix))));
  42. }
  43.  
  44. inline void build(Tree *&u,int l,int r){
  45. u=tree+cnt;++cnt;
  46. if(l==r){
  47. u->prefix=u->suffix=u->middle=u->section=(node){l,r,a[l]};
  48. return ;
  49. }
  50. int mid=l+r>>;
  51. build(u->lc,l,mid);
  52. build(u->rc,mid+,r);
  53. update(u);
  54. }
  55.  
  56. inline void query(Tree *u,int l,int r,int x,int y){
  57. if(x<=l&&r<=y){
  58. if(ans==NULL) ans=u;
  59. else{
  60. Tree *now=ans;ans=tree+cnt,++cnt;
  61. ans->lc=now,ans->rc=u;
  62. update(ans);
  63. }
  64. return ;
  65. }
  66. int mid=l+r>>;
  67. if(x<=mid) query(u->lc,l,mid,x,y);
  68. if(y>mid) query(u->rc,mid+,r,x,y);
  69. }
  70.  
  71. int main(){
  72. freopen("hill.in","r",stdin);
  73. freopen("hill.out","w",stdout);
  74. n=read(),m=read();
  75. for(int i=;i<=n;++i) a[i]=read();
  76. build(root,,n);
  77. for(int i=;i<=m;++i){
  78. int l=read(),r=read();
  79. ans=NULL;query(root,,n,l,r);
  80. ans->middle.out();
  81. }
  82. }

【cogs 775】山海经 ——Segment Tree的更多相关文章

  1. COGS 775 山海经

    COGS 775 山海经 思路: 求最大连续子段和(不能不选),只查询,无修改.要求输出该子段的起止位置. 线段树经典模型,每个节点记录权值和sum.左起最大前缀和lmax.右起最大后缀和rmax.最 ...

  2. BestCoder#16 A-Revenge of Segment Tree

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  3. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  4. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  5. Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  6. Segment Tree Query I & II

    Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...

  7. Segment Tree Build I & II

    Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...

  8. Lintcode: Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...

  9. Lintcode: Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

随机推荐

  1. html5中的全局属性

    在html5中,新增了一个"全局属性"的概念,所谓全局属性,是指可以对任何属性都使用的属性.下面列出常用的全局属性. 1.contentEditable属性,是微软开发的,该属性主 ...

  2. AngularJs 指令directive之require

    controller的用法分为两种情形,一种是require自定义的controller,由于自定义controller中的属性方法都由自己编 写,使用起来比较简单:另一种方法则是require An ...

  3. Spring Aop中,获取被代理类的工具

    在实际应用中,顺着过去就是一个类被代理.反过来,可能需要逆向进行,拿到被代理的类,实际工作中碰到了,就拿出来分享下. /** * 获取被代理类的Object * @author Monkey */ p ...

  4. CAN数据格式-ASC

    Vector工具录制的数据,一般有ASC和BLF两种格式,本文介绍ASC. 1. ASC定义 ASC(ASCII)即文本文件,数据已可视化的文本存储. 2.ASC查看 通常情况下,用记事本就可以打开. ...

  5. 【转载】tomcat+nginx+redis实现均衡负载、session共享(二)

    今天我们接着说上次还没完成session共享的部分,还没看过上一篇的朋友可以先看下上次内容,http://www.cnblogs.com/zhrxidian/p/5432886.html. 1.red ...

  6. A million requests per second with Python

    https://medium.freecodecamp.com/million-requests-per-second-with-Python-95c137af319 Is it possible t ...

  7. 日常踩坑笔记:spring的context:property-placeholder标签

    背景: 原来的项目一直跑着没有问题,今天突然想在原有项目的基础上,加上redis进行数据的缓存,原来项目的架构就是传统的SSM框架,于是,大刀阔斧的开始改装了... 编写redis的配置文件——red ...

  8. SOFA 源码分析 — 自动故障剔除

    前言 集群中通常一个服务有多个服务提供者.其中部分服务提供者可能由于网络,配置,长时间 fullgc ,线程池满,硬件故障等导致长连接还存活但是程序已经无法正常响应.单机故障剔除功能会将这部分异常的服 ...

  9. tensorflow1.0.0 弃用了几个operator写法

    除法和取模运算符(/, //, %)现已匹配 Python(flooring)语义.这也适用于 tf.div 和 tf.mod.为了获取强制的基于整数截断的行为,你可以使用 tf.truncatedi ...

  10. 机器学习,流式IoT和医疗设备互联

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 让我们来看一下机器学习是如何应用于医护行业以及如何借助Apache Spark对患者的监控数据进行处理 现如今,IoT数据,实时流式数据分析 ...