码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3225——Help with Intervals(线段树,成段替换+区间异或+hash)

时间:2014-09-09 18:21:09      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   os   io   ar   strong   for   art   

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

Operation Notation

Definition

Union A ∪ B {x : x ∈ A or x ∈ B}
Intersection A ∩ B {x : x ∈ A and x ∈ B}
Relative complementation A ? B {x : x ∈ A but x ? B}
Symmetric difference A ⊕ 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:

Command Semantics
U T S ← S ∪ T
I T S ← S ∩ T
D T S ← S ? T
C T S ← T ? S
S T S ← 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)


——————————————————————分割线————————————————————

题目大意:

就是实现区间的交,并,补,异或操作


思路:

对于区间的开,闭,将区间乘2,开的话,左端点+1,右端点-1.奇数表示开,偶数表示闭

例如(3,6)变成[7,11],左右端点为奇数,所以左右为开,退回来的时候判断7为奇数,11也为奇数,就是( 7/2, (11+1)/2 )

覆盖标记0、1,-1  :0表示不包含,1表示包含,-1表示既有包含也有不包含

异或标记0、1:0表示没有标记,1表示有标记


则:

U L,R 将区间L,R覆盖为1

D L,R 将区间L,R覆盖为0

I L,R 将区间<L和>R覆盖为0

C L,R 将区间<L和>R覆盖为0,对区间L,R 0,1进行对换(异或)

S L,R 将区间L,R  0,1进行对换(异或)


对于覆盖标记,如果存在异或标记,则将异或标记清0

对于异或标记,优先考虑是否存在覆盖标记,如果存在,则对覆盖标记异或,否则对异或标记异或


PS:还要熟练掌握push_down的内涵才行~~~

#include <iostream>
#include <cstring>
#include <cstdio>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=131071;
using namespace std;
bool hash[maxn+5];
int cover[maxn<<2],Xor[maxn<<2];
void fxor(int rt)
{
    if(cover[rt]!=-1) cover[rt]^=1;
    else Xor[rt]^=1;
}
void push_down(int rt)
{
    if(cover[rt]!=-1){
        cover[rt<<1]=cover[rt<<1|1]=cover[rt];
        cover[rt]=-1;
        Xor[rt]=0;
    }
    if(Xor[rt]){
        fxor(rt<<1);
        fxor(rt<<1|1);
        Xor[rt]=0;
    }
}
void update(char op,int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R){
        if(op=='U'){
            cover[rt]=1;
            Xor[rt]=0;
        }
        if(op=='D'){
            Xor[rt]=cover[rt]=0;
        }
        if(op=='C'||op=='S'){
            fxor(rt);
        }
        return ;
    }
    push_down(rt);
    int m=(l+r)>>1;
    if(L<=m) update(op,L,R,lson);
    else if(op=='I'||op=='C'){
        Xor[rt<<1]=cover[rt<<1]=0;
    }
    if(m<R) update(op,L,R,rson);
    else if(op=='I'||op=='C'){
        Xor[rt<<1|1]=cover[rt<<1|1]=0;
    }

}
void query(int l,int r,int rt)
{
    if(cover[rt]==1){
        for(int it=l;it<=r;++it){
            hash[it]=true;
        }
        return;
    }else if(cover[rt]==0) return;
    push_down(rt);
    int m=(l+r)>>1;
    query(lson);
    query(rson);
}
int main()
{
    char op,l,r;int a,b;
    while(scanf("%c %c%d,%d%c\n",&op,&l,&a,&b,&r)!=EOF){
        a<<=1,b<<=1;
        if(l=='(') a++;
        if(r==')') b--;
        if(a>b){
            if(op=='I'||op=='C'){
                Xor[1]=cover[1]=0;
            }
        }else{
            update(op,a,b,0,maxn,1);
        }
    }
    query(0,maxn,1);
    int s=-1,e;
    bool flag=false;
    for(int i=0;i<=maxn;++i){
        if(hash[i]){
            if(s==-1) s=i;
            e=i;
        }else{
            if(s!=-1){
                if(flag) printf(" ");
                flag=true;
                printf("%c%d,%d%c",s&1?'(':'[',s>>1,(e+1)>>1,e&1?')':']');
                s=-1;
            }
        }
    }
    if(!flag) printf("empty set\n");
    printf("\n");
    return 0;
}



POJ 3225——Help with Intervals(线段树,成段替换+区间异或+hash)

标签:des   style   color   os   io   ar   strong   for   art   

原文地址:http://blog.csdn.net/u014141559/article/details/39158671

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!