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

    Help with Intervals
    Time Limit: 6000MS   Memory Limit: 131072K
    Total Submissions: 7152   Accepted: 1637
    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 AB {x : xA or xB}
    Intersection AB {x : xA and xB}
    Relative complementation AB {x : xA but
    x B}
    Symmetric difference AB (AB) ∪ (BA)

    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 SST
    I T SST
    D T SST
    C T STS
    S T SST

    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, bZ, 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 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)

    Source

     
    分析:成段替换或成段异或,注意异或的懒惰标记要尽量尽早用本段具体值相结合。查询所有满足条件的区间。
    #include<cstdio>
    #include<cstring>
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define maxn 1<<17
    int col[1 << 18], sum[1 << 18];
    
    void funxor(int rt) {
        if (sum[rt] != -1)
            sum[rt] = !sum[rt];
        else
            col[rt] = !col[rt];
    }
    
    void pushdown(int rt) {
        if (sum[rt] != -1) {
            sum[rt << 1] = sum[rt << 1 | 1] = sum[rt];
            col[rt<<1]=col[rt<<1|1]=0;
            sum[rt] = -1;
        }
        if (col[rt]) {
            funxor(rt << 1);
            funxor(rt << 1 | 1);
            col[rt] = 0;
        }
    }
    
    void update(char op, int L, int R, int l, int r, int rt) {
        if (L <= l && R >= r) {
            if (op == 'U') {
                sum[rt] = 1;
                col[rt] = 0;
            } else if (op == 'D') {
                sum[rt] = 0;
                col[rt] = 0;
            } else if (op == 'S' || op == 'C') {
                funxor(rt);
            }
            return;
        }
        pushdown(rt);
        int m = (l + r) >> 1;
        if (L <= m) {
            update(op, L, R, lson);
        } else if (op == 'I' || op == 'C') {
            sum[rt << 1] = 0;
            col[rt << 1] = 0;
        }
        if (R > m) {
            update(op, L, R, rson);
        } else if (op == 'I' || op == 'C') {
            sum[rt << 1 | 1] = 0;
            col[rt << 1] = 0;
        }
    }
    int start, end, first;
    
    void query(int l, int r, int rt) {
        if (sum[rt] != -1) {
            if (sum[rt] == 0)
                return;
            if (l == end + 1) {
                end = r;
            } else {
                if (end == -100) {
                    start = l;
                    end = r;
                    return;
                }
                if (first)
                    first = 0;
                else
                    printf(" ");
                if (start & 1)
                    printf("(%d,", start >> 1);
                else
                    printf("[%d,", start >> 1);
                if (end & 1)
                    printf("%d)", (end >> 1) + 1);
                else
                    printf("%d]", end >> 1);
                start = l;
                end = r;
            }
            return;
        }
        pushdown(rt);
        int m = (l + r) >> 1;
        query(lson);
        query(rson);
    }
    
    int main() {
        int a, b;
        char op, left, right;
        col[1] = sum[1] = 0;
        while (scanf("%c %c%d,%d%c", &op, &left, &a, &b, &right) != EOF && op != '#') {
            scanf("%*c");
            a = a << 1;
            b = b << 1;
            if (left == '(')
                a++;
            if (right == ')')
                b--;
            if (a > b) {
                if (op == 'I' || op == 'C')
                    col[1] = sum[1] = 0;
            } else
                update(op, a, b, 0, maxn, 1);
    
        }
        end = -100;
        first = 1;
        query(0, maxn, 1);
        if (end == -100) {
            printf("empty set\n");
            return 0;
        }
        if (first)
            first = 0;
        else
            printf(" ");
        if (start & 1)
            printf("(%d,", start >> 1);
        else
            printf("[%d,", start >> 1);
        if (end & 1)
            printf("%d)", (end >> 1) + 1);
        else
            printf("%d]", end >> 1);
        printf("\n");
        return 0;
    }
  • 相关阅读:
    Office2003中文绿色免安装版本(wordExcelPowerPoint三合一)
    图片去背景神奇网站remove.bg
    网络视频下载分享
    Windows10手机投屏到电脑
    测试开发工程必备技能之一:Mock的使用
    实战教程:如何将自己的Python包发布到PyPI上
    Sysbench测试神器:一条命令生成百万级测试数据
    Java 获取PDF中的数字签名信息
    Java 在Excel中添加分离型饼图、环形图
    C#/VB.NET 添加、删除PPT幻灯片中的数字签名
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2717037.html
Copyright © 2011-2022 走看看