洛谷 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 ...
随机推荐
- R8—批量生成文件夹,批量读取文件夹名称+R文件管理系统操作函数
一. 批量生成文件夹,批量读取文件夹名称 今日,工作中遇到这样一个问题:boss给我们提供了200多家公司的ID代码(如6007.7920等),需要根据这些ID号去搜索下载新闻,从而将下载到的新闻存到 ...
- CodeForces - 1042B
Berland shop sells nn kinds of juices. Each juice has its price cici. Each juice includes some set o ...
- 结构体变量的sizeof计算
结构体字节对齐准则: 1. 结构体变量的首地址能够被其最宽基本类型成员的大小所整除: 2. 结构体每个成员相对于结构体首地址的偏移量都是当前成员大小的整数倍,如有需要编译器会在成员之间加上填充字节: ...
- Red Hat Enterprise Linux 7.2下使用RPM包安装SQL Server vNext
1.下载安装包 mssql-server:https://packages.microsoft.com/rhel/7/mssql-server/ mssql-tools:https://package ...
- springboot集成mybatis环境搭建以及实现快速开发微服务商品模块基本的增删改查!
之前学习了springboot和mybatis3的一些新特性,初步体会了springboot的强大(真的好快,,,,,),最近趁着复习,参考着以前学习的教程,动手写了一个springboot实战的小例 ...
- Tslib移植与分析【转】
转自:http://blog.csdn.net/water_cow/article/details/7215308 目标平台:LOONGSON-1B开发板(mips32指令集)编译平台:x86PC-- ...
- MySQL 5.6 GTID Replication【转】
一. MySQL 5.6引入了GTID的概念,那么GTID是何方神圣?其实也不复杂,就是一个全局事务标示符.使用GTID时,每次事务提交都会在binlog里生成1个唯一的标示符,它由UUID和事务ID ...
- 『实践』百度地图给map添加右键菜单(判断是否为marker)
var map; var s;//经度 var w;//纬度 $(document).ready(function(){ $(".mune").load("jsp/c ...
- Small Private Cloud Deployment Solution
项目背景 为用户提供可访问的桌面虚拟机,方便软件研发人员日常办公,软件开发,编译等工作.主要操作包括Visor制图.程序开发测试以及使用office软件办公. 目前阶段需要支持100台虚拟机(4VCP ...
- html-表格和列表
一:表格标签 表格 描述 <table> 定义表格 <caption> 定义表格标题. <th> 定义表格的表头. <tr> 定义表格的行. <t ...