您的位置:首页 > 其它

POJ 3225(线段树,区间的交并补操作)

2017-08-17 11:39 429 查看

problem

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:

OperationNotationDefinition
UnionA ∪ B{x : x ∈ A or x ∈ B}
IntersectionA ∩ B{x : x ∈ A and x ∈ B}
Relative complementationA − B{x : x ∈ A but B}
Symmetric differenceA ⊕ 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:

CommandSemantics
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)

思路

1.怎么处理这些操作?

如果直接在线性空间上处理,那样时间复杂度高

想到线段树这种树形的结构来,其经常用来处理区间问题

但怎么处理区间之间的操作呢?

用01来表示有 无 操作即为(不理解的可以在数轴上画一下):

U:把区间[l,r]覆盖成1

I:把[-∞,l)(r,∞]覆盖成0

D:把区间[l,r]覆盖成0

C:把[-∞,l)(r,∞]覆盖成0 , 且[l,r]区间0/1互换

S:[l,r]区间0/1互换

这里用到三个数组:

bool hash[maxn];
int cov[maxn<<2];
int x[maxn<<2];


hash用来统计结点的有无,只有01

cov用来做覆盖标记

x用来做异或标记

核心代码

void funxor(int rt){//当调用这个函数时,表示对rt这个线段树结点作整体操作
if(cov[rt]!=-1) cov[rt]^=1;//有覆盖直接对覆盖标记进行异或
else x[rt]^=1;
}


void PushDown(int rt){//标记下方
if(cov[rt]!=-1){//有覆盖标记
cov[rt<<1]=cov[rt<<1|1]=cov[rt];//下方
x[rt<<1]=x[rt<<1|1]=0;//放下覆盖标记时,下面异或标记清空(还管啥异或,直接覆盖了)
cov[rt]=-1;
}
if(x[rt]){//有异或标记
funxor(rt<<1);//调用上面的函数
funxor(rt<<1|1);
x[rt]=0;
}
}


2.怎么处理开区间和闭区间?

我们会发现,开闭区间处理不了啊!!

如果单独记录,每个点还要标记?

有一种技巧来处理开闭区间 即加倍

左右端点乘2,如果是开区间 左端点+1 右端点-1。 这样就用奇偶性把开闭区间区分开了

而且,只要除以2(右区间+1再除)就又变回原区间了,开还是闭就取决于奇偶

这样的技巧实际上就是由开闭区间的性质及int在做除法向下取整所决定的,在题目中经常用到

代码示例

#include<cstdio>
#include<iostream>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;

const int maxn=140000;
bool hash[maxn]; int cov[maxn<<2]; int x[maxn<<2];

void funxor(int rt){
if(cov[rt]!=-1) cov[rt]^=1;//有覆盖直接对覆盖异或就好
else x[rt]^=1;
}

void PushDown(int rt){
if(cov[rt]!=-1){
cov[rt<<1]=cov[rt<<1|1]=cov[rt];
x[rt<<1]=x[rt<<1|1]=0;//放下覆盖标记时,异或标记清空
cov[rt]=-1;
}
if(x[rt]){
funxor(rt<<1);
funxor(rt<<1|1);
x[rt]=0;
}
}

void update(char op,int L,int R,int l,int r,int rt){
if(L<=l&&r<=R){
if(op=='U'){
cov[rt]=1;
x[rt]=0;
}
else if(op=='D'){
cov[rt]=0;
x[rt]=0;
}
else if(op=='C'||op=='S'){
funxor(rt);
}
return ;
}
PushDown(rt);
int m=(l+r)>>1;
if(L<=m) update(op,L,R,lson);
else if(op=='I'||op=='C'){
x[rt<<1]=cov[rt<<1]=0;
}
if(m<R) update(op,L,R,rson);
else if(op=='I'||op=='C'){
x[rt<<1|1]=cov[rt<<1|1]=0;
}
}

void query(int l,int r,int rt){
if(cov[rt]==1){
for(int i=l;i<=r;++i){
hash[i]=true;
}
return ;
}
else if(cov[rt]==0) return ;
if(l==r) return ;
PushDown(rt);
int m=(l+r)>>1;
query(lson);
query(rson);
}

int main()
{
cov[1]=x[1]=0;
char op,l,r;
int a,b;
while(~scanf("%c %c%d,%d%c\n",&op,&l,&a,&b,&r))
{
a=a<<1;
b=b<<1;
if(l=='(') a++;
if(r==')') b--;
if(a>b){//特判
if(op=='C'||op=='I'){
cov[1]=x[1]=0;
}
}
else update(op,a,b,0,maxn,1);
}
query(0,maxn,1);
bool flag=false;
int s=-1,e;
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");
puts("");
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: