【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 ...
随机推荐
- html5中的全局属性
在html5中,新增了一个"全局属性"的概念,所谓全局属性,是指可以对任何属性都使用的属性.下面列出常用的全局属性. 1.contentEditable属性,是微软开发的,该属性主 ...
- AngularJs 指令directive之require
controller的用法分为两种情形,一种是require自定义的controller,由于自定义controller中的属性方法都由自己编 写,使用起来比较简单:另一种方法则是require An ...
- Spring Aop中,获取被代理类的工具
在实际应用中,顺着过去就是一个类被代理.反过来,可能需要逆向进行,拿到被代理的类,实际工作中碰到了,就拿出来分享下. /** * 获取被代理类的Object * @author Monkey */ p ...
- CAN数据格式-ASC
Vector工具录制的数据,一般有ASC和BLF两种格式,本文介绍ASC. 1. ASC定义 ASC(ASCII)即文本文件,数据已可视化的文本存储. 2.ASC查看 通常情况下,用记事本就可以打开. ...
- 【转载】tomcat+nginx+redis实现均衡负载、session共享(二)
今天我们接着说上次还没完成session共享的部分,还没看过上一篇的朋友可以先看下上次内容,http://www.cnblogs.com/zhrxidian/p/5432886.html. 1.red ...
- A million requests per second with Python
https://medium.freecodecamp.com/million-requests-per-second-with-Python-95c137af319 Is it possible t ...
- 日常踩坑笔记:spring的context:property-placeholder标签
背景: 原来的项目一直跑着没有问题,今天突然想在原有项目的基础上,加上redis进行数据的缓存,原来项目的架构就是传统的SSM框架,于是,大刀阔斧的开始改装了... 编写redis的配置文件——red ...
- SOFA 源码分析 — 自动故障剔除
前言 集群中通常一个服务有多个服务提供者.其中部分服务提供者可能由于网络,配置,长时间 fullgc ,线程池满,硬件故障等导致长连接还存活但是程序已经无法正常响应.单机故障剔除功能会将这部分异常的服 ...
- tensorflow1.0.0 弃用了几个operator写法
除法和取模运算符(/, //, %)现已匹配 Python(flooring)语义.这也适用于 tf.div 和 tf.mod.为了获取强制的基于整数截断的行为,你可以使用 tf.truncatedi ...
- 机器学习,流式IoT和医疗设备互联
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 让我们来看一下机器学习是如何应用于医护行业以及如何借助Apache Spark对患者的监控数据进行处理 现如今,IoT数据,实时流式数据分析 ...