您的位置:首页 > 其它

POJ 3667 Hotel 线段树

2014-06-20 17:11 302 查看
Hotel

Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 11104Accepted: 4793
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 sizeDi (1 ≤
Di ≤ N) and approach the front desk to check in. Each groupi requests a set of
Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbersr..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 ofr to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkouti has the parameters Xi and
Di which specify the vacating of roomsXi ..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 andM

* 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 andDi
(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 integerr, 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

Source
USACO 2008 February Gold

传送门:POJ 3667 Hotel

题目大意:

每次两种操作。

1、询问是否有连续长度为x的空房间,有解则输出能入住房间的最左端,且尽量靠左。无解输出0。

2、将从a开始长度为x的房间变成空房间。

题目分析:

区间合并问题。维护区间内的连续空房间即可。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define lson l , m , o << 1
#define rson m + 1 , r , o << 1 | 1

const int maxN = 200005 ;
const int oo = 0x3f3f3f3f ;

int lmax[maxN] , mmax[maxN] , rmax[maxN] ;
int mark[maxN] ;

int max ( const int X , const int Y ) {
	if ( X > Y ) return X ;
	return Y ;
}

void PushUp ( int o , int l , int r ) {
	lmax[o] = lmax[o << 1] ;
	rmax[o] = rmax[o << 1 | 1] ;
	mmax[o] = max ( mmax[o << 1] , mmax[o << 1 | 1] ) ;
	if ( rmax[o << 1] && lmax[o << 1 | 1] ) {
		int m = ( l + r ) >> 1 ;
		mmax[o] = max ( mmax[o] , rmax[o << 1] + lmax[o << 1 | 1] ) ;
		if ( lmax[o << 1] == m - l + 1 ) lmax[o] += lmax[o << 1 | 1] ;
		if ( rmax[o << 1 | 1] == r - m ) rmax[o] += rmax[o << 1] ;
	}
}

void PushDown ( int o , int l , int r ) {
	if ( mark[o] != -1 ) {
		int m = ( l + r ) >> 1 ;
		mark[o << 1] = mark[o << 1 | 1] = mark[o] ;
		mmax[o << 1] = rmax[o << 1] = lmax[o << 1] = ( mark[o << 1] ? 0 : m - l + 1 ) ;
		mmax[o << 1 | 1] = lmax[o << 1 | 1] = rmax[o << 1 | 1] = ( mark[o << 1 | 1] ? 0 : r - m ) ;
		mark[o] = -1 ;
	}
}

void Build ( int l , int r , int o ) {
	lmax[o] = mmax[o] = rmax[o] = r - l + 1 ;
	mark[o] = -1 ;
	if ( l == r ) return ;
	int m = ( l + r ) >> 1 ;
	Build ( lson ) ;
	Build ( rson ) ;
}

void Update ( int L , int R , int v , int l , int r , int o ) {
	if ( L <= l && r <= R ) {
		mark[o] = v ;
		rmax[o] = mmax[o] = lmax[o] = ( mark[o] ? 0 : r - l + 1 ) ;
		return ;
	}
	PushDown ( o , l , r ) ;
	int m = ( l + r ) >> 1 ;
	if ( L <= m ) Update ( L , R , v , lson ) ;
	if ( m <  R ) Update ( L , R , v , rson ) ;
	PushUp ( o , l , r ) ;
}

int Query ( int x , int l , int r , int o ) {
	if ( l == r ) return l ;
	PushDown ( o , l , r ) ;
	int m = ( l + r ) >> 1 ;
	if ( mmax[o << 1] >= x ) return Query ( x , lson ) ;
	else if ( rmax[o << 1] + lmax[o << 1 | 1] >= x ) return m - rmax[o << 1] + 1 ;
	else return Query ( x , rson ) ;
}

void work () {
	int n , m , ch , l , x ;
	while ( ~scanf ( "%d%d" , &n , &m ) ) {
		Build ( 1 , n , 1 ) ;
		while ( m -- ) {
			scanf ( "%d" , &ch ) ;
			if ( 1 == ch ) {
				scanf ( "%d" , &x ) ;
				if ( mmax[1] < x ) {
					printf ( "0\n" ) ;
					continue ;
				}
				int ans = Query ( x , 1 , n , 1 ) ;
				printf ( "%d\n" , ans ) ;
				Update ( ans , ans + x - 1 , 1 , 1 , n , 1 ) ;
			}
			else {
				scanf ( "%d%d" , &l , &x ) ;
				Update ( l , l + x - 1 , 0 , 1 , n , 1 ) ;
			}
		}
	}
} 

int main () {
	work () ;
	return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: