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的更多相关文章

  1. 洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告

    P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...

  2. 洛谷P2894 [USACO08FEB]酒店Hotel

    P2894 [USACO08FEB]酒店Hotel https://www.luogu.org/problem/show?pid=2894 题目描述 The cows are journeying n ...

  3. 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]

    题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...

  4. 洛谷P2894[USACO08FEB]酒店Hotel(线段树)

    问题描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...

  5. 洛谷 P2894 [USACO08FEB]酒店Hotel

    题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...

  6. 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治

    P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...

  7. 浅谈线段树 (例题:[USACO08FEB]酒店Hotel)By cellur925

    今天我们说说线段树. 我个人还是非常欣赏这种数据结构的.(逃)因为它足够优美,有递归结构,有左子树和右子树,还有二分的思想. emm这个文章打算自用,就不写那些基本的操作了... 1° 简单的懒标记( ...

  8. 洛谷 P2894 [USACO08FEB]酒店

    题目描述 用线段树维护三个值:区间最长空位长度,从左端点可以延伸的最长空位长度,从右端点可以延伸的最长空位长度. #include<complex> #include<cstdio& ...

  9. 洛谷P2894 [USACO08FEB]酒店Hotel_区间更新_区间查询

    Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...

随机推荐

  1. FFplay源代码分析:整体流程图(仅供参考)

  2. (转)FFMPEG类库打开流媒体的方法(需要传参数的时候)

    本文链接:https://blog.csdn.net/leixiaohua1020/article/details/14215393 使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数 ...

  3. mouseover mouseleave

    mouseover在当鼠标移入元素或其子元素的时候都会触发,是一个重复触发,冒泡的过程.见下面例子,一个父元素包含一个子元素,在其父元素内滑动鼠标,超出子元素的范围时,也会触发事件. 而mouseen ...

  4. 使用parted对Linux未分区部分进行分区

    1. 使用命令parted -l 查看当前分区 可以看到硬盘有2396GB即有2.5T , 但是分区就分了50G一个盘, 需要分剩下部分 [root@localhost ~]# parted -l M ...

  5. linux安装png2icon方法

    此工具用于将png图片转换为ico格式的文件,一个小工具,但很实用 官网:http://www.winterdrache.de/freeware/png2ico/ 下载: wget http://ww ...

  6. 持久化存储之 PV、PVC、StorageClass

    PV介绍: PersistentVolume(PV)是群集中由管理员配置的一块存储. 它是集群中的资源,就像节点是集群资源一样. PV是容量插件,如Volumes,但其生命周期独立于使用PV的任何单个 ...

  7. django css

    1. settings.py最下方STATIC_URL下面补上 STATIC_URL = '/home/wjg/code/wblog/static/' STATIC_ROOT = os.path.jo ...

  8. 这些方面做好了才能叫运营-App运营日常

    现在APP的开发推广是时代的潮流,同时也是不少企业的难题.现在我们就来谈谈APP运营的一些问题. 1. 基础运营正常维护产品最日常.最普通的工作,如APP应用包在各大应用市场提交上传等,如安卓渠道,包 ...

  9. K2 BPM_采购端到端解决方案,激活合规采购新动能_十年专注业务流程管理系统

    「方案背景」企业管理标准化演进之路 企业的成长离不开标准化,企业的可持续发展更离不开标准化.随着市场竞争的日趋激烈,标准化已经成为企业参与市场竞争的战略性手段,也成为企业可持续发展的重要手段.聚焦到采 ...

  10. layui表单

    {include file="Public:inner_header" /} <link rel="stylesheet" href="__ST ...