(Relax 线段树1.3)POJ 3667 Hotel(互不相交的线段的更新与统计)


#include <iostream>
#include <cstdio>

#define maxn 80010
#define Fup(i, s, t) for (int i = s; i <= t; i ++)
#define Fdn(i, s, t) for (int i = s; i >= t; i --)

using namespace std;

struct node {
	int ls;//左端空区间的长度
	int rs;//右端空区间的长度
	int  ms;//最长子区间的长度
	int pos;//开始位置
	int mark;//懒惰标记(0:未定 , 1:全空  2:全满)
} tree[4 * maxn];//线段树
int n, m;

void build_tree(int l, int r, int i) {//构建全空的线段树
	tree[i].ls = tree[i].rs = tree[i].ms = r - l + 1;
	tree[i].pos = l;
	if (l == r)
		return;
	int mid = (l + r) / 2;
	build_tree(l, mid, i + i);
	build_tree(mid + 1, r, i + i + 1);
}

bool all_space(int l, int r, int i) {//判断根为i的区间[l,r]是否全空...
	if (tree[i].ls == r - l + 1)
		return 1;
	return 0;
}

void update(int l, int r, int i) {//通过标记法维护线段树..
	if (!tree[i].mark)
		return;
	if (tree[i].mark == 1) {
		int len = r - l + 1;
		tree[i + i].ls = tree[i + i].rs = tree[i + i].ms = (len + 1) / 2;
		tree[i + i].pos = l;
		tree[i + i + 1].ls = tree[i + i + 1].rs = tree[i + i + 1].ms = len / 2;
		tree[i + i + 1].pos = (l + r) / 2 + 1;
		tree[i + i].mark = tree[i + i + 1].mark = 1;
	} else {
		tree[i + i].ls = tree[i + i].rs = tree[i + i].ms = 0;
		tree[i + i].pos = l;
		tree[i + i + 1].ls = tree[i + i + 1].rs = tree[i + i + 1].ms = 0;
		tree[i + i + 1].pos = (l + r) / 2 + 1;
		tree[i + i].mark = tree[i + i + 1].mark = 2;
	}
	tree[i].mark = 0;
}

/**
 * 查询根为i的区间[l,r]是否存在长度为d的区间..
 * 如果存在则返回返回其左指针..
 * 否则返回0
 */
int query(int d, int l, int r, int i) {
	update(l, r, i);
	if (tree[i].ms < d)
		return 0;
	if (tree[i].ms == d)
		return tree[i].pos;
	int mid = (l + r) / 2;
	if (tree[i + i].ms >= d)
		return query(d, l, mid, i + i);
	if (tree[i + i].rs + tree[i + i + 1].ls >= d)
		return mid - tree[i + i].rs + 1;
	return query(d, mid + 1, r, i + i + 1);
}

/**
 * 在根为i的区间[l,r]上插入或删除子区间[tl,tr],插删标记为flag
 */
void change(int tl, int tr, int l, int r, int i, bool flag) {
	if (tl > r || tr < l)
		return;
	if (tl <= l && r <= tr) {
		if (flag) {
			tree[i].ls = tree[i].rs = tree[i].ms = 0;
			tree[i].pos = l;
			tree[i].mark = 2;
		} else {
			tree[i].ls = tree[i].rs = tree[i].ms = r - l + 1;
			tree[i].pos = l;
			tree[i].mark = 1;
		}
		return;
	}
	update(l, r, i);
	int mid = (l + r) / 2;
	change(tl, tr, l, mid, i + i, flag);
	change(tl, tr, mid + 1, r, i + i + 1, flag);
	tree[i].ls = tree[i + i].ls;
	if (all_space(l, mid, i + i))
		tree[i].ls += tree[i + i + 1].ls;
	tree[i].rs = tree[i + i + 1].rs;
	if (all_space(mid + 1, r, i + i + 1))
		tree[i].rs += tree[i + i].rs;
	tree[i].ms = max(tree[i + i].rs + tree[i + i + 1].ls,
			max(tree[i + i].ms, tree[i + i + 1].ms));
	if (tree[i].ms == tree[i + i].ms)
		tree[i].pos = tree[i + i].pos;
	else if (tree[i].ms == tree[i + i].rs + tree[i + i + 1].ls)
		tree[i].pos = mid - tree[i + i].rs + 1;
	else
		tree[i].pos = tree[i + i + 1].pos;
}

int main() {

	while (scanf("%d%d", &n, &m) != EOF) {
		memset(tree, 0, sizeof(tree));
		build_tree(1, n, 1);

		int i;
		for (i = 1; i <= m; ++i) {
			int kind;
			scanf("%d", &kind);

			if (kind == 1) {
				int d;
				scanf("%d", &d);

				int ans = query(d, 1, n, 1);

				printf("%d\n", ans);
				if (ans) {
					change(ans, ans + d - 1, 1, n, 1, 1);
				}
			} else {
				int x, d;
				scanf("%d%d", &x, &d);

				change(x, x + d - 1, 1, n, 1, 0);
			}
		}
	}

	return 0;
}


你可能感兴趣的:((Relax 线段树1.3)POJ 3667 Hotel(互不相交的线段的更新与统计))