【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 ...
随机推荐
- OpenShift实战(七):OpenShift定制镜像S2I
1.基础镜像制作 由于公司的程序是Java开发,上线发布使用的是maven,如果使用openshift自带的S2I,每次都会全量拉取代码(代码比较多,每次全量拉太慢),然后每次打包都会再一次下载mav ...
- ORACLE之TO_DATE (转载)
转自 http://www.cnblogs.com/anran_guojianjun/archive/2009/09/11/1564535.html 一.在使用Oracle的to_date函数来做日期 ...
- 完美解决ScollView内嵌ListView的问题
1.之前看了别人的一代码,解决办法是自己定制一个ListView,代码如下: public class NoScrollListView extends ListView { public NoScr ...
- gitolite服务器部署中的一些坑
1.秘钥登录问题可参考< 安装gitolite,并ssh公钥无密码登录>http://www.cnblogs.com/tr0217/p/4517952.html,该文中推荐了阮一峰的< ...
- JDK8安装时错误1335的解决
Win7安装JDK8 update65版本时,碰到错误1335,错误信息大概是一个cab文件损坏了,搜索了一下,有网站提供这个错误的修补工具,不过最终我没有下载这个工具,说是系统问题,但工具不是MS官 ...
- Python的易错点
一.列表和元组的区别 列表是允许修改的,而元组是不能修改的,元组只能实现拼接,形成一个新的元组.两者可以实现相互转换,列表转换成元组使用truple函数,而元组转换成列表使用list函数. 二.Raw ...
- PHP $_FILES函数详解
原创 转载请注明出处! 先来看一段代码 <form enctype="multipart/form-data" action="upload.php" m ...
- centOS7虚拟环境搭建
今天来记录一下使用WMware虚拟机来搭建centOS虚拟机的过程. 本次使用工具为VMware Workstation 14 Pro,可以从https://www.vmware.com/来获取所需工 ...
- IAAS-libvirt介绍。
Libvirt介绍 Libvirt与hypervisor无关,其提供与多种操作系统虚拟化能力进行交互的API与工具库. Libvirt提供了一个通用稳定的抽象层,可以安全的操作物理机上的虚拟机,同时为 ...
- vue.js常见的报错信息及其解决方法的记录
1.Vue packages version mismatch 翻译:vue包版本匹配错误 报错样例: 报错原因:通常出现于一些依赖库的更新或者安装新的依赖库之后(可以认为npm update已经成为 ...