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 ...
随机推荐
- Codeforces Round #483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]
题目链接:http://codeforces.com/contest/984 A. Game time limit per test:2 seconds memory limit per test:5 ...
- 6、MySQL索引种类
1.普通索引 这是最基本的索引,它没有任何限制,比如上文中为title字段创建的索引就是一个普通索引,MyIASM中默认的BTREE类型的索引,也是我们大多数情况下用到的索引. –直接创建索引 CRE ...
- 【转】ps命令详解
原文地址:http://apps.hi.baidu.com/share/detail/32573968 有 时候系统管理员可能只关心现在系统中运行着哪些程序,而不想知道有哪些进程在运行.由于一个应用程 ...
- Linux线程基础函数
1. 线程标识: (1) 比较两个线程ID: #include <pthread.h> int pthread_equal(pthread_t tid1, pthread_t tid2); ...
- 实验室项目.md
1 嵌入式操作系统 为什么要用嵌入式操作系统 普通的单片机编程:程序(软件)--单片机硬件: 嵌入式操作系统开发:程序(软件)--操作系统--嵌入式硬件(包括单片机等); 我们平时普通所学的单片机编程 ...
- SVN--版本控制系统
引言 SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subvers ...
- 微信小程序获取输入框(input)内容
微信小程序---获取输入框(input)内容 wxml <input placeholder="请输入手机号码" maxlength="11" type= ...
- Linux平台用C++实现事件对象,同步线程(转)
本文属于转载,原文链接如下:http://blog.csdn.net/chexlong/article/details/7080537 与其相关的一组API包括:pthread_mutex_init, ...
- 高性能网络服务器--I/O复用 select poll epoll_wait之间的区别
一.select select采用的是集合的方式,最多只能访问1024个套接字.可读,可写,异常,三种访问,并且采用的是轮训的方式进行每次访问都需要从内核向用户空间拷贝 二.poll poll采用的是 ...
- 【转载】Python: Enum枚举的实现
转自:http://www.cnblogs.com/codingmylife/archive/2013/05/31/3110656.html 从C系语言过来用Python,好不容易适应了写代码不打 ...