poj 3225 Help with Intervals(线段树 区间的并 交 减 替换 等)

来源:互联网 发布:广州数控车床编程软件 编辑:程序博客网 时间:2024/04/30 15:00
Help with Intervals
Time Limit: 6000MS Memory Limit: 131072KTotal Submissions: 5802 Accepted: 1285Case 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] (ab ∈ 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)

Source

PKU Local 2007 (POJ Monthly--2007.04.28), frkstyc

题目:http://poj.org/problem?id=3225

题意:给一个全局为0~65536的区间,一开始区间s为空间,然后不断地对区间s进行并上一个区间,交一个区间,减一个区间,用一个区间减去s,还有异或下两个区间。。。

分析:这题的各个操作都可以用线段树的成段更新在log(65536*2)的时间内完成

           U:并上一个区间,也就是,将这个区间置1就行

           I:交上一个区间[l,r],将区间[-∞,l)和(r,∞]置0

          D:减去一个区间,将这个区间置0就行

          C:用一个区间[l,r]减去s,将区间[-∞,l)和(r,∞]置0,区间[l,r]取反就行

          S:求异或,区间[l,r]取反就行

现在上面的所有操作都在理论上解决掉了,而区间的开或闭这个直接把所有区间乘2,对于左边的开区间要加1,右边减1,这个挺神奇的,看别人的= =

线段树的取反,和置一个值,要先置值再取反,之后记得把两标记都清0,对于取反一个区间,如果这个区间没有任何操作,那么说明它下面有取反或置值,只能改变一下它的取反标志,而不能把这个区间变成1,一开始这样错掉了。。。

代码:

#include<cstdio>#include<iostream>#include<cstring>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;const int mm=133333;int col[mm<<2],turn[mm<<2],cov[mm+2];void T(int rt){    if(col[rt])col[rt]^=3;    else turn[rt]^=1;}void pushdown(int rt){    if(col[rt])    {        col[rt<<1]=col[rt<<1|1]=col[rt];        col[rt]=turn[rt<<1]=turn[rt<<1|1]=0;    }    if(turn[rt])    {        T(rt<<1);        T(rt<<1|1);        turn[rt]=0;    }}void updata(int L,int R,int op,int l,int r,int rt){    if(L<=l&&R>=r)    {        if(op)col[rt]=op,turn[rt]=0;        else T(rt);        return;    }    pushdown(rt);    int m=(l+r)>>1;    if(L<=m)updata(L,R,op,lson);    if(R>m)updata(L,R,op,rson);}void query(int l,int r,int rt){    if(col[rt])    {        if(col[rt]<2)            for(int i=l;i<=r;++i)cov[i]=1;        return;    }    if(l==r)return;    pushdown(rt);    int m=(l+r)>>1;    query(lson);    query(rson);}int main(){    freopen("a.in","r",stdin);    char op,l,r;    int i,a,b,flag;    updata(0,mm,2,0,mm,1);    while(~scanf("%c %c%d,%d%c\n",&op,&l,&a,&b,&r))    {        a=(a<<1)+(l=='(');        b=(b<<1)-(r==')');        if(a>b)        {            if(op=='I'||op=='C')updata(0,mm,2,0,mm,1);        }        else        {            if(op=='U')updata(a,b,1,0,mm,1);            if(op=='I'||op=='C')            {                if(a>0)updata(0,a-1,2,0,mm,1);                if(b<mm)updata(b+1,mm,2,0,mm,1);            }            if(op=='D')updata(a,b,2,0,mm,1);            if(op=='C'||op=='S')updata(a,b,0,0,mm,1);        }    }    memset(cov,0,sizeof(cov));    query(0,mm,1);    for(a=b=-1,flag=i=0;i<=mm;++i)        if(cov[i])b=i,a=a<0?i:a;        else if(a>=0)        {            if(flag)printf(" ");            flag=1;            printf("%c%d,%d%c",a&1?'(':'[',a>>1,(b+1)>>1,b&1?')':']');            a=b=-1;        }    if(!flag)printf("empty set");    puts("");    return 0;}


原创粉丝点击