题目链接:

      TP

题解:

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

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

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

代码: 

  

 #define Troy 

 #include <bits/stdc++.h>

 using namespace std;

 inline int read(){
int s=,k=;char ch=getchar();
while(ch<''|ch>'') ch=='-'?k=-:,ch=getchar();
while(ch>&ch<='') s=s*+(ch^),ch=getchar();
return s*k;
} const int N=; int n,m,a[N]; struct node {
int l,r,val;
friend bool operator <(node x,node y){
return x.val!=y.val?x.val<y.val:(x.l!=y.l?x.l>y.l:x.r>y.r);
}
friend node operator +(node x,node y){
node z;
z.l=min(x.l,y.l);z.r=max(x.r,y.r);
z.val=x.val+y.val;return z;
}
inline void out(){printf("%d %d %d\n",l,r,val);}
}; struct Tree{
node prefix,suffix,middle,section;
Tree *lc,*rc;
}*root,tree[N*],*ans;int cnt; inline void update( Tree *u){
u->middle=u->lc->suffix+u->rc->prefix;
u->prefix=max(u->lc->prefix,u->lc->section+u->rc->prefix);
u->suffix=max(u->rc->suffix,u->rc->section+u->lc->suffix);
u->section=u->lc->section+u->rc->section;
u->middle=max(u->middle,max(u->lc->middle,max(u->rc->middle,max(u->prefix,u->suffix))));
} inline void build(Tree *&u,int l,int r){
u=tree+cnt;++cnt;
if(l==r){
u->prefix=u->suffix=u->middle=u->section=(node){l,r,a[l]};
return ;
}
int mid=l+r>>;
build(u->lc,l,mid);
build(u->rc,mid+,r);
update(u);
} inline void query(Tree *u,int l,int r,int x,int y){
if(x<=l&&r<=y){
if(ans==NULL) ans=u;
else{
Tree *now=ans;ans=tree+cnt,++cnt;
ans->lc=now,ans->rc=u;
update(ans);
}
return ;
}
int mid=l+r>>;
if(x<=mid) query(u->lc,l,mid,x,y);
if(y>mid) query(u->rc,mid+,r,x,y);
} int main(){
freopen("hill.in","r",stdin);
freopen("hill.out","w",stdout);
n=read(),m=read();
for(int i=;i<=n;++i) a[i]=read();
build(root,,n);
for(int i=;i<=m;++i){
int l=read(),r=read();
ans=NULL;query(root,,n,l,r);
ans->middle.out();
}
}

【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. CSS3实现多样的边框效果

    半透明边框 实现效果: 实现代码: <div> 你能看到半透明的边框吗? </div> div { /* 关键代码 */ border: 10px solid rgba(255 ...

  2. linux系统安装mysql数据库

    1.首先关闭linux的防火墙,执行命令 chkconfig iptables off 2.从mysql官网上下载自己适合的mysql版本https://dev.mysql.com/downloads ...

  3. 怎么分别javascript写在<head>里还是<body>里面?

    怎么分别javascript写在<head>里还是<body>里面? 具体哪些语句写在<body>里,哪些语句写在<head>里 满意答案 BeginN ...

  4. Java编程语言下Selenium 鼠标悬停以及右击操作

    // 基于Actions类创建一个对象 Actions action = new Actions(driver); // 鼠标悬停在药渡公司全称字段上 action.moveToElement(Yao ...

  5. vue-cli目录结构

  6. AQS分析(AbstractQueuedSynchronizer)(三)

    1.AQS是什么 AQS同步器是Java并发编程的基础,从资源共享的角度分成独占和共享两种模式,像ReentrantLock.ThreadPoolExecutor.CountDownLatch等都是基 ...

  7. python3学习笔记2---引用http://python3-cookbook.readthedocs.io/zh_CN/latest/2

    2018-03-01数据结构和算法(2) 1.6字典中的键映射多个值 一个字典就是一个键对应一个单值的映射.如果你想要一个键映射多个值,那么你就需要将这多个值放到另外的容器中, 比如列表或者集合里面. ...

  8. Visual Studio Live Share不完全指北

    Visual Studio Live Share是什么? 是VS的一个实时协作开发的扩展工具. github地址:https://github.com/MicrosoftD...文档地址:https: ...

  9. JAVAEE——BOS物流项目13:Quartz概述、创建定时任务、使用JavaMail发送邮件、HighCharts概述、实现区域分区分布图

    1 学习计划 1.Quartz概述 n Quartz介绍和下载 n 入门案例 n Quartz执行流程 n cron表达式 2.在BOS项目中使用Quartz创建定时任务 3.在BOS项目中使用Jav ...

  10. PyQuery详解

    1.What is Pyquery? 答:灵活强大的网页解析库 2.安装: pip3 install pyquery 3.基本使用 初始化操作: 前言:在介绍之前小伙伴们我们先来了解下CSS的基本语法 ...