POJ - 3667 Hotel(线段树区间合并)

POJ - 3667 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.
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

题意:n个空房间排成一列,1代表n个房间里是否右d个连续的空房间,如果有,输出最小的起始编号,这些房间住进人,没有输出0,2代表从X开始有连续D个房间退房

思路:线段树区间合并题,需要注意的是在查询的时候,先查询左子树,在查询左子树的rs与右子树的ls的和,最后查询右子树

#include<stdio.h>
#include<algorithm>
using namespace std;
const int MAXN = 5e4 + 10;
struct node{
	int l,r;
	int ls,rs,maxs;
	int cover; 
}segTree[MAXN<<2];

void build(int num,int l,int r){
	segTree[num].l=l;
	segTree[num].r=r;
	segTree[num].cover=-1;
	segTree[num].ls=segTree[num].rs=segTree[num].maxs=(r-l+1);
	if(l==r){
		return;
	}
	int mid=(segTree[num].l+segTree[num].r)>>1;
	build(num<<1,l,mid);
	build(num<<1|1,mid+1,r);
}
void pushdown(int num){
	if(segTree[num].cover!=-1){
		segTree[num<<1].cover=segTree[num<<1|1].cover=segTree[num].cover;
		if(segTree[num].cover){
			segTree[num<<1].ls=segTree[num<<1].rs=segTree[num<<1].maxs=0;
			segTree[num<<1|1].ls=segTree[num<<1|1].rs=segTree[num<<1|1].maxs=0;
		}else{
			segTree[num<<1].ls=segTree[num<<1].rs=segTree[num<<1].maxs=segTree[num<<1].r-segTree[num<<1].l+1;
			segTree[num<<1|1].ls=segTree[num<<1|1].rs=segTree[num<<1|1].maxs=segTree[num<<1|1].r-segTree[num<<1|1].l+1;
		}
		segTree[num].cover=-1;
	}
}

void pushup(int num){
	
	segTree[num].ls=segTree[num<<1].ls;
	segTree[num].rs=segTree[num<<1|1].rs;
	segTree[num].maxs=max(segTree[num<<1].maxs,segTree[num<<1|1].maxs);
	segTree[num].maxs=max(segTree[num<<1].rs+segTree[num<<1|1].ls,segTree[num].maxs);
	
	if(segTree[num].ls==(segTree[num<<1].r-segTree[num<<1].l+1)){
		segTree[num].ls+=segTree[num<<1|1].ls;
	}
	if(segTree[num].rs==(segTree[num<<1|1].r-segTree[num<<1|1].l+1)){
		segTree[num].rs+=segTree[num<<1].rs;
	}
	
}
void update(int num,int l,int r,int change){
	if(segTree[num].l==l&&segTree[num].r==r){
		segTree[num].cover=change;
		if(change){
			segTree[num].ls=segTree[num].rs=segTree[num].maxs=0;
		}else{
			segTree[num].ls=segTree[num].rs=segTree[num].maxs=segTree[num].r-segTree[num].l+1;
		}
		return;
	}
	
	pushdown(num);
	int mid=(segTree[num].l+segTree[num].r)>>1;
	if(r<=mid){
		update(num<<1,l,r,change);
	}else if(l>mid){
		update(num<<1|1,l,r,change);
	}else{
		update(num<<1,l,mid,change);
		update(num<<1|1,mid+1,r,change);
	}
	pushup(num);
	
}
int query(int num,int l,int r,int d){
	if(l==r) return l;
	int mid=(l+r)>>1;
	pushdown(num);
	if(segTree[num<<1].maxs>=d){
		return query(num<<1,l,mid,d);
	}else if(segTree[num<<1].rs+segTree[num<<1|1].ls>=d){
		return mid-segTree[num<<1].rs+1;
	}else{
		return query(num<<1|1,mid+1,r,d);
	}
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	build(1,1,n);
	while(m--){
		int type;
		scanf("%d",&type);
		if(type==1){
			int d;
			scanf("%d",&d);
			if(segTree[1].maxs<d){
				printf("0\n");
			}else{
				int min_pos=query(1,1,n,d);
				printf("%d\n",min_pos);
				update(1,min_pos,min_pos+d-1,1);
			}
		}else{
			int x,d;
			scanf("%d%d",&x,&d);
			update(1,x,x+d-1,0);
		}
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值