【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 ...
随机推荐
- 二、添加 Insert into
文档目录 开始使用 初始化查询实例: LambdaToSql.SqlClient DB = new LambdaToSql.SqlClient(); 添加实体数据 ", IP = &quo ...
- datagrid 新增,并行内编辑,提交保存
<a class="mini-button" iconCls="icon-add" onclick="addRow()" plain= ...
- CSS position 笔记+实验
目录: 1.static 2.relative 3.absolute 4.fixed 5.实验:static, relative, absolute中,父元素-子元素高度关系 6.z-index 7. ...
- 【转】H.264RTP封包原理
原文地址:H.264RTP封包原理 作者:cnp11 1. 引言 随着信息产业的发展,人们对信息资源的要求已经逐渐由文字和图片过渡到音频和视频,并越来越强调获取资源的实时性和互动性.但人们又面 ...
- MySQL 的索引优化
索引类似大学图书馆建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左右性能开始逐渐下降,虽然官方文档说500~800w记录,所以大数据量建立索引是非常有必要的.My ...
- 微信公众号网页授权登录--JAVA
网上搜资料时,网友都说官方文档太垃圾了不易看懂,如何如何的.现在个人整理了一个通俗易懂易上手的,希望可以帮助到刚接触微信接口的你. 请看流程图!看懂图,就懂了一半了: 其实整体流程大体只需三步:用户点 ...
- SOFA 源码分析 —— 服务引用过程
前言 在前面的 SOFA 源码分析 -- 服务发布过程 文章中,我们分析了 SOFA 的服务发布过程,一个完整的 RPC 除了发布服务,当然还需要引用服务. So,今天就一起来看看 SOFA 是如何引 ...
- c++11线程池
#pragma once #include <future> #include <vector> #include <atomic> #include <qu ...
- HTML学习笔记6:列表标签
列表标签 什么是列表标签呢? 以平台区分有什么游戏? 手游 pc游戏 家用机游戏 掌机游戏 以游戏类型区分有什么游戏? RPG ARPG MMORPG ACT FPS 以上两种就是一种列表标签 ...
- HTML学习笔记4:文档申明和编码标签
①文档申明 作用:为了使浏览器更好的显示HTML文件,必须告知浏览器你的文件为HTML 语法:<!DOCTYPE html> 声明必须放在HTML文档第一行 声明不是HTM ...