【线段树】POJ3225 Help with intervals

【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:

OperationNotation

Definition

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] (ab ∈ 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)

【Analysis】

  需要两个标记的线段树,mk[]记标记0/1,Xor[]记是否取反,重点在于pushdown()中对于两个标记的处理顺序,先mk[],后Xor[];

  U:把给出区间[l,r]均标为1

  I:把[-∞,l)∪(r,+∞]标为0

  D:把[l,r]标为0

  C:把[-∞,l)∪(r,+∞]标为0,区间[l,r]取反

  S:区间[l,r]取反  

  理解‘[’‘(’在整颗线段树中的表示方式,相当于把区间倍长,左区间以偶数表'[',奇数表'(',右区间以偶数表')',奇数表']'。

【Code】

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int n = 65535*2+2;
 6 
 7 char ind,u,v;
 8 int l,r;
 9 int Xo[(n<<2)+10],mk[(n<<2)+10],mark[(n<<1)+10];
10 bool flag;
11 
12 void Xorup(int now) {
13     if(mk[now]!=-1) mk[now]^=1;
14     else Xo[now]^=1;
15 }
16 
17 void pushdown(int now) {
18     if(mk[now]!=-1) {
19         mk[now<<1]=mk[now<<1|1]=mk[now];
20         Xo[now<<1]=Xo[now<<1|1]=0;
21         mk[now]=-1;
22     }    
23     if(Xo[now]) {
24         Xorup(now<<1);
25         Xorup(now<<1|1);
26         Xo[now]=0;
27     }
28 }
29 
30 void query(char a,int L,int R,int now,int l,int r) {
31     if(L<=l&&r<=R) {
32         if(a == 'U') mk[now]=1,Xo[now]=0;
33         if(a == 'D') mk[now]=Xo[now]=0;
34         if(a=='C'||a=='S') Xorup(now);
35         return ;
36     }
37     pushdown(now);
38     int m=(l+r)>>1;
39     if(L<=m) query(a,L,R,now<<1,l,m);
40     else if(a=='I'||a=='C') mk[now<<1]=Xo[now<<1]=0;
41     if(m+1<=R) query(a,L,R,now<<1|1,m+1,r);
42     else if(a=='I'||a=='C') mk[now<<1|1]=Xo[now<<1|1]=0;
43 }
44 
45 void traverse(int now,int l,int r)
46 {
47     if(mk[now]==1)
48     {   
49         for(int i=l;i<=r;++i)mark[i]=1;
50         return;
51     }
52     if(mk[now]==0)return;
53     if(l==r)return;
54     pushdown(now);
55     int m=(l+r)>>1;
56     traverse(now<<1,l,m);
57     traverse(now<<1|1,m+1,r);
58 }
59 
60 int main() {
61 
62     //freopen("p.in","r",stdin);
63     while(scanf("%c %c%d,%d%c\n",&ind,&u,&l,&r,&v)!=EOF){
64         l<<=1;r<<=1;
65         if(u=='(')++l;
66         if(v==')')--r;
67         if(l>r) {
68             if(ind=='I'||ind=='C')mk[1]=Xo[1]=0;
69         }
70         else query(ind,l,r,1,0,n);//
71     }
72     traverse(1,0,n);flag=0;
73     for(int i=0,st=-1,t=-1;i<n;++i){
74         if(mark[i]) st = ((st==-1)?i:st), t=i;//
75         else 
76         if(st!=-1) {
77             if(flag)printf(" ");else flag=1;
78             printf("%c%d,%d%c",st&1 ? '(' : '[',st>>1,(t+1)>>1,t&1 ? ')' : ']');
79             st=-1;
80         }
81     }
82     if(!flag)printf("empty set");
83     printf("\n");
84     return 0;
85 }



转载于:https://www.cnblogs.com/Etta/p/6753529.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值