POJ 3225 Help with Intervals 线段树区间操作

/*Accepted    2364K    969MS    C++    2769B    2012-08-22 16:54:26*/

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<algorithm>

using namespace std;

#define lson l, mid, rt << 1

#define rson mid + 1, r, rt << 1 | 1



const int MAXN = 131072;

bool hash[MAXN + 1];

int col[MAXN << 2], XOR[MAXN << 2];

void FXOR(int rt)

{

    if(col[rt] != -1) col[rt] ^= 1;

    else XOR[rt] ^= 1;

}



void PushDown(int rt)

{

    if(col[rt] != -1)

    {

        col[rt << 1] = col[rt << 1 | 1] = col[rt];

        XOR[rt << 1] = XOR[rt << 1 | 1] = 0;

        col[rt] = -1;

    }

    if(XOR[rt])

    {

        FXOR(rt << 1);

        FXOR(rt << 1 | 1);

        XOR[rt] = 0;

    }

}



void update(char op, int L, int R, int l, int r, int rt)

{

    int mid = l + r >> 1;

    if(L <= l && r <= R)

    {

        if(op == 'U')

        {

            col[rt] = 1;

            XOR[rt] = 0;

        }

        else if(op == 'D')

        {

            col[rt] = 0;

            XOR[rt] = 0;

        }

        else if(op == 'C' || op == 'S')

        {

            FXOR(rt);

        }

        return;

    }

    PushDown(rt);

    if(L <= mid) update(op, L, R, lson);

    else if(op == 'I' || op == 'C')

    {

        XOR[rt << 1] = col[rt << 1] = 0;

    }

    if(R > mid) update(op, L, R, rson);

    else if(op == 'I' || op == 'C')

    {

        XOR[rt << 1 | 1] = col[rt << 1 | 1] = 0;

    }

}



void query(int l, int r, int rt)

{

    int i, mid = l + r >> 1;

    if(col[rt] == 1)

    {

        for(i = l; i <= r; i ++)

        {

            hash[i] = true;

        }

        return;

    }

    else if(col[rt] == 0) return;

    if(l == r) return;

    PushDown(rt);

    query(lson);

    query(rson);

}



int main()

{

    col[1] = XOR[1] = 0;

    char op, left, right, str[25];

    int a, b;

    while(gets(str)!= NULL)

    {

        sscanf(str, "%c %c%d,%d%c", &op, &left, &a, &b, &right);

        a <<= 1, b <<= 1;

        if(left == '(') a ++;

        if(right == ')') b --;

        if(a > b)

        {

            if(op == 'C' || op == 'I')

            {

                col[1] = XOR[1] = 0;

            }

        }

        else update(op, a, b, 0, MAXN, 1);

    }

    query(0, MAXN, 1);

    bool flag = false;

    int s = -1, e, i;

    for(i = 0; i <= MAXN; i ++)

    {

        if(hash[i]) {

            if(s == -1) s = i;

            e = i;

        }

        else

        {

            if(s != -1)

            {

                if(flag) printf(" ");

                flag = true;

                printf("%c%d,%d%c", s & 1 ? '(' : '[', s >> 1, e + 1 >> 1,

                                    e & 1 ? ')' : ']');

                s = -1;

            }

        }

    }

    if(!flag) printf("empty set");

    puts("");

    return 0;

}

你可能感兴趣的:(with)