POJ 3225 Help with Intervals

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

Union AB {x : xA or xB}
Intersection AB {x : xA and xB}
Relative complementation AB {x : xA but x B}
Symmetric difference AB (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 T SST
I T SST
D T SST
C T STS
S T SST

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)

Source


//第一次遇到线段树这么用、偶数点代表点,奇数代表两点之间的区间,所以每个点要乘以2
//还有成段更新中的 up,把子节点信息传个父节点,down,把父节点的信息传递给子节点
//还有根据节点的信息作出相应的操作

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define lson l,m,k<<1
#define rson m+1,r,k<<1|1
#define N 131073
using namespace std;
short cover[N<<2];//代表该段都被覆盖成0或1
short Xor[N<<2],flag;//异或操作,代表从这起于下节点做异或操作
bool hash[N];
void axor(int k)
{
    if(cover[k]!=-1)
       cover[k]^=1;
    else
       Xor[k]^=1;
}
void down(int &k)
{
    if(cover[k]!=-1)
    {
        cover[k<<1]=cover[k<<1|1]=cover[k];
        Xor[k]=0;
        cover[k]=-1;
    }
    else if(Xor[k])
    {
        axor(k<<1);
        axor(k<<1|1);
        Xor[k]=0;
    }
}
void Cover(int L,int R,int l,int r,int k)
{
    if(L<=l&&R>=r)
    {
        cover[k]=flag;
        return ;
    }
    down(k);
    int m=(l+r)>>1;
    if(L<=m) Cover(L,R,lson);
    if(R>m)  Cover(L,R,rson);
}
void XOr(int &L,int &R,int l,int r,int k)
{
    if(L<=l&&R>=r)
    {
        if(cover[k]!=-1)
          cover[k]^=1;
        else
           Xor[k]^=1;
        return ;
    }
    down(k);
    int m=(l+r)>>1;
    if(L<=m) XOr(L,R,lson);
    if(R>m)  XOr(L,R,rson);
}
void query(int l,int r,int k)
{
    if(cover[k]==1)
    {   flag=0;
     for(int i=l;i<=r;i++)
       hash[i]=true;
        return ;
    }
    if(cover[k]==0)
      return ;
    down(k);
    int m=(l+r)>>1;
    query(lson);
    query(rson);
}
int main()
{
    int a,b;
  //  freopen("in.txt","r",stdin);//开始这个没有注释掉、POJ不报编译错误,报WA,唉、郁闷,WA了好多次
    char op,l,r;
    while(scanf("%c %c%d,%d%c",&op,&l,&a,&b,&r)!=EOF)
    {  getchar();
       a=a<<1;b=b<<1;
       if(l=='(') a++;
       if(r==')') b--;
       if(a>b)
       {  if(op=='I'||op=='C')
           cover[1]=Xor[1]=0;
           continue;
       }
       switch(op)
       {
           case 'U':flag=1;Cover(a,b,0,N,1);break;
           case 'I':flag=0;if(a>0) Cover(0,a-1,0,N,1);
                    Cover(b+1,N,0,N,1);break;
           case 'D':flag=0;Cover(a,b,0,N,1);break;
           case 'C':flag=0;
                     if(a>0) Cover(0,a-1,0,N,1);
                     Cover(b+1,N,0,N,1);XOr(a,b,0,N,1); break;
           case 'S':XOr(a,b,0,N,1); break;
       }
    }
        flag=1;
        query(0,N,1);
        int s=-1,e;
        bool f=0;
        if(flag)
           printf("empty set\n");
        else
          {
              for(int i=0;i<=N;i++)
                    if(hash[i])
                    {
                        if(s==-1)
                          s=i;
                    }
                    else
                    {
                        if(s!=-1)
                         {   e=i-1;
                         if(f) printf(" ");
                          f=1;
                            printf("%c%d,%d%c",s&1?'(':'[',s>>1,(e+1)>>1,e&1?')':']');
                             s=-1;
                         }
                    }
            printf("\n");
          }
    return 0;
}

原文地址:https://www.cnblogs.com/372465774y/p/2599199.html