poj 3225 Help with Intervals -線段樹-延遲標記-區間交併補

Help with Intervals
Time Limit: 6000MS   Memory Limit: 131072K
Total Submissions: 13257   Accepted: 3350
Case Time Limit: 2000MS

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:

Operation Notation

Definition

Union AB {x : xA or xB}
Intersection AB {x : xA and xB}
Relative complementation AB {x : xA but x B}
Symmetric difference AB (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:

Command Semantics
U T SST
I T SST
D T SST
C T STS
S T SST

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

X T

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)


/*
集合(a,b), (a,b], [a,b) [a,b]的交併補差
用0和1表示是否包含區間,-1表示該區間內既有包含又有不包含
並   U:把區間[l,r]覆蓋成1,
交   I:把[-∞,l)(r,∞]覆蓋成0,
s-t   D:把區間[l,r]覆蓋成0,
t-s   C:把[-∞,l)(r,∞]覆蓋成0 , 且[l,r]區間0/1互換
異或  S:[l,r]區間0/1互換

比較特殊的就是區間0/1互換這個操作,我們可以稱之爲異或操作
很明顯我們可以知道這個性質:當一個區間被覆蓋後,不管之前有沒有異或標記都沒有意義了
所以當一個節點得到覆蓋標記時把異或標記清空
而當一個節點得到異或標記的時候,先判斷覆蓋標記,
如果是0或1,直接改變一下覆蓋標記,
不然的話改變異或標記
開閉區間只要數字乘以2就可以處理,偶數表示[],奇數表示()

*/
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1

const int maxn = 131072;
bool hash[maxn];
int cover[maxn<<2];
int XOR[maxn<<2];
void FXOR(int rt) {///給rt進行異或操作
	if (cover[rt] != -1) cover[rt] ^= 1;///先覆蓋-異或-覆蓋,相當於異或-覆蓋
	else XOR[rt] ^= 1;///取消這個標記,異或偶數次等於不做
}
void PushDown(int rt) {
	if (cover[rt] != -1) {
		cover[rt<<1] = cover[rt<<1|1] = cover[rt];
		XOR[rt<<1] = XOR[rt<<1|1] = 0;
		cover[rt] = -1;
	}
	if (XOR[rt]) {
		FXOR(rt<<1);
		FXOR(rt<<1|1);
		XOR[rt] = 0;
	}
}
void update(char op,int L,int R,int l,int r,int rt) {
	if (L <= l && r <= R) {
		if (op == 'U') {
			cover[rt] = 1;
			XOR[rt] = 0;
		} else if (op == 'D') {
			cover[rt] = 0;
			XOR[rt] = 0;
		} else if (op == 'C' || op == 'S') {
			FXOR(rt);
		}
		return ;
	}
	PushDown(rt);
	int m = (l + r) >> 1;
	if (L <= m) update(op , L , R , lson);
	else if (op == 'I' || op == 'C') {///區間在右兒子處則左兒子置零
		XOR[rt<<1] = cover[rt<<1] = 0;
	}
	if (m < R) update(op , L , R , rson);
	else if (op == 'I' || op == 'C') {///區間在左兒子處則右兒子置零
		XOR[rt<<1|1] = cover[rt<<1|1] = 0;
	}
}
void query(int l,int r,int rt) {
	if (cover[rt] == 1) {
		for (int it = l ; it <= r ; it ++) {
			hash[it] = true;
		}
		return ;
	} else if (cover[rt] == 0) return ;

	PushDown(rt);
	int m = (l + r) >> 1;
	query(lson);
	query(rson);
}
int main() {
	cover[1] = XOR[1] = 0;
	char op , l , r;
	int a , b;
	while ( ~scanf("%c %c%d,%d%c\n",&op , &l , &a , &b , &r) ) {
		a <<= 1 , b <<= 1;
		if (l == '(') a ++;
		if (r == ')') b --;
		if (a > b) {///空集
			if (op == 'C' || op == 'I') {
				cover[1] = XOR[1] = 0;
			}
		} else update(op , a , b , 0 , maxn , 1);
	}
	query(0 , maxn , 1);
	bool flag = false;
	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 = true;
				printf("%c%d,%d%c",s&1?'(':'[' , s>>1 , (e+1)>>1 , e&1?')':']');
				s = -1;
			}
		}
	}
	if (!flag) printf("empty set");
	puts("");
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章