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 ...
随机推荐
- spring cloud config 详解
Spring Cloud 为开发人员提供了一系列的工具来快速构建分布式系统的通用模型 .例如:配置管理.服务发现.断路由.智能路由.微代理.控制总线.一次性Token.全局锁.决策竞选.分布式sess ...
- 转 Wireshark和TcpDump抓包分析心得
1. Wireshark与tcpdump介绍 Wireshark是一个网络协议检测工具,支持Windows平台和Unix平台,我一般只在Windows平台下使用Wireshark,如果是Linux的话 ...
- Python学习笔记 - day8 - 异常
异常 在程序运行过程中,总会遇到各种各样的错误.有的错误是程序编写有问题造成的,比如本来应该输出整数结果输出了字符串,有的错误是用户输入造成的,比如让用户输入email地址,结果得到一个空字符串,这种 ...
- mysql主从复制、操作语句
授权 grant replication slave on *.* to slave@192.168.10.64 identified by "123456" 登录测试 mysql ...
- 哈希表(一):解决hash冲突的几种方法
(一)线性探测法 线性探测法是最简单的处理冲突的方法. (1)插入元素:插入元素时,如果发生冲突,算法将从该槽位向后遍历哈希表,直到找到表中的下一个空槽,并将该值放入到空槽当中. (2)查找元素:查找 ...
- thread_info&内核栈
转载:http://blog.chinaunix.net/uid-22548820-id-2125152.html 之所以将thread_info结构称之为小型的进程描述符,是因为在这个结构中并没有直 ...
- imx6设备树pinctrl解析【转】
转自:http://blog.csdn.net/michaelcao1980/article/details/50730421 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在移植linu ...
- 【常见的SQL Server连接失败错误以及解决方法】
[常见的SQL Server连接失败错误以及解决方法] http://blog.csdn.net/feixianxxx/article/details/5523922 ADO连接SQL Server ...
- Android 开发之避免被第三方使用代理抓包
现象:charles抓不到包,但wireshark,HttpAnalyzor可以抓到包. 关键代码: URL url = new URL(urlStr); urlConnection = (HttpU ...
- vue 开始开发
1,引入vue.js文件 2,在body里用标签 编辑一个入口 <div id="app">{{msg}}</div> <-- 用双大括号 取数据显示 ...