【cogs 775】山海经 ——Segment Tree
题目链接:
题解:
我数据结构真心是弱啊= =。
线段树好厉害啊,一直不会区间最大连续和,今天刚学习了一下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的更多相关文章
- COGS 775 山海经
COGS 775 山海经 思路: 求最大连续子段和(不能不选),只查询,无修改.要求输出该子段的起止位置. 线段树经典模型,每个节点记录权值和sum.左起最大前缀和lmax.右起最大后缀和rmax.最 ...
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- 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), ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
- Lintcode: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
随机推荐
- 新手入门vue 使用vue-cli创建项目
本文是针对对于完全没有了解过vue 和npm,连运行环境和项目构建的都不会的小白,对于前端老司机的就不用看了,浪费时间. 使用npm 与vue-cli 构建vue 项目 第一步:安装运行环境(node ...
- iOS开发常用第三方库
UI 动画 网络相关 Model 其他 数据库 缓存处理 PDF 图像浏览及处理 摄像照相视频音频处理 响应式框架 消息相关 版本新API的Demo 代码安全与密码 测试及调试 AppleWatch ...
- 7. Selenium的基本使用
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 32.0px "PingFang SC" } span.s1 { font: 32.0p ...
- 使用commons-compress操作zip文件(压缩和解压缩)
http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...
- 基于JS的WEB会议室预订拖拽式图形界面的实现
06年的一篇blog,转到这个博客上: 很早之前写的,后来由于这个功能模块取消,最终没有上线,所以与Server交互的那部分还没有写,不过那部分方案我也已经出来了,而且现在客户端这一部分已经通过了比较 ...
- mysql经典面试题
数据库优化:这个优化法则归纳为5个层次:1. 减少数据访问(减少磁盘访问)2. 返回更少数据(减少网络传输或磁盘访问)3. 减少交互次数(减少网络传输)4. 减少服务器CPU开销(减少CPU及内存开销 ...
- .Net中stirng转Systen.Type的一种实现思路
今天在上班的过程中,许长时间未联系的大学小伙伴发来消息,带着一个疑问来找我. 他的需求是type动态添加,这对我来说当然很easy,用泛型就好了, 随后,手起刀落,Demo就写出来,如下: 写了一个方 ...
- Series
Series是一种类似于一维数组的对象,是由一维数据(各种Numpy数据类型)以及一组与之相关的数据标签(即索引)组成. In [1]: from pandas import Series In [2 ...
- https://doc.opensuse.org/projects/kiwi/doc/
KIWI 是用于创建操作系统映像的系统.映像是带有一个文件的目录,该文件包含操作系统.其应用程序与配置.操作系统的文件系统结构.可能的附加元数据,以及(取决于映像类型)磁盘几何属性和分区表数据.通过 ...
- Linux Vim配置
用过很多vim配置的版本,怎么说,想轻量级就不要胡加乱七八糟的功能:如果不在乎反应是不是快速,侧重功能是否强大,可以参考vim大神的配置策略(spf13-vim)https://github.com/ ...