[POJ 3667] Hotel【线段树区间合并】

  • 2018-01-21
  • 0
  • 0

Problem:

Time Limit: 3000MS Memory Limit: 65536K

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 rto 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 D(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

Source

USACO 2008 February Gold

Solution:

这道题是一道经典的线段树区间合并问题,有一定难度。

编码时思路一定要理清,防止某些难以查出的错误出现。

线段树维护了区间中的最长连续空房数 tlen始于左端的最长连续空房数 llen终于右端的最长连续空房数 rlen

其中 llen 和 rlen 的计算分两种情况,即是否越过中点 smid = (l + r) / 2。见 Line 49~53

而 tlen 的有三种情况,即完全在左半部分,越过中点,和完全在右半部分。见 Line 55

线段树的 lazy 标记(Code 中称作 cov)记录的是将区间置空置满的操作。

注意询问时要求返回区间尽量靠左,所以要注意判断的先后顺序

Code: O((N+M)logN) [2716K, 610MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

inline int getint(){
	char ch; int num;
	while(!isdigit(ch = getchar()));
	num = ch - '0';
	while(isdigit(ch = getchar())) num = (num << 1) + (num << 3) + ch - '0';
	return num;
}

int N, M;

struct Node{
	int cov;  // Lazy Tag: -1 means no operation, 0 means to set empty, and 1 means to set full
	int tlen, llen, rlen;
};

struct Segment_Tree{
	#define MAXNODE 300005
	#define lch (u << 1)
	#define rch (u << 1 | 1)
	#define smid (l + r >> 1)
	Node node[MAXNODE];
	
	inline void build(int u, int l, int r){
		node[u].cov = -1;
		node[u].tlen = node[u].llen = node[u].rlen = r - l + 1;
		if(l == r) return;  // Or the function will fall into an endless loop
		if(l <= smid) build(lch, l, smid);
		if(smid < r) build(rch, smid + 1, r);
	}
	
	inline void pushdown(int u, int l, int r){
		if(node[u].cov != -1){
			node[lch].cov = node[rch].cov = node[u].cov;
			node[lch].tlen = node[lch].llen = node[lch].rlen = (node[u].cov) ? 0 : smid - l + 1;
			node[rch].tlen = node[rch].llen = node[rch].rlen = (node[u].cov) ? 0 : r - smid;  // Empty or fill the sub-region
			node[u].cov = -1;
		}
	}
	
	inline void update(int u, int l, int r){
		node[u].llen = node[lch].llen;
		if(node[u].llen == smid - l + 1) node[u].llen += node[rch].llen;  // llen exceeds half of total length
		
		node[u].rlen = node[rch].rlen;
		if(node[u].rlen == r - smid) node[u].rlen += node[lch].rlen;  // rlen exceeds half of total length
		
		node[u].tlen = max(max(node[lch].tlen, node[rch].tlen), node[lch].rlen + node[rch].llen);
		// Beware of this expression, which is not "max(max(node[u].llen, node[u].rlen), node[lch].rlen + node[rch].llen)" !!!
	}
	
	inline void modify(int u, int l, int r, int ml, int mr, int cov){
		if(l == ml && r == mr){
			node[u].tlen = node[u].llen = node[u].rlen = (node[u].cov = cov) ? 0 : r - l + 1;
			return;
		}
		pushdown(u, l, r);
		if(mr <= smid) modify(lch, l, smid, ml, mr, cov);
		else if(ml > smid) modify(rch, smid + 1, r, ml, mr, cov);
		else modify(lch, l, smid, ml, smid, cov), modify(rch, smid + 1, r, smid + 1, mr, cov);
		update(u, l, r);
	}
	
	inline int query(int u, int l, int r, int len){
		if(l == r) return l;
		pushdown(u, l, r);
		if(node[lch].tlen >= len) return query(lch, l, smid, len);
		else if(node[lch].rlen + node[rch].llen >= len) return smid - node[lch].rlen + 1;  // Return the start position immediately
		else return query(rch, smid + 1, r, len);
	}
} sgt;

int main(){
	N = getint(), M = getint();
	sgt.build(1, 1, N);
	for(register int i = 1; i <= M; i++){
		int op = getint();
		if(op == 1){
			int D = getint();
			if(sgt.node[1].tlen < D){
				puts("0");
				continue;
			}
			int pos = sgt.query(1, 1, N, D);
			printf("%d\n", pos);
			sgt.modify(1, 1, N, pos, pos + D - 1, 1);
		}
		else{
			int X = getint(), D = getint();
			sgt.modify(1, 1, N, X, X + D - 1, 0);
		}
	}
	return 0;
}

评论

还没有任何评论,你来说两句吧



常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
If the code cannot be displayed normally, please refresh the page.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai