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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值