【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 ...
随机推荐
- Windows7安装Pygame软件
1. 工具包下载地址: Python下载地址: https://www.python.org/downloads/ pip下载地址:https://pypi.org/project/pip/#file ...
- 使用微服务架构思想,设计部署OAuth2.0授权认证框架
1,授权认证与微服务架构 1.1,由不同团队合作引发的授权认证问题 去年的时候,公司开发一款新产品,但人手不够,将B/S系统的Web开发外包,外包团队使用Vue.js框架,调用我们的WebAPI,但是 ...
- Java 理论与实践: 并发集合类
Java 理论与实践: 并发集合类 DougLea的 util.concurrent 包除了包含许多其他有用的并发构造块之外,还包含了一些主要集合类型 List 和 Map 的高性能的.线程安全的实现 ...
- js 读取xml文件
读取xml文件 [原创 2007-6-20 17:35:37] 字号:大 中 小 js中读取xml文件,简单的例子: <html><head><script> ...
- Python-Web框架之 - 利用SQLALchemy创建与数据库MySQL的连接, 详解用Flask时会遇到的一些大坑 !
经过这个小项目算是对Django与Flask这两个web框架有了新的认识 , Django本身的轮子非常齐全 , 套路也很固定 , 新手在接触Django框架时 , 不会陷入到处找轮子的大坑 ; 那么 ...
- Windows 2003 Server 标准版启动问题解决(资源转贴)
维护的系统之一是部署在windows2003 Server标准版的服务器上,可能是由于某个应用问题,导致远程重启失败,害得我在机房呆了一早晨,可算是够折腾的.最后按照官方文档解决,刚放文档地址是:ht ...
- Android集成ffmpeg
1.ffmpeg官网文档地址:https://trac.ffmpeg.org/wiki/CompilationGuide/Android 2.上面页面资源列表里面第一项 https://github. ...
- ubuntu 18.04安装docker以及docker内配置neo4j
如题 切换到root用户下 apt install docker.io 等啊等,很快,就好了.. 如图 即可使用 如果出现Cannot connect to the Docker daemon at ...
- 解决0% [Waiting for headers] 导致的unable to lock the administration directory (/var/lib/dpkg/) is another process using it
这是我在配置vim的YouCompleteMe时遇到的问题,我需要使用CMake来编译YCM. 在我输入 $ sudo apt install cmake 由于网络原因导致安装一直卡在0% [Wait ...
- 学习Android过程中的一些博客或工具收集
android studio中使用SlidingMenu: 超简单Android Studio导入第三方库(SlidingMenu)教程绝对傻瓜版 android 更新sdk23以后,报错提示Floa ...