POJ-3225 Help with Intervals
2014-08-26 17:34
435 查看
[align=center]Help with Intervals[/align]
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:
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 setS, which starts out empty and is modified as specified by the following
commands:
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
where
b ∈Z, 0 ≤ a ≤ b ≤ 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. IfS
is empty, just print “
Sample Input
Sample Output
————————————————————集训26.1的分割线————————————————————
前言:补考的准备算是差不多了,毕竟放弃了傅里叶级数和热力学。。。根本看不懂。。。回到实验室。
思路:区间交并补等的线段树模板吧……根据胡大大的理解
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)这种区间并不是空区间。要表示开区间和闭区间,只需要把每个点都拆成两个点——拆点,然后(2, 3)就变成了[5, 5],[2, 3]就变成了[4, 6]。如下:
[a (a , b) b]
[a*2 (a*2+1 , b*2-1) b*2]
类比一下哈希的思想就是:
2 2 ~ 3 3
↓ ↓
↓
4 5 6
代码如下:
Time Limit: 6000MS | Memory Limit: 131072K | |
Case Time Limit: 2000MS |
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 | A ∪ B | {x : x ∈ A or x ∈ B} |
Intersection | A ∩ B | {x : x ∈ A and x ∈ B} |
Relative complementation | A − B | {x : x ∈ A but x ∉ B} |
Symmetric difference | A ⊕ B | (A − B) ∪ (B − A) |
commands:
Command | Semantics |
---|---|
UT | S ← S ∪ T |
IT | S ← S ∩ T |
DT | S ← S − T |
CT | S ← T − S |
ST | S ← S ⊕ T |
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
Xis one of ‘
U’, ‘
I’, ‘
D’, ‘
C’ and ‘
S’ andT is an interval in one of the forms
(a
,b
),
(a
,b
],
[a
,b
)and
[a
,b
](a,
b ∈Z, 0 ≤ a ≤ b ≤ 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. IfS
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)
————————————————————集训26.1的分割线————————————————————
前言:补考的准备算是差不多了,毕竟放弃了傅里叶级数和热力学。。。根本看不懂。。。回到实验室。
思路:区间交并补等的线段树模板吧……根据胡大大的理解
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)这种区间并不是空区间。要表示开区间和闭区间,只需要把每个点都拆成两个点——拆点,然后(2, 3)就变成了[5, 5],[2, 3]就变成了[4, 6]。如下:
[a (a , b) b]
[a*2 (a*2+1 , b*2-1) b*2]
类比一下哈希的思想就是:
2 2 ~ 3 3
↓ ↓
↓
4 5 6
代码如下:
/* ID: j.sure.1 PROG: LANG: C++ */ /****************************************/ #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <stack> #include <queue> #include <vector> #include <map> #include <string> #include <climits> #include <iostream> #define INF 0x3f3f3f3f using namespace std; /****************************************/ #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 const int maxn = 2e5;//65535*2+2; int cover[maxn<<2]; bool oppo[maxn<<2], vis[maxn]; void toOP(int rt) {//翻转操作 if(cover[rt] != -1) { cover[rt] = !cover[rt]; } else oppo[rt] = !oppo[rt]; } void Down(int rt) { if(cover[rt] != -1) { cover[rt<<1] = cover[rt<<1|1] = cover[rt]; oppo[rt<<1] = oppo[rt<<1|1] = 0; cover[rt] = -1; } //没有覆盖标记但是有翻转标记 if(oppo[rt]) { toOP(rt<<1); toOP(rt<<1|1); oppo[rt] = 0; } } void update(char op, int L, int R, int l, int r, int rt) { if(L <= l && r <= R) { switch(op) { case 'U': cover[rt] = 1; oppo[rt] = 0; break; case 'D': cover[rt] = 0; oppo[rt] = 0; break; case 'S': toOP(rt); break;//翻转01 case 'C': toOP(rt); break;//翻转01而且补集用0覆盖 } return ; } Down(rt); int m = (l+r)>>1; if(L <= m) update(op, L, R, lson); else {//否则就是补集 if(op == 'I' || op == 'C') { cover[rt<<1] = oppo[rt<<1] = 0; }//覆盖补集 } if(m < R) update(op, L, R, rson); else { if(op == 'I' || op == 'C') { cover[rt<<1|1] = oppo[rt<<1|1] = 0; } } } void Cnt(int l, int r) { for(int i = l; i <= r; i++) { vis[i] = true; } } void query(int l, int r, int rt) { if(cover[rt] == 1) { Cnt(l, r);//数区间 return ; } else if(!cover[rt]) return ;//就像是剪枝一样 if(l == r) return;//边界 Down(rt); int m = (l+r) >> 1; query(lson); query(rson); } int main() { #ifdef J_Sure // freopen("000.in", "r", stdin); // freopen(".out", "w", stdout); #endif char op, ll, rr; int a, b; while(~scanf("%c", &op)) { #ifdef J_Sure if(op == 'E') break; #endif scanf(" %c%d,%d%c\n", &ll, &a, &b, &rr); a <<= 1; b <<= 1; if(ll == '(') a ++; if(rr == ')') b --;//处理开闭区间的方法:拆点 update(op, a, b, 0, maxn, 1); } query(0, maxn, 1); bool ept = true; char c; //接下来是数区间 for(int i = 0; i <= maxn; ) { if(vis[i]) { c = i&1 ? '(' : '['; if(!ept) printf(" "); ept = false; printf("%c%d,", c, i/2); while(vis[i+1]) i++; c = i&1 ? ')' : ']'; printf("%d%c", (i+1)/2, c); } i++; } if(ept) puts("empty set"); return 0; }
相关文章推荐
- Poj 3225 Help with Intervals
- POJ 3225 - Help with Intervals(线段树)
- POJ - 3225 - Help with Intervals 【区间的并、交、差、对称差】
- poj 3225 Help with Intervals 线段树lazy-tag求解区间运算
- POJ 3225 Help with Intervals
- poj 3225 Help with Intervals 线段树 集合的交并差补运算
- poj 3225 Help with Intervals
- POJ 3225 (线段树 区间更新) Help with Intervals
- poj 3225 Help with Intervals(线段树)
- POJ 3225 Help with Intervals
- poj-3225-Help with Intervals
- POJ 3225 Help with Intervals(线段树)
- POJ 3225 Help with Intervals
- POJ 3225 Help with Intervals 线段树 成段更新
- POJ 3225 Help with Intervals 线段树
- POJ 3225 Help with Intervals 线段树区间操作
- POJ 3225 Help with Intervals(区间更新 + 倍增区间)
- poj 3225 Help with Intervals(线段树进阶,处理区间,拆点)
- POJ 3225 Help with Intervals --线段树区间操作
- POJ 3225 Help with Intervals