【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 ...
随机推荐
- 细说Web页面与本地电脑通讯
话说在很久很久以前.Web页面与客户的本地电脑Localhost通讯,有两种方式: 1.Flash 2.ActiveX控件 由于Flash本人不是很了解,也给出不了什么示例代码, 对于ActiveX控 ...
- CALayer的属性和使用
一.CALayer的常用属性 1.@propertyCGPoint position; 图层中心点的位置,类似与UIView的center:用来设置CALayer在父层中的位置:以父层的左上角为原点( ...
- 关于web页面JApplet打印小票
版权所有 做这个的例子太少,我把我做的示例亮出来 一.先说说需要的版本 1.我用的浏览器只有ie: 火狐只支持52版本以下,并且是java7.java8.chrome不支持 2.applet客户端打印 ...
- JFace dailog button事件中刷新透视图异常 Trying to execute the disabled command org.eclipse.ui.window.closePerspective
报错的代码为 protected void buttonPressed(int buttonId) { Display.getDefault().syncExec(new Runnable() { p ...
- Java---SSH(MVC)面试题
1. 谈谈你mvc的理解 MVC是Model-View-Controler的简称.即模型-视图-控制器.MVC是一种设计模式,它强制性的把应用程序的输入.处理和输出分开. MVC中的模型 ...
- Java Selenium 定位元素 实现的一个注册功能
import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Alert; impor ...
- Mac Launchpad出现两个相同快捷方式的解决办法
进入以下目录 ~/Library/Application Support/Dock 把里面的.db文件删掉,然后注销重新登录即可.
- Heap
#include using namespace std; int heap[100010],cnt=0; void put(int x) { cnt++; heap[cnt]=x; int now= ...
- RabbitMQ windows安装(一 )
RabbitMQ 简单介绍: 是可以实现应用程序的解耦和异步,也可用作消息缓冲和消息分发的消息队列(MQ): 安装: 1.安装RabbitMQ前先安装Erlang语言开发包,下载地址:http://w ...
- MySQL触发器在PHP项目中用来做信息备份、恢复和清空的方法介绍
案例:通过PHP后台代码可以将员工的信息删除,将删除的员工信息进行恢复(类似于从回收站中恢复员工信息),并且还可以将已经删除的员工进行清空(类似于清空回复站的功能). 思路:要有一张员工表,还要有一张 ...