题目描述

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

一开始以为借教室那道题的线段树做法很像,但是仔细揣摩一下才发现两个题完全不是一个档次的。。。这道题的大体思路就是,设没有被租为1,被租为0对于每一段区间l,r1.保存从l向后的最长的为0的长度2.保存从r向前的最长的为0的长度3.保存整个区间里最长的为0的长度4.保存区间的长度对于每一次update,我们同时考虑左孩子的原空闲情况,右孩子的原空闲情况,以及他们拼在一起时中间部分的新的空闲情况
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #define lli long long int
  6 #define ls k<<1
  7 #define rs k<<1|1
  8 using namespace std;
  9 const int MAXN=100001;
 10 inline void read(int &n)
 11 {
 12     char c='+';int x=0;bool flag=0;
 13     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
 14     while(c>='0'&&c<='9'){x=x*10+c-48;c=getchar();}
 15     flag==1?n=-x:n=x;
 16 }
 17 struct node
 18 {
 19     lli l,r,w,f,lfree,rfree,allfree,chang;
 20 }tree[MAXN<<2];
 21 int n,m;
 22 inline void update(int k)
 23 {
 24     if(tree[ls].allfree==tree[ls].chang)
 25         tree[k].lfree=(tree[ls].chang+tree[rs].lfree);
 26     else
 27         tree[k].lfree=(tree[ls].lfree);
 28
 29     if(tree[rs].allfree==tree[rs].chang)
 30         tree[k].rfree=(tree[rs].chang+tree[ls].rfree);
 31     else
 32         tree[k].rfree=tree[rs].rfree;
 33
 34     tree[k].allfree=max(tree[ls].allfree,tree[rs].allfree);
 35     tree[k].allfree=max(tree[k].allfree,tree[ls].rfree+tree[rs].lfree);
 36
 37     return ;
 38 }
 39 inline void build_tree(int ll,int rr,int k)
 40 {
 41     tree[k].l=ll;tree[k].r=rr;
 42     tree[k].chang=(tree[k].r-tree[k].l+1);
 43     if(ll==rr)
 44     {    tree[k].lfree=tree[k].rfree=tree[k].allfree=(tree[k].r-tree[k].l+1);        return ;    }
 45     lli mid=(tree[k].l+tree[k].r)>>1;
 46     build_tree(ll,mid,ls);
 47     build_tree(mid+1,rr,rs);
 48     update(k);
 49 }
 50 inline void pushdown(int k,int how)
 51 {
 52     if(how==1)
 53     {
 54         tree[ls].lfree=tree[ls].rfree=tree[ls].allfree=0;
 55         tree[rs].lfree=tree[rs].rfree=tree[rs].allfree=0;
 56         tree[ls].f=tree[k].f;
 57         tree[rs].f=tree[k].f;
 58         tree[k].f=0;
 59     }
 60     else
 61     {
 62         tree[ls].lfree=tree[ls].rfree=tree[ls].allfree=tree[ls].chang;
 63         tree[rs].lfree=tree[rs].rfree=tree[rs].allfree=tree[rs].chang;
 64         tree[ls].f=tree[k].f;
 65         tree[rs].f=tree[k].f;
 66         tree[k].f=0;
 67     }
 68     return ;
 69 }
 70 inline int query(int k,int num)
 71 {
 72     if(tree[k].f)
 73         pushdown(k,tree[k].f);
 74     if(tree[k].r==tree[k].l)
 75         return tree[k].l;
 76     if(tree[ls].allfree>=num)
 77         return query(ls,num);
 78     if(tree[ls].rfree+tree[rs].lfree>=num)
 79         return tree[ls].r-tree[ls].rfree+1;
 80     else
 81         return query(rs,num);
 82 }
 83 inline void change(int k,int ll,int rr,int how)
 84 {
 85     if(ll<=tree[k].l&&tree[k].r<=rr)
 86     {
 87         if(how==1)
 88             tree[k].allfree=tree[k].lfree=tree[k].rfree=0;
 89         else
 90             tree[k].allfree=tree[k].lfree=tree[k].rfree=tree[k].chang;
 91
 92         tree[k].f=how;
 93         return ;
 94     }
 95     int mid=(tree[k].l+tree[k].r)>>1;
 96     if(tree[k].f)
 97         pushdown(k,tree[k].f);
 98     if(ll<=mid)
 99         change(ls,ll,rr,how);
100     if(rr>mid)
101         change(rs,ll,rr,how);
102     update(k);
103 }
104 int main()
105 {
106     read(n);read(m);
107     build_tree(1,n,1);
108     for(int i=1;i<=m;i++)
109     {
110         int how;
111         read(how);
112         if(how==1)
113         {
114             int num;
115             read(num);
116             if(tree[1].allfree<num)
117             {
118                 printf("0\n");
119                 continue;
120             }
121             int pos=query(1,num);
122             change(1,pos,pos+num-1,1);// 放置
123             printf("%d\n",pos);
124         }
125         else
126         {
127             int xx,yy;
128             read(xx);read(yy);
129             change(1,xx,xx+yy-1,2);//清空
130         }
131     }
132     return 0;
133 }

P2894 [USACO08FEB]酒店Hotel相关推荐

  1. 线段树||BZOJ1593: [Usaco2008 Feb]Hotel 旅馆||Luogu P2894 [USACO08FEB]酒店Hotel

    题面:P2894 [USACO08FEB]酒店Hotel 题解:和基础的线段树操作差别不是很大,就是在传统的线段树基础上多维护一段区间最长的合法前驱(h_),最长合法后驱(t_),一段中最长的合法区间 ...

  2. P2894 USACO08FEB HOTEL G

    [USACO08FEB]Hotel G 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enr ...

  3. P2894 [USACO08FEB]Hotel G

    传送门 一个 N N N的数组 支持两种操作 查询 1 1 1 ~ N N N 长度最少为 x x x 连续的 1 1 1 将区间 [ l , r ] [l,r] [l,r] 的值置为 x , x ∈ ...

  4. word20170105订酒店 hotel reservation有用的词和句子

    有用的词: hotel reservation/booking: 酒店预订 standard room:标准间 suite: 套房 king size bed: 大床房 double bed:双床房 ...

  5. 线段树维护(最大区间和,最大子段和,最长连续上升子序列)

    本文主要介绍用线段树来维护(最大区间和,最大子段和,最长连续上升子序列)的问题. HDU 1540 Tunnel Warfare(最长连续区间+单点修改) 洛谷 P2894 [USACO08FEB]酒 ...

  6. 清北学堂学习笔记 第一期

    Day 1 1.贪心的奇怪方法:调整法 调整法,顾名思义,就是用别的方式进行题目的分析以及证明,例如说luogu的最大乘积.这种题目的主要分析思路为:先考虑一些简单的情况,通过简单的情况来推出一些有用 ...

  7. JAVA day06 酒店管理系统

    酒店管理系统分析 业务: 1.查房(show)      1001    1002    1003    1004    1005     null    null    null    null   ...

  8. 洲际酒店集团发布全新奢华精选品牌Vignette Collection

    上海2021年8月25日 /美通社/ -- 洲际酒店集团为旗下全新奢华及精品系列品牌Vignette Collection揭开面纱,同时宣布首批酒店落户澳大利亚和泰国. Vignette Collec ...

  9. 简单的酒店住房退房管理系统

    运用java面向对象和二维数组的知识写了一个简单的酒店住房退房管理系统 适合初学java的同学 首先给大家看一下我的项目的目录结构,很简单就3个类 接下来给大家看一下我这3个类的代码 1,首先考虑一个 ...

最新文章

  1. 使用了这个神器,让我的代码bug少了一半
  2. AngularJs的UI组件ui-Bootstrap分享(六)——Tabs
  3. 理解 Python 中的多线程
  4. ReactNative ES6简介 及基本语法第一篇
  5. Workspace in use or cannot be created, choose a different one.--错误解决办法
  6. Karrigell 入门教程
  7. 基于顺序存储结构的图书信息表的新图书的入库(C++)
  8. C++ 类的定义、作用域及大小计算,限定访问符,this指针
  9. 95-130-342-源码-source-kafka相关-AbstractPartitionDiscoverer
  10. JavaScript or JQuery 获取服务器时间
  11. 跨境电商为什么需要ERP系统?
  12. QQ 的登录封面是怎么设计的
  13. Flocker 做为后端存储代理 docker volume-driver 支持
  14. jetson nano ROS 教程
  15. 完美解决.CHM文件打不开或者打开以后显示空白的情况
  16. X4扭曲字体或图形 coreldraw_cdrx4精简版下载|coreldraw x4 sp2 精简版下载增强版 15.2.3.1614 - 系统天堂...
  17. android 图片轮播框架banner
  18. SAP中货物移动库位权限管理测试
  19. Kubernetes如何被应用在华为
  20. java 中long和Long的区别

热门文章

  1. 微信之父张小龙:产品经理的必备书单(转)
  2. C#中对于float,double,decimal的误解
  3. c#利用三层架构做一个简单的登录窗体
  4. 项目乱码 GBK转UTF-8工具
  5. Spring+SpringMVC项目搭建
  6. css区块定位之浮动与清除属性
  7. C#正则表达式整理备忘
  8. ViewTreeObserver简介
  9. div+css中clear用法
  10. GridView中实现单选RadioButton