POJ - 3225 Help with Intervals (开闭区间)

Description

LogLoader, Inc. is a company specialized in providing products for analyzing logs. While Ikki is working on graduation design, he is also engaged in an internship at LogLoader. Among his tasks, one is to write a module for manipulating time intervals, which have confused him a lot. Now he badly needs your help.

In discrete mathematics, you have studied several basic set operations, namely union, intersection, relative complementation and symmetric difference, which naturally apply to the specialization of sets as intervals.. For your quick reference they are summarized in the table below:

OperationNotation

Definition

UnionAB{x : xA or xB}
IntersectionAB{x : xA and xB}
Relative complementationAB{x : xA but <script lang="javascript" src="">document.write(navigator.userAgent.indexOf("MSIE 6.0")!=-1?"not x ∈":"x ∉"); B}
Symmetric differenceAB(AB) ∪ (BA)

Ikki has abstracted the interval operations emerging from his job as a tiny programming language. He wants you to implement an interpreter for him. The language maintains a set S, which starts out empty and is modified as specified by the following commands:

CommandSemantics
UTSST
ITSST
DTSST
CTSTS
STSST

Input

The input contains exactly one test case, which consists of between 0 and 65,535 (inclusive) commands of the language. Each command occupies a single line and appears like

XT

where X is one of ‘U’, ‘I’, ‘D’, ‘C’ and ‘S’ and T is an interval in one of the forms (a,b), (a,b], [a,b) and [a,b] (a, bZ, 0 ≤ ab ≤ 65,535), which take their usual meanings. The commands are executed in the order they appear in the input.

End of file (EOF) indicates the end of input.

Output

Output the set S as it is after the last command is executed as the union of a minimal collection of disjoint intervals. The intervals should be printed on one line separated by single spaces and appear in increasing order of their endpoints. If S is empty, just print “empty set” and nothing else.

Sample Input

U [1,5]
D [3,3]
S [2,4]
C (1,5)
I (2,3]

Sample Output

(2,3)

题意:求经过一系列操作后的集合是什么

思路:开闭区间线段树:

                                     U:把区间[l,r]覆盖成1
                                     I:把[-∞,l)(r,∞]覆盖成0
                                     D:把区间[l,r]覆盖成0
                                    C:把[-∞,l)(r,∞]覆盖成0 , 且[l,r]区间0/1互换
                                    S:[l,r]区间0/1互换

看懂题目意思就赢了一半了,注意的一点是当我们确定是要覆盖的时候就不用在乎它的异或值了,还有处理开闭区间的方法是将区间*2,下标偶数的是闭,奇数是开,相当于: 原本[3, 4] 变成了[6, 8] ,如果是(3, 4]-> (7, 8],这样我们就可以很好的处理区间问题了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson(x) ((x) << 1)
#define rson(x) ((x) << 1 | 1)
using namespace std;
const int maxn =  (65535<<1) + 5;

int hash[maxn<<1];
struct seg {
	int w;
	int v;
};

struct segment_tree {
	seg node[maxn<<2];

	void Fxor(int pos)  {
		if (node[pos].w != -1)
			node[pos].w ^= 1;
		else node[pos].v ^= 1;
	}

	void build(int l, int r, int pos) {
		node[pos].w = node[pos].v = 0;
		if (l == r)
			return;
		int m = l + r >> 1;
		build(l, m, lson(pos));
		build(m+1, r, rson(pos));
	}

	void push(int pos) {
		if (node[pos].w != -1) {
			node[lson(pos)].w = node[rson(pos)].w = node[pos].w;
			node[lson(pos)].v = node[rson(pos)].v = 0;
			node[pos].w = -1;
		}
		if (node[pos].v) {
			Fxor(lson(pos));
			Fxor(rson(pos));
			node[pos].v = 0;
		}
	}

	void modify(int l, int r, int pos, int x, int y, char op) {
		if (x <= l && y >= r) {
			if (op == 'U') {
				node[pos].w = 1;
				node[pos].v = 0;
			} else if (op == 'D') 
				node[pos].w = node[pos].v = 0;
			else if (op == 'C' || op == 'S')
				Fxor(pos);
			return;
		}
		push(pos);
		int m = l + r >> 1;
		if (x <= m)
			modify(l, m, lson(pos), x, y, op);
		else if (op == 'I' || op == 'C')
			node[lson(pos)].w = node[lson(pos)].v = 0;
		if (y > m)
			modify(m+1, r, rson(pos), x, y, op);
		else if (op == 'I' || op == 'C')
			node[rson(pos)].w = node[rson(pos)].v = 0;
	}

	void query(int l, int r, int pos) {
		if (node[pos].w == 1) {
			for (int i = l; i <= r; i++)
				hash[i] = 1;
			return;
		} else if (node[pos].w == 0)
			return;
		push(pos);
		int m = l + r >> 1;
		query(l, m, lson(pos));
		query(m+1, r, rson(pos));
	}

} tree;

int main() {
	char op, l, r;
	int a, b;
	tree.build(0, maxn, 1);
	while (scanf("%c %c%d,%d%c\n", &op, &l, &a, &b, &r) != EOF) {
		a <<= 1, b <<= 1;
		if (l == '(')
			a++;
		if (r == ')')
			b--;
		if (a > b) {
			if (op == 'C' || op == 'I')
				tree.node[1].w = tree.node[1].v = 0;
		}
		else tree.modify(0, maxn, 1, a, b, op);
	}
	memset(hash, 0, sizeof(hash));
	tree.query(0, maxn, 1);
	int flag = 0;
	int s = -1, e;
	for (int i = 0; i < maxn; i++) {
		if (hash[i]) {
			if (s == -1)
				s = i;
			e = i;
		}
		else {
			if (s != -1) {
				if (flag)
					printf(" ");
				flag = 1;
				printf("%c%d,%d%c", s&1?'(':'[', s>>1, (e+1)>>1, e&1?')':']');
				s = -1;
			}
		} 
	}
	if (!flag)
		printf("empty set");
	puts("");
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值