标签:des style java os strong io for art
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 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 <script lang="javascript" src="">document.write(navigator.userAgent.indexOf("MSIE 6.0")!=-1?"not x ∈":"x ?"); B} Symmetric difference A ⊕ B (A ? B) ∪ (B ? A)
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
TS ← S ∪ T I
TS ← S ∩ T D
TS ← S ? T C
TS ← T ? S S
TS ← S ⊕ T
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, 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. 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; }
POJ - 3225 Help with Intervals (开闭区间),布布扣,bubuko.com
POJ - 3225 Help with Intervals (开闭区间)
标签:des style java os strong io for art
原文地址:http://blog.csdn.net/u011345136/article/details/38360635