P2894 [USACO08FEB]Hotel G(线段树)

题目描述

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.

输入格式

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.

输入输出样例

输入
10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
输出
1
4
7
0
5

题目大意

一开始有n间空房,下面有m个操作
1.表示查询房间,再输入一个数x,表示在1–n 房间中找到长度为x的连续空房,输出连续x个房间中左端的房间号,尽量让这个房间号最小,若找不到长度为x的连续空房,输出0。若找得到,在这x个空房间中住上人。
2.表示退房,再输入两个数 x,y 代表 房间号 x—x+y-1 退房,即让房间为空。

题目分析

这个题很明显就是线段树的题了,用线段树来维护一个01序列,0表示该位置是空房,1表示该位置住人了。

先来看看操作1需要什么,为了快速的找出长度为x的连续空房,我们可以在线段树中维护一个tm //表示当前段中,最大连续个0的长度再来想想如何维护tm(即某一段中的tm如何通过子段信息获得),要维护tm我们还需要lm //表示当前段最左端的最大连续个0的个数rm //表示当前段最右端的最大连续个0的个数
tm=max(max(左子段.tm,右子段.tm),左子段.rm+右子段.lm);
lm=左子段全为0 ? 左子段的长度+右子段.lm : 左子段.lm;
rm=右子段全为0 ? 右子段的长度+左子段.rm : 右子段.rm;
有了tm,就可以比较快速的找出长度为x的连续空房了。
第二步是将这一段全部变为1,只需要额外再维护一个懒标记即可(用懒标记改变一段序列的值是懒标记的基本用法,这里不多说了)。

操作2是将一段序列全部变为0。于前面的操作是一样的。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#define LL long long
#define PII pair<int,int>
#define x first
#define y second
using namespace std;
const int N=5e4+5,INF=0x3f3f3f3f;
struct Node{
    
    			//线段树
	int l,r;
	int tm,lm,rm;
	int lazy;			//lazy=1,表示将该段全部变为1
}tr[N*4];				//lazy=-1,表示将该段全部变为0
void pushup(int u)		//详情请看上面的解释
{
    
    
	if(tr[u<<1].tm<tr[u<<1].r-tr[u<<1].l+1) tr[u].lm=tr[u<<1].lm;
	else tr[u].lm=tr[u<<1].lm+tr[u<<1|1].lm;
	if(tr[u<<1|1].tm<tr[u<<1|1].r-tr[u<<1|1].l+1) tr[u].rm=tr[u<<1|1].rm;
	else tr[u].rm=tr[u<<1].rm+tr[u<<1|1].rm;
	tr[u].tm=max(tr[u<<1].rm+tr[u<<1|1].lm,max(tr[u<<1].tm,tr[u<<1|1].tm));
}
void push(int u,int st)		//改变一段序列的值。st=1,将该段全部变为1
{
    
    							//st=-1,将该段全部变为0
	if(st==1) tr[u].tm=tr[u].lm=tr[u].rm=0;
	else tr[u].tm=tr[u].lm=tr[u].rm=tr[u].r-tr[u].l+1;
	tr[u].lazy=st;
}
void pushdown(int u)		//下放懒标记
{
    
    
	if(tr[u].lazy)
	{
    
    
		push(u<<1,tr[u].lazy);
		push(u<<1|1,tr[u].lazy);
		tr[u].lazy=0;
	}
}
void build(int u,int l,int r)			//建树
{
    
    
	if(l==r) tr[u]={
    
    l,r,r-l+1,r-l+1,r-l+1,0};	//初始序列全部为0
	else {
    
    
		tr[u]={
    
    l,r};
		int mid=l+r>>1;
		build(u<<1,l,mid),build(u<<1|1,mid+1,r);
		pushup(u);
	}
}
void update(int u,int l,int r,int c)		//将[l,r]区间内的数全部变为c
{
    
    
	if(l<=tr[u].l&&tr[u].r<=r) push(u,c==1?1:-1);
	else {
    
    
		pushdown(u);
		int mid=tr[u].l+tr[u].r>>1;
		if(mid>=l) update(u<<1,l,r,c);
		if(mid<r) update(u<<1|1,l,r,c);
		pushup(u);
	}
}
int query(int u,int x)					//查找最靠左的,长度为x的连续个0的位置
{
    
    
	if(tr[u].l==tr[u].r) return tr[u].l;
	pushdown(u);
	int mid=tr[u].l+tr[u].r>>1;
	if(tr[u<<1].tm>=x) return query(u<<1,x);	//如果左子段的tm>=x,说明存在更靠左的解
	else if(tr[u<<1].rm+tr[u<<1|1].lm>=x) return mid-tr[u<<1].rm+1;	//此时,说明mid位于最优解上,答案即为mid-tr[u<<1].rm+1
	return query(u<<1|1,x);			//向右查找合适的解
}
int main()
{
    
    
	int n,m;
	scanf("%d%d",&n,&m);
	build(1,1,n);						//建树
	while(m--)
	{
    
    
		int op,x,y;
		scanf("%d%d",&op,&x);
		if(op==1)
		{
    
    
			if(tr[1].tm>=x)				//存在合适的解
			{
    
     
				int st=query(1,x);		//查找最优解的位置
				printf("%d\n",st);		//输出该位置
				update(1,st,st+x-1,1);	//更新区间
			}
			else puts("0");
		}
		else {
    
    
			scanf("%d",&y);
			update(1,x,x+y-1,0);		//更新区间
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/li_wen_zhuo/article/details/113098094
今日推荐