POJ 3225 Help with Intervals【线段树 区间更新 异或运算】
2015-11-15 20:49
453 查看
Help with Intervals
Time Limit: 6000MS | Memory Limit: 131072K | |
Total Submissions: 11487 | Accepted: 2834 | |
Case Time Limit: 2000MS |
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) |
S, which starts out empty and is modified as specified by the following commands:
Command | Semantics |
---|---|
UT | S ← S ∪ T |
IT | S ← S ∩ T |
DT | S ← S − T |
CT | S ← T − S |
ST | S ← S ⊕ T |
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
XT
where
Xis 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; }
相关文章推荐
- 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest G】【脑洞 拓扑排序 大根堆小根堆极限维护】Graph DAG加k边使得最小拓扑序最大
- 第八周实践项目1—建立顺序串的算法库
- JavaScript学习手记3事件详解
- GDAL\OGR C#中文路径不支持的问题解决方法
- QTP初级篇
- 十天冲刺---总结
- 刷新电脑的DNS缓存
- 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest E】【模拟 贪心】Easy Arithmetic 需要补 加减法表达式添加加减号使得数
- Java学习笔记 第一章 入门<转>
- 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D】【脑洞 构造】Distribution in Metagonia 把数以2^p3^
- nodejs 利用express框架 创建一个简单的web项目
- hihocoder #1249 枚举
- MD5编码工具类 MD5Code.java
- RabbitMQ
- python异常处理
- 11.函数和函数式编程
- hihoCoder 1257 Snake Carpet (构造题+详解) 2015北京区域赛
- 关于Wowza Streaming Engine 的Transcoder插件的功能简介
- 文件上传与下载
- Visual Studio 2015配置opencv开发环境