Help with Intervals+POJ+线段树成段更新+lazy标记的应用

Help with Intervals
Time Limit: 6000MS Memory Limit: 131072K
Total Submissions: 10100 Accepted: 2425
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

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:

CommandSemantics
U TS ← S ∪ T
I TS ← S ∩ T
D TS ← S − T
C TS ← T − S
S 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)
解决方案:区间的交,差(s-t或t-s),并,异或。
这五种操作:
1)并和(s-t)好搞。
2)交,异或,t-s就有点麻烦了。要用到两个维护数组xor,cover。
3)交的时候,可把不在t区间内的线段置0(0表示不取该区间,1表示取);
4)异或的时候,在t区间内把1的变0,0的变1,可实现即在s又在t的区间归0,不在s的在t的变1,这可以用一个异或来标记哪些区间要执行这些操作;
5)t-s首先把不在t里面的全部置0,在t里面的进行异或操作。
code:
#include<cstdio>
#include<iostream>
#include<cstring>
#define MMAX 140000
using namespace std;
int XOR[4*MMAX];///标记否要取反
int cover[4*MMAX];///求覆盖
bool Hash[4*MMAX];///求最终覆盖
void XXOR(int rt)
{
    if(cover[rt]!=-1) cover[rt]^=1;///若cover代表该区间段,果断取反
    else
    {
        XOR[rt]^=1;///若cover不代表该区间段用XOR进行标记
    }
}
void push_down(int rt)
{

    if(cover[rt]!=-1)
    {
        cover[rt*2]=cover[rt*2+1]=cover[rt];
        XOR[rt*2]=XOR[rt*2+1]=0;
        cover[rt]=-1;
    }
    if(XOR[rt])
    {
        XXOR(rt*2);
        XXOR(rt*2+1);
        XOR[rt]=0;
    }///有异或标记时要下传,且对相应区间进行异或。

}///标记下传
void update(int rt,int L,int R,char op,int l,int r)
{
    if(l<=L&&r>=R)
    {
        if(op=='U')
        {
            cover[rt]=1;
            XOR[rt]=0;
        }///若是并,t区间全包括
        else if(op=='D')
        {
            cover[rt]=0;
            XOR[rt]=0;
        }///若是减t,t区间去掉
        else if(op=='C'||op=='S')
        {
            XXOR(rt);
        }///异或,t-s,t内要异或

    }
    else
    {
        int M=(L+R)/2;
        push_down(rt);///标记下移
        if(l<=M)
        {
            update(rt*2,L,M,op,l,r);
        }
        else if(op=='I'||op=='C')
        {
            cover[rt*2]=XOR[rt*2]=0;
        }///当为交,或t-s时,不在t区间内的区间都置0
        if(r>M)
        {
            update(rt*2+1,M+1,R,op,l,r);
        }
        else if(op=='I'||op=='C')
        {
            cover[rt*2+1]=XOR[rt*2+1]=0;
        }///当为交,或t-s时,不在t区间内的区间都置0

    }

}
void query(int rt,int L,int R)
{
    if(cover[rt]==1)
    {
        for(int i=L; i<=R; i++) Hash[i]=true;
    }
    else if(cover[rt]==0) return ;
    else
    {
        push_down(rt);
        int M=(L+R)/2;
        query(rt*2,L,M);
        query(rt*2+1,M+1,R);
    }

}
int main()
{
    int a,b;
    char op,l,r;
    memset(XOR,0,sizeof(XOR));
    memset(cover,-1,sizeof(cover));
    cover[1]=0;
    while(~scanf("%c %c%d,%d%c\n",&op,&l,&a,&b,&r))///记得加个'\n'回车,防止输入回车造成输入混乱
    {
        a*=2;
        b*=2;///将区间扩大两倍,用端点为奇数表示闭区间
        if(l=='(') a++;
        if(r==')') b--;
        if(a>b)
        {
            if(op=='I'||op=='C')
                cover[1]=XOR[1]=0;
        }///若输入时a==b则在交或t-s时区间变空
        else
        {
            update(1,0,MMAX,op,a,b);
        }
    }
    memset(Hash,0,sizeof(Hash));
    query(1,0,MMAX);
    int s=-1,en;
    bool flag=true;
    for(int i=0; i<=MMAX; i++)
    {
        if(Hash[i])
        {
            if(s==-1) s=i;
            en=i;
        }
        else if(s!=-1)
        {
            if(!flag) printf(" ");
            flag=false;
            printf("%c%d,%d%c",s&1?'(':'[',s>>1,(en+1)>>1,en&1?')':']');
            s=-1;
        }
    }
    if(flag) printf("empty set");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值