zoukankan      html  css  js  c++  java
  • poj 3225 Help with Intervals

    Help with Intervals
    Time Limit: 6000MS   Memory Limit: 131072K
    Total Submissions: 7745   Accepted: 1789
    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:

    OperationNotation

    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)

    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:

    CommandSemantics
    U T S ← S ∪ T
    I T S ← S ∩ T
    D T S ← S − T
    C T S ← T − S
    S T S ← 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)
     1 #include <cstdio>
     2 using namespace std;
     3 #define lson l, m, rt<<1
     4 #define rson m+1, r, rt<<1|1
     5 const int maxn = 65535*2+10;
     6 int cover[maxn<<2],xorm[maxn<<2];
     7 bool hash[maxn+10];
     8 void fxor(int rt){
     9   if (cover[rt] != -1) cover[rt] ^= 1;
    10   else xorm[rt] ^= 1;
    11 }
    12 void PushDown(int rt){
    13   if (cover[rt] != -1){
    14     cover[rt<<1] = cover[rt<<1|1] = cover[rt];
    15     xorm[rt<<1] = xorm[rt<<1|1] = 0; cover[rt] = -1; // there has been a BUG.
    16   }
    17   if (xorm[rt]){
    18     fxor(rt<<1); fxor(rt<<1|1); xorm[rt] = 0;
    19   }
    20 }
    21 void update(char op, int L, int R, int l, int r, int rt){
    22   if (L <= l && R >= r){
    23     if (op == 'U') {cover[rt] = 1; xorm[rt] = 0;}
    24     else if (op == 'D') {cover[rt] = 0; xorm[rt] = 0;}
    25     else if (op == 'C' || op == 'S') {
    26       if (cover[rt] != -1) cover[rt] ^= 1;
    27       else xorm[rt] ^= 1;
    28     }
    29     return;
    30   }
    31   PushDown(rt); int m = (l + r) >> 1;
    32   if (L <= m) update(op, L, R, lson);
    33   else if (op == 'I' || op == 'C'){cover[rt<<1] = xorm[rt<<1] = 0;}
    34   if (R > m) update(op, L, R, rson);
    35   else if (op == 'I' || op == 'C'){cover[rt<<1|1] = xorm[rt<<1|1] = 0;}
    36 }
    37 void query(int l, int r, int rt){
    38   if (cover[rt] == 1)
    39   {
    40     for (int i = l; i <= r; ++i){
    41       hash[i] = true;
    42     }
    43     return; // there had been a BUG.
    44   }
    45   else if (cover[rt] == 0) return;
    46   if (l == r) return;
    47   PushDown(rt); int m = (l + r) >> 1;
    48   query(lson); query(rson);
    49 }
    50 int main(void){
    51   char op, l, r; int a, b;
    52   cover[1] = xorm[1] = 0;
    53 #ifndef ONLINE_JUDGE
    54   freopen("poj3225.in", "r", stdin);
    55 #endif
    56   while (~scanf("%c %c%d,%d%c\n", &op, &l, &a, &b, &r)){
    57     a <<= 1; b <<= 1; if (l == '(') a++; if (r == ')') b--;
    58     if (a > b) { // there has been a BUG.
    59       if (op == 'I' || op == 'C') cover[1] = xorm[1] = 0;
    60     }
    61     else update(op, a, b, 0, maxn, 1);
    62   }
    63   query(0, maxn, 1); bool mrk = false; int start = -1, end;
    64   for (int i = 0; i <= maxn; ++i){
    65     if (hash[i]){
    66       if (start == -1) start = i;
    67       end = i;
    68     }
    69     else {
    70       if (start != -1){
    71         if (mrk) printf(" ");
    72         mrk = true;
    73         printf("%c%d,%d%c", start&1?'(':'[', start>>1, (end+1)>>1,end&1?')':']');
    74         start = -1;
    75       }
    76     }
    77   }
    78   if (!mrk) printf("empty set");
    79   printf("\n");
    80   return 0;
    81 }

    线段树,成段更新

  • 相关阅读:
    [Tips] uncompyle6进行python的pyc反编译
    [Tips] pip太慢,换源
    [Notes] docker build与docker file
    [Tips] 生成当前python环境的依赖
    [Tips] imagePullPolicy取值
    [Tips] docker镜像和容器导出导入
    js模块化历程
    从一个简单例子来理解js引用类型指针的工作方式
    数组去重的方法总结
    js中const,var,let区别
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/2958322.html
Copyright © 2011-2022 走看看