(线段树)poj3225-Help with Intervals

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 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 ⊕ 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)
  1 #include <iostream>
  2 //#include<bits/stdc++.h>
  3 #include <stack>
  4 #include <queue>
  5 #include <cstdio>
  6 #include <cstring>
  7 #include <algorithm>
  8 using namespace std;
  9 typedef long long ll;
 10 typedef unsigned long long ull;
 11 const int MAX=65535;
 12 struct node
 13 {
 14     int st,rev;
 15 }a[500*MAX];
 16 bool vi[3*MAX];
 17
 18 void xxor(int k)
 19 {
 20     if(a[k].st!=-1)//如果区间状态一致,直接改变区间的情况
 21         a[k].st^=1;
 22     else
 23         a[k].rev^=1;//不然标记区间0,1互换1次
 24 }
 25 void pushdown(int k)
 26 {
 27     if(a[k].st!=-1)//如果区间状态一致
 28     {
 29         a[2*k].st=a[2*k+1].st=a[k].st;//将父结点的状态传递到子节点
 30         a[2*k].rev=a[2*k+1].rev=0;
 31         a[k].st=-1;//并且将父结点状态改回
 32     }
 33     if(a[k].rev)//如果需要0,1互换
 34     {
 35         xxor(2*k);
 36         xxor(2*k+1);
 37         a[k].rev=0;
 38     }
 39 }
 40 void update(char operation,int l,int r,int L,int R,int k)//[l,r]是目标区间,[L,R]是此时区间
 41 {
 42     if(L>=l&&R<=r)//如果当前区间已经包含在了目标区间之中
 43     {
 44         if(operation==‘U‘)//如果取并集
 45         {
 46             a[k].st=1;//T区间所有元素值都赋1
 47             a[k].rev=0;//0,1互换情况初始化为0
 48         }
 49         else if(operation==‘D‘)//如果取差集,差掉T
 50         {
 51             a[k].st=0;//T区间所有元素都赋0
 52             a[k].rev=0;//0,1互换情况初始化为0
 53         }
 54         else if(operation==‘C‘||operation==‘S‘)//如果是原集合被差掉或异或集
 55         {
 56             xxor(k);
 57         }
 58         return;
 59     }
 60 //    if(L+1==R)
 61 //        return;
 62     pushdown(k);
 63     if(l<=(L+R)/2)//如果当前区间左半部分存在有部分在目标区间内的可能
 64     {
 65         update(operation,l,r,L,(L+R)/2,2*k);
 66     }
 67     else//若不然,当前区间左半部分已经在目标区间之外
 68     {
 69         if(operation==‘I‘||operation==‘C‘)
 70         {
 71             a[2*k].st=0;a[2*k].rev=0;
 72         }
 73     }
 74     if(r>(L+R)/2)//如果当前区间的右半部分存在有部分在目标区间内的可能
 75     {
 76         update(operation,l,r,(L+R)/2+1,R,2*k+1);
 77     }
 78     else
 79     {
 80         if(operation==‘I‘||operation==‘C‘)
 81         {
 82             a[2*k+1].st=a[2*k+1].rev=0;
 83         }
 84     }
 85 }
 86 void query(int l,int r,int k)
 87 {
 88     if(a[k].st==1)//如果这个区间完整的都是状态1
 89     {
 90         for(int i=l;i<=r;i++)
 91             vi[i]=true;
 92         return;
 93     }
 94     else if(a[k].st==0)//若此区间状态是已经完整的标明为状态0的,就不用再往下看了
 95         return;
 96     if(l==r)
 97         return;
 98     pushdown(k);
 99     query(l,(l+r)/2,2*k);
100     query((l+r)/2+1,r,2*k+1);
101 }
102 int main()
103 {
104 //    int n;
105 //    cin>>n;
106 //    scanf("%d\n",&n);
107     char op,ls,rs,tem;//tem接收逗号
108     int l,r;
109 //    memset(vi,false,sizeof(vi));
110     a[1].st=a[1].rev=0;
111 //    while(--n)
112 //    for(int q=1;q<=n;q++)
113
114 //        scanf("%c %c%d,%d%c\n",&op,&ls,&l,&r,&rs);
115         while(~scanf("%c %c%d,%d%c\n",&op,&ls,&l,&r,&rs))
116 //        cin>>op>>ls>>l>>tem>>r>>rs;
117       {
118 //          scanf("%c %c%d,%d%c\n",&op,&ls,&l,&r,&rs);
119         l*=2;
120         r*=2;
121         if(ls==‘(‘)
122             l++;
123         if(rs==‘)‘)
124             r--;
125         update(op,l,r,0,2*MAX,1);
126 //        printf("%d\n",n);
127     }
128 //    printf("~~!\n");
129     query(0,2*MAX,1);
130 //    printf("!!\n");
131     int st=-1,en;//输出时的区间
132     bool flag=false;
133     for(int i=0;i<=2*MAX;i++)
134     {
135         if(vi[i])
136         {
137             if(st==-1)
138                 st=i;
139             en=i;
140         }
141         else
142         {
143             if(st!=-1)
144             {
145                 if(flag)
146                     printf(" ");
147 //                if(st%2==0)
148 //                    printf("[");
149 //                else
150 //                    printf("(");
151                 printf("%c%d,%d%c",st&1?‘(‘:‘[‘,st/2,(en+1)/2,en&1?‘)‘:‘]‘);//因为区间右侧做的是--,故需要+1再除二
152 //                if(en%2==0)
153 //                    printf("]");
154 //                else
155 //                    printf(")");
156                 flag=true;
157                 st=-1;
158             }
159
160         }
161     }
162     if(!flag)
163         printf("empty set");
164     return 0;
165 }
时间: 2024-12-11 06:01:35

(线段树)poj3225-Help with Intervals的相关文章

【线段树】POJ3225-Help with Intervals

---恢复内容开始--- [题目大意] (直接引用ACM神犇概括,貌似是notonlysucess?) 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互换 [思路] 由于涉及到开区间和闭区间,我们如此规定数组下表: 下标      0      1      2      3      4      5…… 含义1   (1    1    (2  

poj 3225 Help with Intervals(线段树)

题目链接:poj 3225 Help with Intervals 题目大意:模拟集合操作,输出最终的集合. 解题思路:线段树. U l r:[l,r]区间置为1 I l r:[0,l),(r,maxn]置为0 D l r:[l,r]区间置为0 C l r:[0,l),(r,maxn]置为0,[l,r]区间取逆 S l r:[l,r]区间取逆. 然后基本水水的线段树,注意一下区间开和闭. #include <cstdio> #include <cstring> #include &

poj 3225 Help with Intervals(线段树,区间更新)

Help with Intervals Time Limit: 6000MS   Memory Limit: 131072K Total Submissions: 12474   Accepted: 3140 Case Time Limit: 2000MS Description LogLoader, Inc. is a company specialized in providing products for analyzing logs. While Ikki is working on g

Poj3225Help with Intervals区间线段树

这题不说了,都是泪.这题也是拆点. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set&g

Help with Intervals(集合的交并补,线段树)

很早以前做过这题,早就没印象了,估计当时也是照着某大神的代码抄过的,现在是连题意都看了好长时间. 刚开始的S集合是空集,给你一些操作和一个T集合,把操作的结果再赋给S集合. 解法:因为会有开区间和闭区间,对于一个值我拆成了两个点 比如 1,2,3, 表示的区间为[1,2] 把两个端点值分别设为2个点,把端点之间的区间也设为一个点,那么 区间就可以表示为(1,2) = 1,[1,2) = 1,2  (1,2] = 2,3   ,把这些点建成树,然后进行区间的操作,也就转换成了对点的操作. 并操作,

pojHelp with Intervals线段树解法

题:点击打开链接 分析:稍加分析一下交并关系,很好理解.要求掌握线段树区间更新.注意几点:由于是连续的集合,而线段树是节点,所以要将集合扩大两倍以便用点表示.注意输入[0,x)(x是任意大于0的数)即a(左边)为0,并且包含,当处理0到a-1时a-1为-1,会报RE. 此处用到延迟标记col,col=0时将标记的区间更新为0:col为1时将区间更新为1:col为2时将区间翻转.其中col为2时翻转操作1-col表示:当col为0时会翻转为1:当col为1时会翻转为0:当col为-1(不用作处理)

线段树 区间更新

poj3468 A Simple Problem with Integers ( m - ( m >> 1 ) )这里跪了几发.. - 的优先级大于 >> 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdio> 5 #include<string> 6 #include<queue> 7 #include

数据结构---线段树

线段树 转载请注明出处,谢谢!http://blog.csdn.net/metalseed/article/details/8039326  持续更新中···   一:线段树基本概念 1:概述 线段树,类似区间树,是一个完全二叉树,它在各个节点保存一条线段(数组中的一段子数组),主要用于高效解决连续区间的动态查询问题,由于二叉结构的特性,它基本能保持每个操作的复杂度为O(lgN)! 性质:父亲的区间是[a,b],(c=(a+b)/2)左儿子的区间是[a,c],右儿子的区间是[c+1,b],线段树

(转载)线段树模板(来自胡浩大牛)

http://www.notonlysuccess.com/(今天看二叉树,想回来看看,发现大牛博客进不去...) 如果要学,就要好好学.我copy的,如有错,请看http://www.cnblogs.com/Mu-Tou/archive/2011/08/11/2134427.html [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉得当时的代码风格实在是太丑了,很多线段树的初学者