Help with Intervals POJ - 3225(线段树区间异或)

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 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] (a, b ∈ 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)

题意:
5种指令
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
输出最后的集和
思路:
还是比较麻烦的。
几个需要考虑的点
开闭区间:这个通过每个坐标占两个格子解决
注意起点从0开始,血泪教训。。。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxx = 65535 * 2 + 2;
const int maxn = 65535 * 2 + 1000;

struct Node
{
    int l,r;
    int cover,Xor;
}t[maxn << 2];
int vis[maxn];

void XOR(int i)
{
    if(t[i].cover != -1)
    {
        t[i].cover ^= 1;
        t[i].Xor = 0;
    }
    else
    {
        t[i].Xor ^= 1;
    }
}

void pushup(int i)
{
    if(t[i].cover == -1 || t[i * 2 + 1].cover == -1)
        t[i].cover = -1;
    else if(t[i * 2].cover == t[i * 2 + 1].cover)
        t[i].cover = t[i * 2].cover;
    else t[i].cover = -1;
}

void pushdown(int i)
{
    if(t[i].cover != -1)
    {
        t[i * 2].cover = t[i * 2 + 1].cover = t[i].cover;
        t[i * 2].Xor = t[i * 2 + 1].Xor = 0;
    }
    else if(t[i].Xor == 1)
    {
        t[i].Xor = 0;
        XOR(i * 2);
        XOR(i * 2 + 1);
    }
}

void build(int i,int l,int r)
{
    t[i].l = l;t[i].r = r;
    if(l == r)
    {
        t[i].cover = t[i].Xor = 0;
        return;
    }
    int m = (l + r) >> 1;
    build(i * 2,l,m);
    build(i * 2 + 1,m + 1,r);
}

void update1(int i,int x,int y,int v)
{
    if(x > y)return ;
    if(x <= t[i].l && t[i].r <= y)
    {
        t[i].cover = v;
        t[i].Xor = 0;
        return;
    }
    pushdown(i);
    int m = (t[i].l + t[i].r) >> 1;
    if(x <= m)update1(i * 2,x,y,v);
    if(y > m)update1(i * 2 + 1,x,y,v);
    pushup(i);
}

void update2(int i,int x,int y)
{
    if(x > y)return ;
    if(x <= t[i].l && t[i].r <= y)
    {
        XOR(i);
        return;
    }
    pushdown(i);
    int m = (t[i].l + t[i].r) >> 1;
    if(x <= m)update2(i * 2,x,y);
    if(y > m)update2(i * 2 + 1,x,y);
    pushup(i);
}



void query(int i)
{
    if(t[i].cover == 1)
    {
        for(int j = t[i].l;j <= t[i].r;j++)
        {
            vis[j] = 1;
        }
        return;
    }
    else if(t[i].cover == 0)return;
    if(t[i].l == t[i].r) return;
    pushdown(i);
    query(i * 2);
    query(i * 2 + 1);
}


void solve()
{
    memset(vis,0,sizeof(vis));
    bool flag = true;
    int s = -1,e = 0;
    query(1);
    for(int i = 0;i <= maxx;i++)
    {
        if(vis[i])
        {
            if(s == -1)s = i;
            e = i;
        }
        else
        {
            if(s != -1)
            {
                if(!flag)printf(" ");
                flag = false;
                
                if(s % 2)printf("(");
                else printf("[");
                
                printf("%d,%d",s / 2,(e + 1) / 2);
                
                if(e % 2)printf(")");
                else printf("]");
                
                s = -1;
            }
        }
    }
    if(flag)printf("empty set");
    printf("\n");
}
int main()
{
    char op,L,R;
    int l,r;
    build(1,0,maxx);
    while(~scanf("%c %c%d,%d%c\n",&op,&L,&l,&r,&R))
    {
        l *= 2;
        r *= 2;
        
        if(L == '(')l++;
        if(R == ')')r--;
        
        if(op == 'U')
        {
            update1(1,l,r,1);
        }
        else if(op == 'I')
        {
            update1(1,0,l - 1,0);
            update1(1,r + 1,maxx,0);
        }
        else if(op == 'D')
        {
            update1(1,l,r,0);
        }
        else if(op == 'C')
        {
            update1(1,0,l - 1,0);
            update1(1,r + 1,maxx,0);
            update2(1,l,r);
        }
        else if(op == 'S')
        {
            update2(1,l,r);
        }
    }
    solve();
    return 0;
}

发布了628 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104073713