POJ 3225 Help with Intervals【线段树 区间更新 异或运算】

本文介绍了一种基于离散数学中集合操作原理的区间处理方法,包括并集、交集、相对补集等操作,并通过一种小型编程语言实现这些操作,特别讨论了如何利用区间端点的奇偶性来区分开闭区间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Help with Intervals

Time Limit: 6000MS Memory Limit: 131072K
Total Submissions: 11487 Accepted: 2834
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

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 set S, which starts out empty and is modified as specified by the following commands:

CommandSemantics
U TSST
I TSST
D TSST
C TSTS
S 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’ and T 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. 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)


恩,题目大意就是说,集合的交,并,相减,对称差。开始时为空集,集合的开闭区间处理是将区间扩大一倍,若为闭区间偶数表示,开区间奇数表示。


//u [l,r] ->1 并
//i [-oo,l) (r,+oo) ->0 交
//d [l,r] ->0 减
//c (-oo,l) (r,+oo) ->0 [l,r] 0/1 被减
//s [l,r] 0/1 对称差
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 65555<<1
using namespace std;
bool mark[maxn<<4];
struct lnode
{
    int l,r,v;
    bool f;
};
lnode node[maxn<<4];
void build(int o,int l,int r)
{
    node[o].l=l;
    node[o].r=r;
    node[o].v=0;
    node[o].f=false;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    build(o<<1,l,mid);
    build(o<<1|1,mid+1,r);
}
void pushdown(int o)
{
    if(node[o].v!=-1)
    {
        node[o<<1].v=node[o<<1|1].v=node[o].v;
        node[o<<1].f=node[o<<1|1].f=false;
        node[o].v=-1;
    }
    if(node[o].f)
    {
        if(node[o<<1].v!=-1)
            node[o<<1].v^=1;
        else
            node[o<<1].f^=1;
        if(node[o<<1|1].v!=-1)
            node[o<<1|1].v^=1;
        else
            node[o<<1|1].f^=1;
        node[o].f=false;
    }
}
void update(int o,int l,int r,char c)
{
    if(node[o].l>=l&&node[o].r<=r)
    {
        if(c=='U')
        {
            node[o].v=1;
            node[o].f=false;
        }
        else if(c=='D')
        {
            node[o].v=0;
            node[o].f=false;
        }
        else if(c=='C'||c=='S')
        {
            if(node[o].v!=-1)
                node[o].v^=1;
            else
                node[o].f^=1;
        }
        return ;
    }
    pushdown(o);
    int mid=(node[o].l+node[o].r)>>1;
    if(l<=mid)
        update(o<<1,l,r,c);
    else if(c=='I'||c=='C')
        node[o<<1].v=node[o<<1].f=0;
    if(r>mid)
        update(o<<1|1,l,r,c);
    else if(c=='I'||c=='C')
        node[o<<1|1].f=node[o<<1|1].v=0;
}
void query(int o)
{
    if(node[o].l==node[o].r)
    {
        if(node[o].v==1)
            mark[node[o].l]=true;
        return ;
    }
    pushdown(o);
    query(o<<1);
    query(o<<1|1);
}
int is(char c)
{
    if(c>='0'&&c<='9')
        return 1;
    return 0;
}
int main()
{
    int a,b;
    char s[100];
    build(1,0,maxn);
    memset(mark,false,sizeof(mark));
    while(gets(s)&&strcmp(s,"EOF")!=0)
    {
        int i,len=strlen(s);
        a=0,b=0;
        i=3;
        while(i<len&&is(s[i]))
            a=a*10+s[i++]-'0';
        i++;
        while(i<len&&is(s[i]))
            b=b*10+s[i++]-'0';
        if(s[i]==']')
            b=b*2;
        else
            b=b*2-1;
        if(s[2]=='[')
            a=a*2;
        else
            a=a*2+1;
        if(a>b)
            continue;
        update(1,a,b,s[0]);
    }
    bool f=true;
    query(1);
    for(int i=0;i<=maxn;++i)
    {
        if(!mark[i])
            continue;
        a=i;
        while(mark[i]&&i<=maxn)
            i++;
        b=--i;
        if(f)
            f=false;
        else
            printf(" ");
        if(a&1)
            printf("(%d",a/2);
        else
            printf("[%d",a/2);
        printf(",");
        if(b&1)
            printf("%d)",(b+1)/2);
        else
            printf("%d]",b/2);
    }
    if(f)
        printf("empty set");
    printf("\n");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值