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:
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
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]
(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)
恩,题目大意就是说,集合的交,并,相减,对称差。开始时为空集,集合的开闭区间处理是将区间扩大一倍,若为闭区间偶数表示,开区间奇数表示。
//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;
}