poj3225 Help with Intervals 线段树 区间交并补

来源:互联网 发布:电脑远程桌面软件 编辑:程序博客网 时间:2024/05/03 11:30

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

UnionAB{x : xA or xB}IntersectionAB{x : xA and xB}Relative complementationAB{x : xA but x B}Symmetric differenceAB(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 setS, which starts out empty and is modified as specified by the following commands:

CommandSemanticsU TSSTI TSSTD TSSTC TSTSS TSST

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’ andT 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. IfS 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)

  给你一些区间操作,求操作完后的区间。

  设cover[o]为区间覆盖情况,1是区间都被覆盖,0是区间都不被覆盖,-1是不确定(以前做的是1被覆盖,其他情况都是-1,突然发现像这样设三种状态更好)。设XOR[o]代表区间翻转情况,翻转是把区间覆盖的变成不覆盖,不覆盖的变成覆盖,这种情况在T-S的时候会用到。并且cover值不为-1的时候XOR[o]的值为0,如果要翻转的话就直接改变cover值,不需要标记XOR。还要注意交和T-S这种操作要把T区间外的区间都取消覆盖。

  关于开闭区间怎么办呢,只要把本来的区间都乘以2,左开的话加1,右开的话减1,这样就可以表示出所有情况了,最后输出的时候也要注意判断区间开闭问题。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<cstdlib>#include<cmath>#define INF 0x3f3f3f3f#define MAXN 132000#define MAXM 20010#define MAXNODE 4*MAXN#define MOD 1000000007#define eps 1e-9using namespace std;int cover[MAXN<<2],XOR[MAXN<<2],vis[MAXN];void pushdown(int o){    if(cover[o]!=-1){        cover[o<<1]=cover[o<<1|1]=cover[o];        XOR[o<<1]=XOR[o<<1|1]=0;        cover[o]=-1;    }    if(XOR[o]){        if(cover[o<<1]!=-1) cover[o<<1]^=1;        else XOR[o<<1]^=1;        if(cover[o<<1|1]!=-1) cover[o<<1|1]^=1;        else XOR[o<<1|1]^=1;        XOR[o]=0;    }}void update(int o,int L,int R,int ql,int qr,char op){    if(ql<=L&&qr>=R){        if(op=='U'){            cover[o]=1;            XOR[o]=0;        }        if(op=='D') cover[o]=XOR[o]=0;        if(op=='C'||op=='S'){            if(cover[o]!=-1) cover[o]^=1;            else XOR[o]^=1;        }        return;    }    pushdown(o);    int mid=(L+R)>>1;    if(ql<=mid) update(o<<1,L,mid,ql,qr,op);    else if(op=='I'||op=='C') cover[o<<1]=XOR[o<<1]=0;  //区间外的取消覆盖    if(qr>mid) update(o<<1|1,mid+1,R,ql,qr,op);    else if(op=='I'||op=='C') cover[o<<1|1]=XOR[o<<1|1]=0;  //区间外的取消覆盖}void query(int o,int L,int R){    if(cover[o]!=-1){        if(cover[o]==1){            for(int i=L;i<=R;i++) vis[i]=1;        }        return;    }    pushdown(o);    int mid=(L+R)>>1;    query(o<<1,L,mid);    query(o<<1|1,mid+1,R);}int main(){    freopen("in.txt","r",stdin);    cover[1]=XOR[1]=0;    char op,lb,rb;    int ql,qr;    while(scanf("%c %c%d,%d%c\n",&op,&lb,&ql,&qr,&rb)!=EOF){        ql<<=1;        qr<<=1;        if(lb=='(') ql++;        if(rb==')') qr--;        if(ql>qr){            if(op=='I'||op=='C'){                cover[1]=XOR[1]=0;  //T是空集的时候这两个操作把S变空集            }        }        else update(1,0,MAXN,ql,qr,op);    }    memset(vis,0,sizeof(vis));    query(1,0,MAXN);    int flag=0;    for(int i=0;i<MAXN;i++){        if(vis[i]){            if(flag) printf(" ");            flag=1;            int j;            for(j=i;j<MAXN&&vis[j];j++);            printf("%c%d,%d%c",i&1?'(':'[',i>>1,j>>1,(j-1)&1?')':']');            i=j;        }    }    if(!flag) printf("empty set");    puts("");    return 0;}



0 0
原创粉丝点击