POJ 3667 Hotel(线段树:区间覆盖+维护最大连续子区间长度)

Hotel

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 21673   Accepted: 9442

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 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

Source

 

 

USACO 2008 February Gold

 

题意:

长度为n的区间,m个操作,一开始都是0

1 x表示求出长度为x的0的连续区间的最左端,并把这个区间变成1

2 x y表示将区间[x,y]变成0

分析:

线段树区间合并的入门题,注意pushdow和pushup操作。

区间赋值:需要懒惰标记,题意需要覆盖标记,直接flag充当

整体查询(全部的)

#include 
#include 
#include 
using namespace std;
const int N = 500006, INF = 0x3f3f3f3f;
int n, m, a[N];
typedef long long ll;
struct T {
	//int l, r, sum, lx, rx, ans;
	//1.两个操作:(l l r)求l和r的最大连续字段和,(0,x,y):a[x]修改成y
    //区间和sum,区间最大连续字段和ans,
	//紧靠左端的最大连续字段和lx,紧靠右端的最大连续字段和rx
	int l,r,ls,rs,sum,flag;
	//2.两个操作:(l len)求整个序列满足长度为len连续子区间度,返回左端点;(2,x,len):清除a[x]~a[x+len-1]
	//懒惰笔记+覆盖标记flag:为0时表示没有被覆盖,为1时表示被覆盖了,为-1时表示子节点中既有被覆盖的也有没被覆盖的.
} tree[N*4];
void  pushup(int p){
	//tree[p].sum = tree[p<<1].sum + tree[p<<1|1].sum;
	//tree[p].lx = max(tree[p<<1].lx, tree[p<<1].sum + tree[p<<1|1].lx);
	//tree[p].rx = max(tree[p<<1|1].rx, tree[p<<1].rx + tree[p<<1|1].sum);
    //tree[p].ans = max(max(tree[p<<1].ans, tree[p<<1|1].ans), tree[p<<1].rx + tree[p<<1|1].lx); 
    tree[p].ls = tree[p<<1].ls;
	tree[p].rs = tree[p<<1|1].rs;
	if (tree[p<<1].ls == tree[p<<1].r - tree[p<<1].l + 1) 
		tree[p].ls += tree[p<<1|1].ls;
	if (tree[p<<1|1].rs == tree[p<<1|1].r - tree[p<<1|1].l + 1)
	    tree[p].rs += tree[p<<1].rs;
	    
	tree[p].sum = max(max(tree[p<<1].sum, tree[p<<1|1].sum), tree[p<<1].rs + tree[p<<1|1].ls);
}
//1问题未涉及到区间更新不需要pushdown
void pushdown(int p){

    if (tree[p].flag != -1)
	{
		tree[p<<1].flag = tree[p<<1|1].flag = tree[p].flag;
	    if (tree[p<<1].flag) {
		     tree[p<<1].ls = tree[p<<1].rs = tree[p<<1].sum = tree[p<<1].r - tree[p<<1].l + 1;
		     tree[p<<1|1].ls = tree[p<<1|1].rs = tree[p<<1|1].sum = tree[p<<1|1].r - tree[p<<1|1].l + 1;
	    } else 
	    tree[p<<1].ls = tree[p<<1].rs = tree[p<<1].sum = tree[p<<1|1].ls = tree[p<<1|1].rs = tree[p<<1|1].sum = 0;
	    tree[p].flag = -1;
	}
}
void build(int p, int l, int r) {
	
	tree[p].l = l;
	tree[p].r = r;
	tree[p].sum = tree[p].ls = tree[p].rs = r - l + 1;
	tree[p].flag = -1;
	if (l == r) {
		//tree[p].sum = tree[p].lx = tree[p].rx = tree[p].ans = a[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(p << 1, l, mid);
	build(p << 1 | 1, mid + 1, r);
	pushup(p);
}

//1问题单点赋值,a[x]=y 
/*void change1(int p, int x, int y) {
	if (tree[p].l == tree[p].r) {
		tree[p].sum = tree[p].lx = tree[p].rx = tree[p].ans = y;
		return;
	}
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (x <= mid) change1(p << 1, x, y);
	else change1(p << 1 | 1, x, y);
	pushup(p);
} */
//2问题的区间赋值
void change2(int p, int l, int r,ll val) {
	if (tree[p].l >= l && tree[p].r <= r) {
		if(val==0)
		tree[p].ls = tree[p].rs = tree[p].sum = tree[p].flag = 0;
	
		if(val==1)
		{
			tree[p].ls = tree[p].rs = tree[p].sum = tree[p].r - tree[p].l + 1;
			tree[p].flag=1;
		}
		return;
	}
	pushdown(p);
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (l <= mid) change2(p << 1, l, r,val);
	if (r > mid) change2(p << 1 | 1, l, r,val);
	pushup(p);
}
//2问题的整个序列的查询最大子序列长度
int ask1(int p, int d) {
	if (tree[p].l == tree[p].r) return tree[p].l;
	 pushdown(p);
	if (tree[p<<1].sum >= d) return ask1(p << 1, d);
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (tree[p<<1].rs + tree[p<<1|1].ls >= d) return mid - tree[p<<1].rs + 1;
	if (tree[p<<1|1].sum >= d) return ask1(p << 1 | 1, d);
	return -1;
}
/*
//问题1的查询l和r之间最大子序列和
T ask(int p, int l, int r) {
	if (l <= tree[p].l && r >= tree[p].r) return tree[p];
	T a, b, ans;
	a.sum = a.lx = a.rx = a.ans = b.sum = b.lx = b.rx = b.ans = -INF;
	ans.sum = 0;
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (l <= mid) {
		a = ask(p << 1, l, r);
		ans.sum += a.sum;
	}
	if (r > mid) {
		b = ask(p << 1 | 1, l, r);
		ans.sum += b.sum;
	}
	ans.ans = max(max(a.ans, b.ans), a.rx + b.lx);
	ans.lx = max(a.lx, a.sum + b.lx);
	ans.rx = max(b.rx, b.sum + a.rx);
	if (l > mid) ans.lx = max(ans.lx, b.lx);
	if (r <= mid) ans.rx = max(ans.rx, a.rx);
	return ans;
}
 */
int main() {
	cin >> n >> m;
	build(1, 1, n);
	while (m--) {
		int op, x, d;
		scanf("%d", &op);
		if (op == 1) {
			scanf("%d", &d);
			if (tree[1].sum < d) puts("0");
			else {
				int ans = ask1(1, d);
				printf("%d\n", ans);
				change2(1, ans, ans + d - 1,0);
			}
		} else {
			scanf("%d %d", &x, &d);
			change2(1, x, x + d - 1,1);
		}
	}
	return 0;
}

 

你可能感兴趣的:(数据结构--线段树)