Hotel POJ – 3667 (线段树的区间更新)洛谷P2894 [USACO08FEB]

作者: zqqian 分类: ACM 发布时间: 2017-09-21 19:06

题目描述

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.

输入输出样例

输入样例#1:

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
输出样例#1:

1
4
7
0
5

一道典型的线段树的题目

请参考https://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html

的解析,他的方法和我的是一样的,但是实现不太一样,他的更好理解一些

#include <iostream>
#include<cstring>
using namespace std;
int a[300007], lx[300007], rx[300007], lazy[300007];
	void update(int n, int l, int r, int L, int R, int c);

void build(int n, int l, int r) {
	if(l == r) {
		a[n] = 1;
		lx[n] = 1;
		rx[n] = 1;
		return ;
	}
	build(n << 1, l, (l + r) / 2);
	build(n << 1 | 1, (l + r) / 2 + 1, r);
	a[n] = r - l + 1;
	lx[n] = r - l + 1;
	rx[n] = r - l + 1;
}

int query(int n, int l, int r, int N) {
	//cout<<n<<" "<<l<<" "<<r<<" "<<N<<endl;
	if(lazy[n] != -1) {
		update(n << 1, l, (l + r) / 2, l, (l + r) / 2, lazy[n]);
		update(n << 1 | 1, (l + r) / 2 + 1, r, (l + r) / 2 + 1, r, lazy[n]);
		lazy[n] = -1;
	}
	if(a[n] < N) {
		return 0;
	}
	if(lx[n] >= N) {
		return l;
	}

	
	if(a[n << 1] >= N) {
		return query(n << 1, l, (l + r) / 2, N);
	} else if(rx[n << 1] + lx[n << 1 | 1] >= N) {
		return (r+l)/2 - rx[n<<1]+1;
	} else{
		return query(n << 1 | 1, (l + r) / 2 + 1, r, N);
	}




}
void update(int n, int l, int r, int L, int R, int c) {
	//cout<<n<<" "<<l<<" "<<r<<" "<<L<<" "<<R<<" "<<c<<endl;
	if(l==r){
		a[n]=lx[n]=rx[n]=c;
		return;
	}
	if(L <= l && R >= r) {
		lazy[n] = c;
		lx[n] = rx[n] = a[n] = (r - l + 1) * c;
		return ;
	}

	if(lazy[n] != -1) {
		update(n << 1, l, (l + r) / 2, l, (l + r) / 2, lazy[n]);
		update(n << 1 | 1, (l + r) / 2 + 1, r, (l + r) / 2 + 1, r, lazy[n]);
		lazy[n] = -1;
	}
	int mid=(l+r)/2;
	if(L>mid){
		update(n<<1|1,mid+1,r,L,R,c);
	}else if(R<=mid){
		update(n<<1,l,mid,L,R,c);

	}else{
		update(n<<1|1,mid+1,r,L,R,c);
		update(n<<1,l,mid,L,R,c);
	}

	int t = max(a[n << 1], a[n << 1 | 1]);
	a[n] = max(t, rx[n << 1] + lx[n << 1 | 1]);
	lx[n] = lx[n << 1];
	rx[n] = rx[n << 1 | 1];
	if(lx[n << 1] == (r + l) / 2 - l + 1) {
		lx[n] += lx[n << 1 | 1];
	}
	if(rx[n << 1 | 1] == (r - (l + r) / 2)) {
		rx[n] += rx[n << 1];
	}


}




int main(int argc, char const *argv[]) {
	ios::sync_with_stdio(0);
	int n, m;
	memset(lazy, -1, sizeof(lazy));
	cin >> n >> m;
	int c, d, e;
	build(1,1,n);
	for (int i = 0; i < m; ++i) {
		cin >> c;
		if(c == 1) {
			cin >> d;
			int ans=query(1,1,n,d);
			if(ans!=0){
				update(1,1,n,ans,ans+d-1,0);
			}
			cout<<ans<<endl;

		} else {
			cin >> d >> e;
			update(1,1,n,d,d+e-1,1);

		}
	}



	return 0;
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注