线段树交并补+poj3225

来源:互联网 发布:centos 7 删除文件夹 编辑:程序博客网 时间:2024/04/25 21:06
Language:
Help with Intervals
Time Limit: 6000MS Memory Limit: 131072KTotal Submissions: 9602 Accepted: 2282Case 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] (a, b  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)思路:
我们一个一个操作来分析:(用0和1表示是否包含区间,-1表示该区间内既有包含又有不包含)
U:把区间[l,r]覆盖成1
I:把[-∞,l)(r,∞]覆盖成0
D:把区间[l,r]覆盖成0
C:把[-∞,l)(r,∞]覆盖成0 , 且[l,r]区间0/1互换
S:[l,r]区间0/1互换

成段覆盖的操作很简单,比较特殊的就是区间0/1互换这个操作,我们可以称之为异或操作很明显我们可以知道这个性质:当一个区间被覆盖后,不管之前有没有异或标记都没有意义了所以当一个节点得到覆盖标记时把异或标记清空而当一个节点得到异或标记的时候,先判断覆盖标记,如果是0或1,直接改变一下覆盖标记,不然的话改变异或标记

开区间闭区间只要数字乘以2就可以处理(偶数表示端点,奇数表示两端点间的区间)

一是要注意各种操作对cover和xor数组的影响,再就是开区间和闭区间如何表示。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=65536*2;char op,l,r;int x,y;bool hash[maxn+1];struct IntevalTree{    int cover[maxn<<3];    int XOR[maxn<<3];    void maintain(int o)    {        if(cover[o]!=-1)cover[o]^=1;        else XOR[o]^=1;    }    void pushdown(int o)    {        if(cover[o]!=-1)        {            cover[o*2]=cover[o*2+1]=cover[o];            XOR[o*2]=XOR[o*2+1]=0;            cover[o]=-1;        }        if(XOR[o])        {            maintain(o*2);            maintain(o*2+1);            XOR[o]=0;        }    }    void update(int o,int l,int r,int q1,int q2,char op)    {        if(q1<=l&&r<=q2)        {            if(op=='U'){cover[o]=1;XOR[o]=0;}            else if(op=='D'){cover[o]=0;XOR[o]=0;}            else if(op=='C'||op=='S')maintain(o);            return;        }        pushdown(o);        int mid=(l+r)/2;        if(q1<=mid)update(o*2,l,mid,q1,q2,op);        else if(op=='I'||op=='C')cover[o*2]=XOR[o*2]=0;        if(q2>mid)update(o*2+1,mid+1,r,q1,q2,op);        else if(op=='I'||op=='C')cover[o*2+1]=XOR[o*2+1]=0;    }    void query(int o,int l,int r)    {        if(cover[o]==1)        {            for(int i=l;i<=r;i++)hash[i]=true;            return;        }        else if(cover[o]==0)return;        pushdown(o);        int mid=(l+r)/2;        query(o*2,l,mid);        query(o*2+1,mid+1,r);    }}tree;int main(){    memset(tree.cover,-1,sizeof(tree.cover));    memset(tree.XOR,0,sizeof(tree.XOR));    tree.cover[1]=tree.XOR[1]=0;    while(scanf("%c %c%d,%d%c\n",&op,&l,&x,&y,&r)!=EOF)    {        x<<=1,y<<=1;        if(l=='(')x++;        if(r==')')y--;        if(x>y&&(op=='C'||op=='I'))tree.cover[1]=tree.XOR[1]=0;        else tree.update(1,0,maxn,x,y,op);    }    memset(hash,0,sizeof(hash));    tree.query(1,0,maxn);    int s=-1,e=-1;    bool first=true;    for(int i=0;i<=maxn;i++)    {        if(hash[i])        {            if(s==-1)s=i;            e=i;        }        else if(s!=-1)        {            if(first)first=false;            else printf(" ");            printf("%c%d,%d%c",s&1?'(':'[' , s>>1 , (e+1)>>1 , e&1?')':']');            s=-1;        }    }    if(first)printf("empty set");    printf("\n");    return 0;}


0 0
原创粉丝点击