POJ3225 Help with Intervals

来源:互联网 发布:怎么注册国外域名 编辑:程序博客网 时间:2024/05/21 03:03
题目链接
Help with Intervals
Time Limit: 6000MS Memory Limit: 131072KTotal Submissions: 12255 Accepted: 3078Case 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:

OperationNotation

Definition

UnionA ∪ B{x : x ∈ A or x ∈ B}IntersectionA ∩ B{x : x ∈ A and x ∈ B}Relative complementationA − B{x : x ∈ A but x ∉ B}Symmetric differenceA ⊕ 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:

CommandSemanticsU TS ← S ∪ TI TS ← S ∩ TD TS ← S − TC TS ← T − SS 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] (ab ∈ 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)


题目大意:

将一个空集进行题目所给出的5种操作,输出最终集合。

题解:

线段树维护一个区间是否全为1/0,以及是否被全部异或。
5种操作可以分别分解为:
区间全覆盖为1。
区间外全覆盖为0。
区间全覆盖为0。
C  区间外全覆盖为0,区间内异或。
区间内异或。
另外区间开闭的处理是将端点乘2,若左开则左+1,右开则右-1,最后输出时判断奇偶即可。
#include <cstdio>#include <iostream>using namespace std;#define lx (x<<1)#define rx ((x<<1)|1)#define mid ((l+r)>>1)const int N = 65535 + 10 << 1;int num[N<<2], Xor[N<<2];bool now[N+10];int X, Y;char q[1];void pxor(int x){    if(num[x] != -1) num[x] ^= 1;    else Xor[x] ^= 1;}void push_down(int x){    if(num[x] != -1){        num[lx] = num[rx] = num[x];        Xor[lx] = Xor[rx] = 0;        num[x] = -1;    }    if(Xor[x]){        pxor(lx); pxor(rx);        Xor[x] = 0;    }}void update(int l, int r, int x){    if(X <= l && r <= Y){        if(q[0] == 'U') {            num[x] = 1;            Xor[x] = 0;        }        else if(q[0] == 'D'){            num[x] = 0;            Xor[x] = 0;        }        else if(q[0] == 'C' || q[0] == 'S') pxor(x);        return;    }    push_down(x);    if(X <= mid) update(l, mid, lx);    else if(q[0] == 'I' || q[0] == 'C') num[lx] = Xor[lx] = 0;    if(Y > mid) update(mid+1, r, rx);    else if(q[0] == 'I' || q[0] == 'C') num[rx] = Xor[rx] = 0;}void query(int l, int r, int x){    if(num[x] == 1){        for(int i = l; i <= r; i++) now[i] = 1;        return;    }    else if(num[x] == 0) return;    if(l == r) return;    push_down(x);    query(l, mid, lx);    query(mid+1, r, rx);}int main(){    char lc, rc;    int a, b;    while(~scanf("%s %c%d,%d%c\n", q, &lc, &a, &b, &rc)){        X = a << 1; Y = b << 1;        if(lc == '(') X++; if(rc == ')') Y--;        update(0, N, 1);    }    query(0, N, 1);    a = -1;    bool flag = false;    for(int i = 0; i <= N; i++){        if(now[i]){            if(a == -1) a = i;            b = i;        }        else{            if(a != -1){                if(flag) putchar(' ');                printf("%c%d,%d%c",a&1?'(':'[', a>>1, (b+1)>>1, b&1?')':']');                a = -1;                flag = true;            }        }    }    if(!flag) puts("empty set");    else puts("");    return 0;}


0 0
原创粉丝点击