洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治
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.
输入输出样例
线段树区间合并
代码:
#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-线段树区间合并(判断找位置,不需要维护端点)+分治的更多相关文章
- 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]
题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...
- 洛谷 P3071 [USACO13JAN]座位Seating-线段树区间合并(判断找,只需要最大前缀和最大后缀)+分治+贪心
P3071 [USACO13JAN]座位Seating 题目描述 To earn some extra money, the cows have opened a restaurant in thei ...
- 洛谷 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(线段树)
问题描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...
- 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel
https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel
题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...
- P2894 [USACO08FEB]酒店Hotel 线段树
题目大意 多次操作 查询并修改区间内长度==len的第一次出现位置 修改区间,变为空 思路 类似于求区间最大子段和(应该是这个吧,反正我没做过) 维护区间rt的 从l开始向右的最长长度 从r开始向左的 ...
- [USACO08FEB]酒店Hotel 线段树
[USACO08FEB]酒店Hotel 线段树 题面 其实就是区间多维护一个lmax,rmax(表示从左开始有连续lmax个空房,一直有连续rmax个空房到最右边),合并时讨论一下即可. void p ...
随机推荐
- springboot 以jar方式在linux后台运行
linux命令如下: nohup java -jar 自己的springboot项目.jar >日志文件名.log 2>&1 & 命令解释: nohup:不挂断地运行命令, ...
- Java并发编程原理与实战十五:手动实现一个可重入锁
package com.roocon.thread.ta1; public class Sequence { private MyLock lock = new MyLock(); private ...
- ipynb to pdf
Q: 如何把jupyter notebook 转为 pdf 文档? A: 尝试了几种python包, 结果都没有成功. 包括: xhtml2pdf, 查看官方的介绍说用pandoc也是一种方法, 但是 ...
- JQuery的选择器对控件ID含有特殊字符的解决方法-涨姿势了!
1.jquery类库在我们实际项目中用的很多,大家经常需要根据控件的id,获取对应的html元素. 但是:当id含有特殊字符的时候,是不能选中的. 2. 自己简单的测试了下,jquery的id选择器只 ...
- Hadoop/Spark环境运行过程中可能遇到的问题或注意事项
1.集群启动的时候,从节点的datanode没有启动 问题原因:从节点的tmp/data下的配置文件中的clusterID与主节点的tmp/data下的配置文件中的clusterID不一致,导致集群启 ...
- CodeForces 816C 思维
On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as fo ...
- 【leetcode 简单】 第七十六题 移动零
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作, ...
- python 爬虫 ~ 查看收发包的情况
DebugLog 可以用来查看收发包的情况,比较有意思,现特意记录下来: Sample: import urllib2 httpHandler = urllib2.HTTPHandler(debugl ...
- exec操作文件描述符
exec命令可以用来替代当前shell:换句话说,并没有启动子shell.使用这一命令时任何环境都将被清除,并重新启动一个shell. 它的一半形式为: exec command 其中,command ...
- Dropout caffe源码
GPU和CPU实现的不一样,这里贴的是CPU中的drop out 直接看caffe里面的源码吧:(产生满足伯努利分布的随机数mask,train的时候,data除以p,...... scale_ = ...