题目链接:

      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. 细说Web页面与本地电脑通讯

    话说在很久很久以前.Web页面与客户的本地电脑Localhost通讯,有两种方式: 1.Flash 2.ActiveX控件 由于Flash本人不是很了解,也给出不了什么示例代码, 对于ActiveX控 ...

  2. CALayer的属性和使用

    一.CALayer的常用属性 1.@propertyCGPoint position; 图层中心点的位置,类似与UIView的center:用来设置CALayer在父层中的位置:以父层的左上角为原点( ...

  3. 关于web页面JApplet打印小票

    版权所有 做这个的例子太少,我把我做的示例亮出来 一.先说说需要的版本 1.我用的浏览器只有ie: 火狐只支持52版本以下,并且是java7.java8.chrome不支持 2.applet客户端打印 ...

  4. JFace dailog button事件中刷新透视图异常 Trying to execute the disabled command org.eclipse.ui.window.closePerspective

    报错的代码为 protected void buttonPressed(int buttonId) { Display.getDefault().syncExec(new Runnable() { p ...

  5. Java---SSH(MVC)面试题

    1.        谈谈你mvc的理解 MVC是Model-View-Controler的简称.即模型-视图-控制器.MVC是一种设计模式,它强制性的把应用程序的输入.处理和输出分开. MVC中的模型 ...

  6. Java Selenium 定位元素 实现的一个注册功能

    import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Alert; impor ...

  7. Mac Launchpad出现两个相同快捷方式的解决办法

    进入以下目录 ~/Library/Application Support/Dock 把里面的.db文件删掉,然后注销重新登录即可.    

  8. Heap

    #include using namespace std; int heap[100010],cnt=0; void put(int x) { cnt++; heap[cnt]=x; int now= ...

  9. RabbitMQ windows安装(一 )

    RabbitMQ 简单介绍: 是可以实现应用程序的解耦和异步,也可用作消息缓冲和消息分发的消息队列(MQ): 安装: 1.安装RabbitMQ前先安装Erlang语言开发包,下载地址:http://w ...

  10. MySQL触发器在PHP项目中用来做信息备份、恢复和清空的方法介绍

    案例:通过PHP后台代码可以将员工的信息删除,将删除的员工信息进行恢复(类似于从回收站中恢复员工信息),并且还可以将已经删除的员工进行清空(类似于清空回复站的功能). 思路:要有一张员工表,还要有一张 ...