Hotel(线段树合并)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 14958 | Accepted: 6450 |
Description
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.
Input
* 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
Output
* 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.
Sample Input
10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
Sample Output
1
4
7
0
5
题解:
Hotel有N(1 ≤ N ≤ 50,000)间rooms,并且所有的rooms都是连续排列在同一边,groups需要check in 房间,
要求房间的编号为连续的r..r+Di-1并且r是最小的;visitors同样可能check out,
并且他们每次check out都是编号为Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1)的房间,题目的输入有两种样式:
- 1 a : groups需要check in a间编号连续的房间
- 2 a b : visitors check out 房间,其中房间编号是 a…a+b-1
要求对于每次request,输出为groups分配数目为a的房间中编号最小的房间编号
利用线段树建立模型,维护最大连续区间长度,其中区间长度就是对应的房间数目,并且对应区间中最左边的断点就是answer,
同时因为需要求出连续区间的最大长度,因此每次PushUp时都将左右区间合并,
lsum维护左区间的最大长度,rsum维护右区间的最大长度,sum维护区间1…N中的最大连续区间长度,lazy延迟标记;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x,y) scanf("%lf%lf",&x,&y)
#define P_ printf(" ")
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
typedef long long LL;
const int MAXN=50010<<2;
int sum[MAXN],lsum[MAXN],rsum[MAXN];
int lazy[MAXN];
void pushdown(int root,int v){
if(lazy[root]!=-1){
lazy[ll]=lazy[rr]=lazy[root];
sum[ll]=lsum[ll]=rsum[ll]=lazy[root]?0:v-(v>>1);
sum[rr]=lsum[rr]=rsum[rr]=lazy[root]?0:v>>1;
lazy[root]=-1;
}
}
void pushup(int root,int v){
lsum[root]=lsum[ll];
rsum[root]=rsum[rr];
if(lsum[root]==v-(v>>1))lsum[root]+=lsum[rr];
if(rsum[root]==(v>>1))rsum[root]+=rsum[ll];
sum[root]=max(lsum[rr]+rsum[ll],max(sum[ll],sum[rr]));
}
void build(int root,int l,int r){
int mid=(l+r)>>1;
sum[root]=lsum[root]=rsum[root]=r-l+1;
lazy[MAXN]=-1;
if(l==r)return;
build(lson);
build(rson);
}
void update(int root,int l,int r,int A,int B,int c){
if(l>=A&&r<=B){
lazy[root]=c;
sum[root]=lsum[root]=rsum[root]=c?0:r-l+1;
return;
}
pushdown(root,r-l+1);
int mid=(l+r)>>1;
if(mid>=A)update(lson,A,B,c);
if(mid<B)update(rson,A,B,c);
pushup(root,r-l+1);
}
int query(int root,int l,int r,int v){
if(l==r)return l;
pushdown(root,r-l+1);
int mid=(l+r)>>1;
if(sum[ll]>=v)return query(lson,v);
else if(lsum[rr]+rsum[ll]>=v)return mid-rsum[ll]+1;
else return query(rson,v);
}
int main(){
int N,M;
while(~scanf("%d%d",&N,&M)){
build(1,1,N);
int a,b,c;
while(M--){
SI(a);SI(b);
if(a==1){
if(sum[1]<b){
puts("0");continue;
}
int ans=query(1,1,N,b);
printf("%d\n",ans);
update(1,1,N,ans,ans+b-1,1);//询问完毕需要更新上;
}
else{
SI(c);
update(1,1,N,b,b+c-1,0);
}
}
}
return 0;
}
Hotel(线段树合并)的更多相关文章
- [USACO08FEB]酒店Hotel 线段树
[USACO08FEB]酒店Hotel 线段树 题面 其实就是区间多维护一个lmax,rmax(表示从左开始有连续lmax个空房,一直有连续rmax个空房到最右边),合并时讨论一下即可. void p ...
- [XJOI NOI2015模拟题13] C 白黑树 【线段树合并】
题目链接:XJOI - NOI2015-13 - C 题目分析 使用神奇的线段树合并在 O(nlogn) 的时间复杂度内解决这道题目. 对树上的每个点都建立一棵线段树,key是时间(即第几次操作),动 ...
- [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】
题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
- BZOJ 3307: 雨天的尾巴( LCA + 线段树合并 )
路径(x, y) +z : u处+z, v处+z, lca(u,v)处-z, fa(lca)处-z, 然后dfs一遍, 用线段树合并. O(M log M + M log N). 复杂度看起来不高, ...
- BZOJ2733 [HNOI2012]永无乡 【线段树合并】
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- bzoj 2243 [SDOI2011]染色(树链剖分+线段树合并)
[bzoj2243][SDOI2011]染色 2017年10月20日 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询 ...
- bzoj3702二叉树 线段树合并
3702: 二叉树 Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 600 Solved: 272[Submit][Status][Discuss] ...
- BZOJ_2212_[Poi2011]Tree Rotations_线段树合并
BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...
- B20J_2733_[HNOI2012]永无乡_权值线段树合并
B20J_2733_[HNOI2012]永无乡_权值线段树合并 Description:n座岛,编号从1到n,每座岛都有自己的独一无二的重要度,按照重要度可以将这n座岛排名,名次用1到 n来表示.某些 ...
随机推荐
- 分支-03. 三天打鱼两天晒网-B3
/*B3-分支-03. 三天打鱼两天晒网 *Main.c *测试通过 */ #include <stdio.h> #include <stdlib.h> int main() ...
- 获取Json对象的长度或计数
最近又开始写博客了.因为最近的工作又开始与技术方面接口了.现在在开发WEB的时候,经常会遇到JSON对象的传递,JSON是个好东西,但是它却没有提供一些简单便捷的处理方法,其中获取JSON对象的长度就 ...
- SNMP_802.1
http://www.cisco.com/en/US/tech/tk648/tk362/technologies_tech_note09186a00801c9199.shtml http://www. ...
- Android仿微信界面--使用Fragment实现(慕课网笔记)
1 效果图 这里我们没有实现滑动切换view的功能 2 具体实现: 2.1 布局文件:top.xml, bottom.xml,tab01.xml,tab02.xml,tab03.xml,tab04. ...
- AJP协议总结与分析
Tomcat服务器通过Connector连接器组件与客户程序建立连接,Connector组件负责接收客户的请求,以及把Tomcat服务器的响应结果发送给客户.默认情况下,Tomcat在server.x ...
- docker 网络4种模式
1.host 模式,使用docker run 时 使用--net=host 指定 docker 使用的网络和宿主机一样,在容器上看到的网卡ip就是宿主机上的ip 2.container 模式,使用-- ...
- Android 数字签名学习笔记
Android 数字签名学习笔记 在Android系统中,所有安装到系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程序之间建立信任关系,如果一个permission的pro ...
- POI开源项目-PPT2PNG转换测试
第一次学使用软件去读取Office类文件. 今天百度了一下,发现Apache也是有这样的开源项目的,POI提供下列支持: 测试了一下HSLF对PPT的读操作,并将PPT转化为图片保存到指定目录: 使用 ...
- 弱类型语言中的0和空字符串(''或"")以及字符串'0'
在弱类型语言(js/PHP)中, 当我们用==判断0和'0'以及空字符串(''或"")是否相等的时候, 返回的是true. 而且在PHP中, 当我们用==判断0和null是否相等的 ...
- Android学习之SQLite学习
花了2天时间,系统学习了下Android开发过程中使用的轻量级数据库SQLite的使用. 并掌握其增,删,该,查的基本数据库操作. 首先要使用SQLite数据库,须要通过Android系统提供的SQL ...