P2894 [USACO08FEB]酒店Hotel

题目描述

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course).

The cows and other visitors arrive in groups of size Di (1 ≤ Di ≤ N) and approach the front desk to check in. Each group i requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of r to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied.

参考样例,第一行输入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 退房,即让房间为空。

输入输出格式

输入格式:

* Line 1: Two space-separated integers: N and M

* Lines 2..M+1: Line i+1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and Di (b) Three space-separated integers representing a check-out: 2, Xi, and Di

输出格式:

* Lines 1.....: For each check-in request, output a single line with a single integer r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.

输入输出样例

输入样例#1: 复制

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
输出样例#1: 复制

1
4
7
0
5

线段树区间合并

代码:

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 struct Tree{
int lch,rch,val,lazy;
}tree[maxn<<]; void pushup(int l,int r,int rt)
{
int m=(l+r)>>;
if(tree[rt<<].val==(m-l+)) tree[rt].lch=tree[rt<<].val+tree[rt<<|].lch;
else tree[rt].lch=tree[rt<<].lch;
if(tree[rt<<|].val==(r-m)) tree[rt].rch=tree[rt<<|].val+tree[rt<<].rch;
else tree[rt].rch=tree[rt<<|].rch;
tree[rt].val=max(max(tree[rt<<].val,tree[rt<<|].val),tree[rt<<].rch+tree[rt<<|].lch);
} void pushdown(int l,int r,int rt)
{
int m=(l+r)>>;
if(tree[rt].lazy){
if(tree[rt].lazy==){//空房
tree[rt<<].lazy=tree[rt<<|].lazy=tree[rt].lazy;
tree[rt<<].lch=tree[rt<<].rch=tree[rt<<].val=m-l+;
tree[rt<<|].lch=tree[rt<<|].rch=tree[rt<<|].val=r-m;
}
else if(tree[rt].lazy==){//住满
tree[rt<<].lazy=tree[rt<<|].lazy=tree[rt].lazy;
tree[rt<<].lch=tree[rt<<].rch=tree[rt<<].val=;
tree[rt<<|].lch=tree[rt<<|].rch=tree[rt<<|].val=;
}
tree[rt].lazy=;
}
} //两种build的方式,直接在判断前面写,就不需要pushup,写在判断里面就需要pushup,上传到爸爸
void build(int l,int r,int rt)
{
tree[rt].lazy=;
if(l==r){
tree[rt].lch=tree[rt].rch=tree[rt].val=;
return ;
} int m=(l+r)>>;
build(lson);
build(rson);
pushup(l,r,rt);
} //void build(int l,int r,int rt)
//{
// tree[rt].lch=tree[rt].rch=tree[rt].val=r-l+1;
// tree[rt].lazy=0;
// if(l==r){
// return ;
// }
//
// int m=(l+r)>>1;
// build(lson);
// build(rson);
//} void update(int L,int R,int c,int l,int r,int rt)//更新,节点标记已经下传了,所以下面判断就不对了
{
if(tree[rt].lazy!=){
pushdown(l,r,rt);
} if(L<=l&&r<=R){
if(c==){
tree[rt].lch=tree[rt].rch=tree[rt].val=r-l+;
}
else if(c==){
tree[rt].lch=tree[rt].rch=tree[rt].val=;
}
tree[rt].lazy=c;
return ;
} int m=(l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R> m) update(L,R,c,rson);
pushup(l,r,rt);
} int query(int c,int l,int r,int rt)
{
if(tree[rt].lazy!=){
pushdown(l,r,rt);
} if(l==r){
return l;
} int m=(l+r)>>;
if(tree[rt<<].val>=c) return query(c,lson);
else if(tree[rt<<].rch+tree[rt<<|].lch>=c) return m-tree[rt<<].rch+;
else return query(c,rson);
} int main()
{
int n,m;
scanf("%d%d",&n,&m);
build(,n,);
// for(int i=1;i<=40;i++)
// cout<<i<<" "<<tree[i].val<<endl;
for(int i=;i<=m;i++){
int op;
scanf("%d",&op);
if(op==){
int x;
scanf("%d",&x);
if(tree[].val>=x){
int ans=query(x,,n,);
printf("%d\n",ans);
update(ans,ans+x-,,,n,);//住满
}
else{
printf("0\n");
}
}
else{
int x,y;
scanf("%d%d",&x,&y);
update(x,x+y-,,,n,);//清空
}
}
return ;
}

洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治的更多相关文章

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

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

  2. 洛谷 P3071 [USACO13JAN]座位Seating-线段树区间合并(判断找,只需要最大前缀和最大后缀)+分治+贪心

    P3071 [USACO13JAN]座位Seating 题目描述 To earn some extra money, the cows have opened a restaurant in thei ...

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

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

  4. 洛谷P2894 [USACO08FEB]酒店Hotel

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

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

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

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

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

  7. 洛谷 P2894 [USACO08FEB]酒店Hotel

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

  8. P2894 [USACO08FEB]酒店Hotel 线段树

    题目大意 多次操作 查询并修改区间内长度==len的第一次出现位置 修改区间,变为空 思路 类似于求区间最大子段和(应该是这个吧,反正我没做过) 维护区间rt的 从l开始向右的最长长度 从r开始向左的 ...

  9. [USACO08FEB]酒店Hotel 线段树

    [USACO08FEB]酒店Hotel 线段树 题面 其实就是区间多维护一个lmax,rmax(表示从左开始有连续lmax个空房,一直有连续rmax个空房到最右边),合并时讨论一下即可. void p ...

随机推荐

  1. [转载]AMD 的 CommonJS wrapping

    https://www.imququ.com/post/amd-simplified-commonjs-wrapping.html 它是什么? 为了复用已有的 CommonJS 模块,AMD 规定了 ...

  2. 浅谈ASP.net处理XML数据

    XML是一种可扩展的标记语言,比之之前谈到的html有着很大的灵活性,虽然它只是与HTML仅有一个字母只差,但两者有很大的区别. XML也是标记语言,所以它每个标签必须要闭合,而HTML偶尔忘了闭合也 ...

  3. 即时新闻展示插件jQuery News Ticker,超级简单!

    有时候我们为了节省页面空间,会在页面明显处放一小条,用来展示比较重要的即时新闻,一般以轮播的形式出现.今天要介绍的jQuery News Ticker插件就是用来实现这个即时新闻展示功能的,效果图如下 ...

  4. 环境变量ANDROID_SDK_HOME的作用

    默认情况下,开发者创建的AVD(Android Virtual Device)存放在家目录的.android下. 如果是Linux,其路径就是 /home/<your_user_name> ...

  5. Mother's Mil 母亲的牛奶

    Description 农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数,最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另一个桶中,直到被灌桶装 ...

  6. Anaconda+django写出第一个web app(五)

    今天开始学习网页风格和设计,就像python有Web框架一样,也有一些CSS框架.对于CSS框架,我们可以使用默认的样式,也可以在原基础上编辑修改.本教程使用的是materialize这个CSS框架[ ...

  7. C++ Primer 5th 第17章 标准库特殊设施

    C++新标准库提供了很多新功能,它们更加强大和易用. tuple类型 tuple是一种类似pair的模板,pair可以用来保存一对逻辑上有关联的元素对.但与pair不同的是,pair只能存储两个成员, ...

  8. Oracle和MySQL的高可用方案对比【转】

    关于Oracle和MySQL的高可用方案,其实一直想要总结了,就会分为几个系列来简单说说.通过这样的对比,会对两种数据库架构设计上的细节差异有一个基本的认识.Oracle有一套很成熟的解决方案.用我在 ...

  9. 牛x的JavaScript编辑器你知道几个

    英文:Martin Heller  译文:葡萄城控件 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入学习交流群343599877,我们一起学前端! 对于JavaScript程序员来说,目前有很 ...

  10. Linux下实现ping功能

    实现ping功能,就肯定要用到ping命令,那么在Linux下ping命令为: ping [-dfnqrRv][-c<完成次数>][-i<间隔秒数>][-I<网络界面&g ...