POJ 3225 Help with Intervals(线段树)

POJ 3225 Help with Intervals

题目链接

集合数字有的为1,没有为0,那么几种操作相应就是置为0或置为1或者翻转,这个随便推推就能够了,然后开闭区间的处理方式就是把区间扩大成两倍,偶数存点,奇数存线段就可以

代码:

#include <cstdio>
#include <cstring>

#define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2)

const int N = 65536 * 2;

struct Node {
	int l, r, flip, setv;
} node[N * 4];

int to[N];

void build(int l, int r, int x = 0) {
	node[x].l = l; node[x].r = r;
	node[x].flip = 0; node[x].setv = -1;
	if (l == r) {
		to[l] = x;
		return;
	}
	int mid = (l + r) / 2;
	build(l, mid, lson(x));
	build(mid + 1, r, rson(x));
}

void pushdown(int x) {
	if (node[x].setv != -1) {
		node[lson(x)].setv = node[rson(x)].setv = node[x].setv;
		node[lson(x)].flip = node[rson(x)].flip = 0;
		node[x].setv = -1;
	}
	if (node[x].flip) {
		node[lson(x)].flip ^= 1;
		node[rson(x)].flip ^= 1;
		node[x].flip = 0;
	}
}

void add(int l, int r, int v, int x = 0) {
	if (l > r) return;
	if (node[x].l >= l && node[x].r <= r) {
		if (v != -1) {
			node[x].setv = v;
			node[x].flip = 0;
		} else
			node[x].flip ^= 1;
		return;
	}
	pushdown(x);
	int mid = (node[x].l + node[x].r) / 2;
	if (l <= mid) add(l, r, v, lson(x));
	if (r > mid) add(l, r, v, rson(x));
}

void query(int x = 0) {
	if (node[x].l == node[x].r) {
		if (node[x].setv == -1) node[x].setv = 0;
		return;
	}
	pushdown(x);
	int mid = (node[x].l + node[x].r) / 2;
	query(lson(x));
	query(rson(x));
}

char c, a, b;
int l, r;

int main() {
	build(0, N - 1);
	while (~scanf("%c %c%d,%d%c\n", &c, &a, &l, &r, &b)) {
		l = l * 2 + (a == '(');
		r = r * 2 - (b == ')');
		if (c == 'U') add(l, r, 1);
		if (c == 'I' || c == 'C') {
			add(0, l - 1, 0);
			add(r + 1, N - 1, 0);
			if (c == 'C') add(l, r, -1);
		}
		if (c == 'D') add(l, r, 0);
		if (c == 'S') add(l, r, -1);
	}
	query();
	int pre = 0, flag = 0, bo = 0;
	for (int i = 0; i < N; i++) {
		int id = to[i];
		int tmp = (node[id].setv^node[id].flip);
		if (!tmp && flag) {
			if (bo) printf(" ");
			else bo = 1;
			if (pre % 2) printf("(");
			else printf("[");
			printf("%d,%d", pre / 2, i / 2);
			if (i % 2 == 0) printf(")");
			else printf("]");
			flag = 0;
		} else if (tmp && !flag) {
			pre = i;
			flag = 1;
		}
	}
	if (bo == 0) printf("empty set");
	printf("\n");
	return 0;
}


posted @ 2017-06-16 14:24  yfceshi  阅读(214)  评论(0编辑  收藏  举报