• 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;
    }
  • 相关阅读:
    juqery 点击分页显示,指定一页显示多少个,首次加载显示多少个
    PHP指定时间戳/日期加一天,一年,一周,一月
    POJ 2955 Brackets 区间合并
    zoj 3537 Cake 区间DP (好题)
    DP——最优三角形剖分
    LightOJ 1422 Halloween Costumes
    POJ 1738 石子合并2 GarsiaWachs算法
    NIOP1995 石子合并(区间DP)
    POJ 2429
    pollard_rho和Miller_Rabin
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2717037.html
Copyright © 2020-2023  润新知