poj 3225 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 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
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)
Source
题意:给一个全局为0~65536的区间,一开始区间s为空间,然后不断地对区间s进行并上一个区间,交一个区间,减一个区间,用一个区间减去s,还有异或下两个区间。。。
题意:区间操作,交,并,补等
思路:
我们一个一个操作来分析:(用0和1表示是否包含区间,-1表示该区间内既有包含又有不包含)
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互换
分析:这题的各个操作都可以用线段树的成段更新在log(65536*2)的时间内完成
U:并上一个区间,也就是,将这个区间置1就行
I:交上一个区间[l,r],将区间[-∞,l)和(r,∞]置0
D:减去一个区间,将这个区间置0就行
C:用一个区间[l,r]减去s,将区间[-∞,l)和(r,∞]置0,区间[l,r]取反就行
S:求异或,区间[l,r]取反就行
现在上面的所有操作都在理论上解决掉了,而区间的开或闭这个直接把所有区间乘2,对于左边的开区间要加1,右边减1。
记得!空集!还有边界的0!MAXN!
#include<cstdio>
#include<iostream>
#include<cstring>
#define clr(x) memset(x,0,sizeof(x))
const int MAXN=*;
using namespace std;
struct segtree
{
int l,r,rev,val;// val==0表示全部没有覆盖,val==1表示全部被覆盖,其余为-1,rev==1表示取反,rev==0表示不变,只有val==-1时候rev才有作用
}tree[MAXN*+];
int cov[MAXN*+];
void init(int l,int r,int i)//初始化
{
tree[i].l=l;
tree[i].r=r;
tree[i].rev=tree[i].val=;
if(l==r) return ;
int mid=(l+r)>>;
init(l,mid,i<<);
init(mid+,r,(i<<)+);
return ;
}
void reverse(int i)//取反
{
if(tree[i].val!=-) tree[i].val^=;
else
tree[i].rev^=;
}
void downto(int i)//向下更新
{
if(tree[i].val!=-)
{
tree[i<<].val=tree[(i<<)+].val=tree[i].val;
tree[i].val=-;
tree[i<<].rev=tree[(i<<)+].rev=;
}
if(tree[i].rev)
{
reverse(i<<);
reverse((i<<)+);
tree[i].rev=;
}
return ;
}
void update(int i,int l,int r,int op)//操作更新区间
{
if(l<=tree[i].l && r>=tree[i].r)
{
if(op== || op==)
{
tree[i].val=op;
tree[i].rev=;
}
else
reverse(i);
return;
}
downto(i);
int mid=(tree[i].l+tree[i].r)>>;
if(r<=mid)
update(i<<,l,r,op);
else
if(l>mid)
update((i<<)+,l,r,op);
else
{
update((i<<),l,r,op);
update((i<<)+,l,r,op);
}
return ;
}
void query(int i)//标识出哪些区间存在,cov数组用来记录
{
if(tree[i].val==)
{
for(int j=tree[i].l;j<=tree[i].r;j++)
cov[j]=;
return ;
}
if(tree[i].val==)
return;
if(tree[i].l==tree[i].r) return ;
downto(i);
query(i<<);
query((i<<)+);
return ;
}
int main()
{
int lt,rt;
char op,lc,rc;
init(,MAXN,);
while(scanf(" %c %c%d,%d%c",&op,&lc,<,&rt,&rc)!=EOF)
{
lt<<=;
if(lc=='(')
lt++;
rt<<=;
if(rc==')')
rt--;
if(lt>rt)//空集情况!一定要注意
{
if(op=='I' || op=='C') update(,,MAXN,);
}
else
{
if(op=='U')
{
update(,lt,rt,);
}
if(op=='I')
{
if(lt>)update(,,lt-,);//注意边界lt>0 才能-1,rt也是如此。
if(rt<MAXN)update(,rt+,MAXN,);
}
if(op=='D')
{
update(,lt,rt,);
}
if(op=='C')
{
if(lt>)update(,,lt-,);
if(rt<MAXN)update(,rt+,MAXN,);
update(,lt,rt,);
}
if(op=='S')
{
update(,lt,rt,);
}
}
}
clr(cov);
query();
lt=;
rt=-;
for(int i=;i<=MAXN;i++)
{
if(cov[i]== && cov[i+]==)
{
lt=i+;
}
if(cov[i]== && cov[i+]==)
{
rt=i;
if(lt%==)
printf("(%d,",lt/);
else
printf("[%d,",lt/);
if(rt%==)
printf("%d) ",rt/+);
else
printf("%d] ",rt/);
}
}
if(lt>rt)
{
printf("empty set\n");
}
else
printf("\n");
return ;
}
poj 3225 Help with Intervals(线段树,区间更新)的更多相关文章
- POJ 3225 Help with Intervals --线段树区间操作
题意:给你一些区间操作,让你输出最后得出的区间. 解法:区间操作的经典题,借鉴了网上的倍增算法,每次将区间乘以2,然后根据区间开闭情况做微调,这样可以有效处理开闭区间问题. 线段树维护两个值: cov ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- poj 2777 Count Color(线段树 区间更新)
题目:http://poj.org/problem?id=2777 区间更新,比点更新多一点内容, 详见注释, 参考了一下别人的博客.... 参考博客:http://www.2cto.com/kf/ ...
- (中等) POJ 3225 Help with Intervals , 线段树+集合。
Description LogLoader, Inc. is a company specialized in providing products for analyzing logs. While ...
- (中等) POJ 1436 Horizontally Visible Segments , 线段树+区间更新。
Description There is a number of disjoint vertical line segments in the plane. We say that two segme ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
随机推荐
- [IOS]vmxsmc.exe已停止工作 VMware11 Unlocker for Mac OSX无法使用的解决办法.
今天我帮同事安装VMware workstation12后发现之前的unlocker已经无法进行解锁了(就是VMware新建虚拟机无App Mac选项) 使用unlocker会出现vmsxmc.exe ...
- 详解JS中Number()、parseInt()和parseFloat()的区别
三者的作用: Number(): 可以用于任何数据类型转换成数值: parseInt().parseFloat(): 专门用于把字符串转换成数值: 一.Number( ): (1)如果是Boolean ...
- jq_常用开发模块
1.循环模块 var html = ""; $.each(data, function(k, v) { html += '<li>' + '<div>' + ...
- 概率DP入门学习QAQ
emmmm博客很多都烂尾了...但是没空写..先写一下正在学的东西好了 概率DP这东西每次考到都不会..听题解也是一脸懵逼..所以决定学习一下这个东东..毕竟NOIP考过...比什么平衡树实在多了QA ...
- Vue组件-动态组件
动态组件 通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,可以让多个组件使用同一个挂载点,并动态切换: <div id="app6"& ...
- •搭建LAMP环境及快速部署双网站并实现基于域名的虚拟主机
本节所讲内容: 实战:搭建LAMP环境及快速部署双网站并实现基于域名的虚拟主机 LAMP架构:??? Linux+Apache+Mysql+PHP Linux+Apache+Mysql/MariaDB ...
- KVM虚拟机建立快照
部分转载: http://blog.csdn.net/gg296231363/article/details/6899533 windows虚拟机默认镜像格式为raw,快照默认格式为qcow2.win ...
- 浅析linux内核中timer定时器的生成和sofirq软中断调用流程【转】
转自:http://blog.chinaunix.net/uid-20564848-id-73480.html 浅析linux内核中timer定时器的生成和sofirq软中断调用流程 mod_time ...
- GOLANG编译安装
GO这个编译器搞的比较混乱,GO本身是汇编+C开发出来的,后来因为觉得自己牛逼,然后用GO语言又写了一次编译器,所以中途抛弃了C,不过这种做法好与不好很难说,go真的这么有自信用自己语言写自己的编译器 ...
- Educational Codeforces Round 23 补题小结
昨晚听说有教做人场,去补了下玩. 大概我的水平能做个5/6的样子? (不会二进制Trie啊,我真菜) A. 傻逼题.大概可以看成向量加法,判断下就好了. #include<iostream> ...