P2894 [USACO08FEB]酒店Hotel

参考样例,第一行输入n,m ,n代表有n个房间,编号为1---n,开始都为空房,m表示以下有m行操作,以下 每行先输入一个数 i ,表示一种操作:

若i为1,表示查询房间,再输入一个数x,表示在1--n 房间中找到长度为x的连续空房,输出连续x个房间中左端的房间号,尽量让这个房间号最小,若找不到长度为x的连续空房,输出0。

若i为2,表示退房,再输入两个数 x,y 代表 房间号 x---x+y-1 退房,即让房间为空。

题目描述有坑啊。。。 一开始不知道操作一之后要区间覆盖为一,整的我tag只写了0的情况。。。

其实自己脑子有坑。。。 不覆盖为一这题还做啥。。。

头一次感觉自己代码写短了啊。。。

话说我的线段树结构体名字也成了SGT了。。吉利吉丽emmm

查询最左端的值,那么在查询的时候递归按照左中右的顺序找就可以了。

code:

#include <iostream>
#include <cstdio> #define ls(o) o<<1
#define rs(o) o<<1|1 using namespace std; const int wx=50017; inline int read(){
int sum=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
return sum*f;
} int n,m; struct SGT{
int l,r,tag,lsum,rsum,sum;
#define tag(o) t[o].tag
#define lsum(o) t[o].lsum
#define rsum(o) t[o].rsum
#define sum(o) t[o].sum
}t[wx*4]; void up(int o){
if(sum(ls(o))==t[ls(o)].r-t[ls(o)].l+1)lsum(o)=sum(ls(o))+lsum(rs(o));
else lsum(o)=lsum(ls(o));
if(sum(rs(o))==t[rs(o)].r-t[rs(o)].l+1)rsum(o)=sum(rs(o))+rsum(ls(o));
else rsum(o)=rsum(rs(o));
sum(o)=max(max(sum(ls(o)),sum(rs(o))),rsum(ls(o))+lsum(rs(o)));
} void down(int o){
if(tag(o)==0){
sum(ls(o))=lsum(ls(o))=rsum(ls(o))=t[ls(o)].r-t[ls(o)].l+1;
sum(rs(o))=lsum(rs(o))=rsum(rs(o))=t[rs(o)].r-t[rs(o)].l+1;
tag(ls(o))=tag(rs(o))=tag(o);
tag(o)=-1;
}
if(tag(o)==1){
sum(ls(o))=lsum(ls(o))=rsum(ls(o))=0;
sum(rs(o))=lsum(rs(o))=rsum(rs(o))=0;
tag(ls(o))=tag(rs(o))=tag(o);
tag(o)=-1;
}
} void build(int o,int l,int r){
t[o].l=l; t[o].r=r; tag(o)=-1;
if(l==r){sum(o)=lsum(o)=rsum(o)=1; return ;}
int mid=t[o].l+t[o].r>>1;
if(l<=mid)build(ls(o),l,mid);
if(r>mid)build(rs(o),mid+1,r);
up(o);
} void update(int o,int l,int r,int k){
if(l<=t[o].l&&t[o].r<=r){
if(!k){
sum(o)=lsum(o)=rsum(o)=t[o].r-t[o].l+1;
tag(o)=k;
}
else{
sum(o)=lsum(o)=rsum(o)=0;
tag(o)=k;
}
return ;
}
down(o);
int mid=t[o].l+t[o].r>>1;
if(l<=mid)update(ls(o),l,r,k);
if(r>mid)update(rs(o),l,r,k);
up(o);
} int query(int o,int l,int r,int len){
if(l==r)return l;
down(o);
if(t[ls(o)].sum>=len)return query(ls(o),l,r,len);
else if(t[ls(o)].rsum+t[rs(o)].lsum>=len)return t[ls(o)].r-rsum(ls(o))+1;
else return query(rs(o),l,r,len);
} int main(){
n=read(); m=read(); build(1,1,n);
for(int i=1;i<=m;i++){
int opt;
opt=read();
if(opt==1){
int x;
x=read();
if(sum(1)<x){
puts("0");
continue;
}
int tmp=query(1,1,n,x);
printf("%d\n",tmp);
update(1,tmp,tmp+x-1,1);
}
else{
int x,y;
x=read(); y=read();
update(1,x,x+y-1,0);
}
}
return 0;
}

线段树【洛谷P2894】 [USACO08FEB]酒店Hotel的更多相关文章

  1. 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel

    https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...

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

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

  3. 洛谷P2894 [USACO08FEB]酒店Hotel

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

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

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

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

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

  6. 洛谷 P2894 [USACO08FEB]酒店Hotel

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

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

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

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

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

  9. 洛谷 P2894 [USACO08FEB]酒店

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

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

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

随机推荐

  1. 基于候选区域的深度学习目标检测算法R-CNN,Fast R-CNN,Faster R-CNN

    参考文献 [1]Rich feature hierarchies for accurate object detection and semantic segmentation [2]Fast R-C ...

  2. oracle connect by用法篇 (包括树遍历)之二

    3.2查询当前时间往前的12周的开始时间.结束时间.第多少周 , ) - (rownum ) as startDate, sysdate , 'd'))) - (rownum ) as endDate ...

  3. 通过在Oracle子表外键上建立索引提高性能

    根据我的经验,导致死锁的头号原因是外键未加索引(第二号原因是表上的位图索引遭到并发更新).在以下两种情况下,Oracle在修改父表后会对子表加一个全表锁: 1)如果更新了父表的主键(倘若遵循关系数据库 ...

  4. 窗体的keypreview属性的作用是什么?(设置快捷键和钩子)

    如果把窗体的KeyPreview属性设为True,那么窗体将比其内的控件优先获得键盘事件的激活权.比如窗体Form1和其内的文本框Text1都准备响应KeyPress事件,那么以下代码将首先激活窗体的 ...

  5. java,js判断全角半角

    function chkHalf(str){ for(var i=0;i { strCode=str.charCodeAt(i); if((strCode>65248)||(strCode==1 ...

  6. 安装完Ubuntu 14.04后的几件事

    周末折腾一下Ubuntu 14.04,稍作记录: 1. 切换源,我还是钟情163的(ps, 这里如果用的以前13.04时候163的源会有问题,记得配置最新的http://mirrors.163.com ...

  7. AWT简介

    -------------siwuxie095                         AWT 简介:     AWT(Abstract Window Toolkit)是最原始的 Java G ...

  8. go build 命令

    go build命令简介 1.用于编译源码文件或代码包 2.编译非命令源码文件不会产生任何结果文件 3.编译命令源码文件会在该命令的执行目录中生成一个可执行文件 4.执行该命令切不追加任何参数时,他会 ...

  9. repeater的command事件用法

    当Repeater里面循环控件时就会用到command, 是Repeater控件的原生事件用法 Repeater里面如果循环控件,控件的ID是会被改变的 repeater.itemcommand+= ...

  10. 30-盐水(分段dfs)

    链接:https://www.nowcoder.com/acm/contest/94/K来源:牛客网 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 131072K,其他语言2621 ...