洛谷 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 ...
随机推荐
- 组合数学及其应用——polya计数
在处理类似下面的问题中,一般的计数方法会出现问题:假如你要用红.蓝两种颜色给一个正四面体的四个顶点着色,试问存在多少种不同的着色方案? 在高中我们常用的方法是模拟涂色过程,分情况讨论,然后基于分步乘法 ...
- UNDERSTANDING THE GAUSSIAN DISTRIBUTION
UNDERSTANDING THE GAUSSIAN DISTRIBUTION Randomness is so present in our reality that we are used to ...
- Understanding the Bias-Variance Tradeoff
Understanding the Bias-Variance Tradeoff When we discuss prediction models, prediction errors can be ...
- SQL Server 2008 R2 企业版安装教程
1 安装包解压 2 解压后,打开setup.exe文件,选择安装,显示如图: 3 选择全新安装或向现有安装添加功能 4 点确定 5 输入 企业版序列号:R88PF-GMCFT-KM2KR-4R7GB- ...
- IE6下面的css调试工具
在开发过程中,代码部分实现之后,就要着手于前台展示部分的界面,公司的美工又是新手,无奈,只有自己慢慢调了,但IE6之前的版本都没有好的调试工具,后来在网上搜索了一个 IE Developer Tool ...
- caffe的特殊层
每次写博客都带有一定的目的,在我看来这是一个记录的过程,所以尽量按照循序渐进的顺序逐步写,前面介绍的CNN层应该是非常常用的,这篇博客介绍一下某些特殊的layer,但是由于特殊的layer都带有一定的 ...
- HDU 1556 Color the ball (树状数组 区间更新+单点查询)
题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...
- CSV转excel方法
步骤一:新建excel文件,数据—>自文本,导入文件 步骤二:选择分隔符,下一步 步骤三:勾选分隔符符合,下一步 步骤四:直接下一步,可在预览里看到格式 步骤五:点击确定,等待数据导入
- Python练习-三级菜单与"片儿"无关!
# 编辑者:闫龙 #三级目录 menu = { '北京':{ '海淀':{ '五道口':{'soho':{},'网易':{},'google':{}}, '中关村':{'爱奇艺':{},'汽车之家': ...
- APScheduler API -- apscheduler.triggers.date
apscheduler.triggers.date API Trigger alias for add_job(): date class apscheduler.triggers.date.Date ...