区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel
https://www.luogu.org/problem/P2894
#include<cstdio>
#include<iostream>
using namespace std;
struct ben
{
int lmax,rmax,len,sum,lz;
}tr[400005];
void bt(int x,int l,int r)//建树
{
tr[x].lz=0;//标记清空
tr[x].sum=tr[x].len=tr[x].lmax=tr[x].rmax=r-l+1;//区间连续,区间长度,左端点连续,右端点连续都初始化成区间的长度。(因为所有房间都是空的
if(l==r)//如果到了叶子节点
{
return ;//返回
}
int mid=(l+r)/2;
bt(x*2,l,mid);
bt(x*2+1,mid+1,r);
//左右建子节点
}
void down(int x)//标记下放
{
if(tr[x].lz==0)return ;//如果没有标记,直接返回
if(tr[x].lz==1)//如果是要开房
{
tr[x*2].lz=tr[x*2+1].lz=1;//左右子节点都要标记上开房
tr[x*2].rmax=tr[x*2].lmax=tr[x*2].sum=0;
tr[x*2+1].rmax=tr[x*2+1].lmax=tr[x*2+1].sum=0;
//左右子节点因为已经开房而没有空房都变成0
}
if(tr[x].lz==2)//如果要退房
{
tr[x*2].lz=tr[x*2+1].lz=2;////左右子节点都要标记上退房
tr[x*2].rmax=tr[x*2].lmax=tr[x*2].sum=tr[x*2].len;
tr[x*2+1].rmax=tr[x*2+1].lmax=tr[x*2+1].sum=tr[x*2+1].len;
//此区间已经全为空房,所有直接都等于len即可
}
tr[x].lz=0;//要把父节点的标记清空
}
void renew(int x)//更新节点信息
{
if(tr[x*2].sum==tr[x*2].len)//如果左子节点全为空房
{
tr[x].lmax=tr[x*2].sum+tr[x*2+1].lmax;//那么父节点的左端最大连续为左子区间加上右子区间从左的最大连续
}
else
{
tr[x].lmax=tr[x*2].lmax;//否则就是沿用左从左
}
if(tr[x*2+1].sum==tr[x*2+1].len)//右边同理
{
tr[x].rmax=tr[x*2+1].sum+tr[x*2].rmax;
}
else
{
tr[x].rmax=tr[x*2+1].rmax;
}
tr[x].sum=max(max(tr[x*2].sum,tr[x*2+1].sum),tr[x*2].rmax+tr[x*2+1].lmax);//最大连续的是左区间或右区间或左+右中最大的
}
void change_interval(int x,int l,int r,int tag,int L,int R)//区间修改,tag=1表示开房,tag=2表示退房
{
down(x);//下放懒标记
if(L<=l&&r<=R)//当前要查`询的区间如果在区间内
{
if(tag==1)
{
tr[x].sum=tr[x].lmax=tr[x].rmax=0;//开房就没有连续
}
else
{
tr[x].sum=tr[x].lmax=tr[x].rmax=tr[x].len;//退房就全是连续
}
tr[x].lz=tag;//打个懒标记
return ; //别忘返回,因为在整个查询区间内
}
int mid=(l+r)/2;
if(L<=mid)change_interval(x*2,l,mid,tag,L,R);
if(R>mid)change_interval(x*2+1,mid+1,r,tag,L,R);
renew(x);//更新当前节点 信息
}
int ask(int x,int l,int r,int ans)//查询有没有那么多连续的空房间
{
down(x);//下放懒标记
if(l==r)return l;//到叶子了,直接反回
int mid=(l+r)/2;
if(tr[x*2].sum>=ans)return ask(x*2,l,mid,ans);//如果左边就够用了 ,那么去左边找 (不要忘了是return !!!!!!!
if(tr[x*2].rmax+tr[x*2+1].lmax>=ans)return mid-tr[x*2].rmax+1;//否则左+右够了,那么就找左加右那个端点
return ask(x*2+1,mid+1,r,ans);//再否则就只能在右区间找了
}
int main()
{
int n,m,opt,x,y;
scanf("%d%d",&n,&m);
bt(1,1,n);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&opt,&x);
if(opt==1)
{
if(tr[1].sum>=x)
{
int left=ask(1,1,n,x);
printf("%d\n",left);
change_interval(1,1,n,1,left,left+x-1);
}
else
{
printf("0\n");
}
}
else
{
scanf("%d",&y);
change_interval(1,1,n,2,x,x+y-1);
}
}
return 0;
}
区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel的更多相关文章
- 洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告
P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...
- 洛谷P2894 [USACO08FEB]酒店Hotel
P2894 [USACO08FEB]酒店Hotel https://www.luogu.org/problem/show?pid=2894 题目描述 The cows are journeying n ...
- 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]
题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...
- 洛谷P2894[USACO08FEB]酒店Hotel(线段树)
问题描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel
题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治
P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...
- 浅谈线段树 (例题:[USACO08FEB]酒店Hotel)By cellur925
今天我们说说线段树. 我个人还是非常欣赏这种数据结构的.(逃)因为它足够优美,有递归结构,有左子树和右子树,还有二分的思想. emm这个文章打算自用,就不写那些基本的操作了... 1° 简单的懒标记( ...
- 洛谷 P2894 [USACO08FEB]酒店
题目描述 用线段树维护三个值:区间最长空位长度,从左端点可以延伸的最长空位长度,从右端点可以延伸的最长空位长度. #include<complex> #include<cstdio& ...
- 洛谷P2894 [USACO08FEB]酒店Hotel_区间更新_区间查询
Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...
随机推荐
- 1. Spark基础解析
1.1 Spark概述 1.1.1 什么是Spark 官网:http://spark.apache.org Spark是一种快速.通用.可扩展的大数据分析引擎,2009年诞生于加州大学伯克利分校AMP ...
- hibernate注解(自动建表如何有表DDL注释) -- Comment用法
import java.io.Serializable; import java.sql.Date; import java.sql.Timestamp; import javax.persisten ...
- Git pull记住密码
在使用https git拉取代码时,每次git pull的时候都会让输入用户名和密码 进入项目目录 命令:git config --global credential.helper store 然后会 ...
- Nuget & VS Plugin
VS Plugin vsCode:https://marketplace.visualstudio.com/vscode vs:https://marketplace.visualstudio.com ...
- apache 防盗链
方法1:Apache防盗链的第一种实现方法,可以用rewrite实现 (1.)首先要确认Apache的rewrite module可用,打开 httpd.conf 文件,如果前面有注释去掉 LoadM ...
- Bash基础——printf
简介 printf将参数插入到用户定义的文本字符串中,从而创建格式化的输出.printf将格式化的字符串输出到标准输出.printf命令根源是C语言下面的printf函数,就连名字都一样,很多用法也是 ...
- vi编辑器简介
vi编辑器是Linux和Unix上最基本的文本编辑器,工作在字符模式下.由于不需要图形界面,vi是效率很高的文本编辑器.尽管在Linux上也有很多图形界面的编辑器可用,但vi在系统和服务器管理中的功能 ...
- oj.zstu 4421交税(合数分解成素数)
题目 题意:T组,每一组输入一个数X, 求X最少能分成几个素数的和,输出. 思路: 对于一个大于2的偶数,由哥德巴赫猜想,一定能分成2个素数. 对于一个奇数来说,一定能分成2个或者3个素数之和.如果 ...
- 关于TCP/IP协议的记录
本博客是个人随笔,只是记录自己的学习过程.
- Mybatis二级缓存的简单应用
1.接口 public interface MemberMapperCache { public Members selectMembersById(Integer id); } 2.POJO类 实现 ...