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 ⊕ TInput
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 likeX Twhere 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.
按下标建立线段树,对于每种操作转换成取1、取0和取反。
对于每个节点维护当前标记和当前状态【全0、全1、有0有1】。
将开区间看成0.5,再将所有数乘2作为下标。
U是直接标记l..r为1,
I是将-oo..l-1和r+1..oo标记为0,
D是爸爸l..r标记为0,
C是把-oo..l-1和r+1..oo标记为1,l..r取反。
S直接把l..r取反。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int rmx=131100;
//131100
struct inter
{
int l,r;
bool operator < (const inter & iii) const
{
return l<iii.l;
}
}i1,i2,ans[600000];
int lson[600000],rson[600000],con[600000],tot=1,mk[600000],cnt;
void down(int p)
{
if (mk[p])
{
if (mk[p]<3)
{
con[p]=mk[lson[p]]=mk[rson[p]]=mk[p];
mk[p]=0;
}
else
{
if (con[p]<3) con[p]=3-con[p];
if (mk[lson[p]])
{
if (mk[lson[p]]<3) mk[lson[p]]=3-mk[lson[p]];
else mk[lson[p]]=0;
}
else mk[lson[p]]=3;
if (mk[rson[p]])
{
if (mk[rson[p]]<3) mk[rson[p]]=3-mk[rson[p]];
else mk[rson[p]]=0;
}
else mk[rson[p]]=3;
mk[p]=0;
}
}
}
void up(int p)
{
down(p);
if (lson[p]==0&&rson[p]==0) return;
down(lson[p]);
down(rson[p]);
if (con[lson[p]]!=con[rson[p]]||con[lson[p]]==3)
con[p]=3;
else con[p]=con[lson[p]];
}
void build(int p,int ll,int rr)
{
con[p]=2;
if (ll==rr) return;
lson[p]=++tot;
int mid=(ll+rr)/2;
build(tot,ll,mid);
rson[p]=++tot;
build(tot,mid+1,rr);
}
void mark(int p,int ll,int rr,int l,int r,int b)
{
if (l>r) return;
down(p);
if (ll==l&&rr==r)
{
mk[p]=b;
return;
}
int mid=(ll+rr)/2,x,y,z;
if (r<=mid) mark(lson[p],ll,mid,l,r,b);
else
{
if (l>mid) mark(rson[p],mid+1,rr,l,r,b);
else
{
mark(lson[p],ll,mid,l,mid,b);
mark(rson[p],mid+1,rr,mid+1,r,b);
}
}
up(p);
}
void oppo(int p,int ll,int rr,int l,int r)
{
if (l>r) return;
down(p);
if (ll==l&rr==r)
{
mk[p]=3;
return;
}
int mid=(ll+rr)/2,x,y,z;
if (r<=mid) oppo(lson[p],ll,mid,l,r);
else
{
if (l>mid) oppo(rson[p],mid+1,rr,l,r);
else
{
oppo(lson[p],ll,mid,l,mid);
oppo(rson[p],mid+1,rr,mid+1,r);
}
}
up(p);
}
void find(int p,int ll,int rr)
{
down(p);
up(p);
if (con[p]<3)
{
if (con[p]==1) ans[++cnt]=(inter){ll,rr};
return;
}
int mid=(ll+rr)/2;
find(lson[p],ll,mid);
find(rson[p],mid+1,rr);
}
void out(int l,int r)
{
if (l&1) printf("(");
else printf("[");
printf("%d,%d",l/2,(r+1)/2);
if (r&1) printf(")");
else printf("]");
printf(" ");
}
int main()
{
int i,j,k,m,n,p,q,x,y,z,l,r,l_r,r_l;
char s[3],c;
build(1,0,rmx);
while (scanf("%s",s+1)==1)
{
getchar();
c=getchar();
scanf("%d",&x);
if (c=='(') l=2*x+1;
else l=2*x;
getchar();
scanf("%d",&x);
c=getchar();
if (c==')') r=2*x-1;
else r=2*x;
if (s[1]=='U') mark(1,0,rmx,l,r,1);
if (s[1]=='I')
{
mark(1,0,rmx,0,l-1,2);
mark(1,0,rmx,r+1,rmx,2);
}
if (s[1]=='D') mark(1,0,rmx,l,r,2);
if (s[1]=='C')
{
mark(1,0,rmx,0,l-1,2);
mark(1,0,rmx,r+1,rmx,2);
oppo(1,0,rmx,l,r);
}
if (s[1]=='S') oppo(1,0,rmx,l,r);
}
find(1,0,rmx);
if (cnt==0)
{
printf("empty set\n");
return 0;
}
sort(ans+1,ans+cnt+1);
l=r=-1;
for (i=1;i<=cnt;i++)
{
if (l==-1)
{
l=ans[i].l;
r=ans[i].r;
continue;
}
if (r+1==ans[i].l) r=ans[i].r;
else
{
out(l,r);
l=ans[i].l;
r=ans[i].r;
}
}
out(l,r);
}

本文介绍了一种使用线段树实现的时间区间操作解析方法,通过离散数学中的集合运算概念,如并集、交集等,来处理编程语言中的时间区间命令。文章详细描述了如何通过特定的数据结构和算法优化来解决实际问题。

1336

被折叠的 条评论
为什么被折叠?



