POJ-3225 Help with Intervals (open and closed Intervals)

Source: Internet
Author: User

POJ-3225 Help with Intervals (open and closed 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 semantic Ric 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 ABytesB {X:XεAOrXεB}
Intersection ABytesB {X:XεAAndXεB}
Relative complementation A?B {X:XεABut <script lang="javascript" src=""> Document. write (navigator. userAgent. indexOf ("MSIE 6.0 ")! =-1? "Not X":" X? "); B}
Symmetric difference ABytesB (A?B) Begin (B?A)

Ikki has got acted 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:

Command Semantics
UT SBytesSBytesT
IT SBytesSBytesT
DT SBytesS?T
CT SBytesT?S
ST SBytesSBytesT

Input

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

XT

WhereXIs one'U','I','D','CAnd'S'AndTIs an interval in one of the forms(A,B),(A,B],[A,B)And[A,B](A,BεZ, 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 setSAs it is after the last command is executed as the union of a minimal collection of disjoint intervals. the intervals shoshould be printed on one line separated by single spaces and appear in increasing order of their endpoints. ifSIs 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)

Question: What is the set after a series of operations?

Idea: open and closed interval line segment tree:

U: overwrite the interval [l, r] to 1.
I: overwrite [-∞, l) (r, ∞] to 0.
D: overwrite the range [l, r] to 0.
C: overwrite [-∞, l) (r, ∞] to 0, and the [l, r] range is 0/1 interchangeable.
S: [l, r] 0/1 Interchange

If you understand the meaning of a question, you will lose half of it. Note that when we are sure we want to cover it, we don't have to care about its differences or values, another way to deal with the open and closed intervals is to set the range * 2, the even number of subscripts is closed, and the odd number is open, which is equivalent to: the original [3, 4] is changed to [6, 8], if it is (3, 4]-> (7, 8], we can handle the interval issue well.

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





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.