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 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)

Source


很久没做线段树了,拿来练手一脸懵逼。
这题坑点太多了,空集情况要考虑(这个坑了好久),边界要考虑(你以为是真无穷吗那不是药丸)
题解摘自kungbin博客:http://www.cnblogs.com/kuangbin/archive/2013/04/10/3012986.html
 

题意:给一个全局为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,&lt,&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(线段树,区间更新)的更多相关文章

  1. POJ 3225 Help with Intervals --线段树区间操作

    题意:给你一些区间操作,让你输出最后得出的区间. 解法:区间操作的经典题,借鉴了网上的倍增算法,每次将区间乘以2,然后根据区间开闭情况做微调,这样可以有效处理开闭区间问题. 线段树维护两个值: cov ...

  2. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  3. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  4. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

  5. poj 2777 Count Color(线段树 区间更新)

    题目:http://poj.org/problem?id=2777 区间更新,比点更新多一点内容, 详见注释,  参考了一下别人的博客.... 参考博客:http://www.2cto.com/kf/ ...

  6. (中等) POJ 3225 Help with Intervals , 线段树+集合。

    Description LogLoader, Inc. is a company specialized in providing products for analyzing logs. While ...

  7. (中等) POJ 1436 Horizontally Visible Segments , 线段树+区间更新。

    Description There is a number of disjoint vertical line segments in the plane. We say that two segme ...

  8. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  9. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. HDU - 5327 Olympiad(一维前缀和)

    Olympiad Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem ...

  2. dot.js使用心得

    一.dot.js介绍 最近用到的数据模板引擎有很多,今天讲的doT.js也是其中一种. doT.js的特点是体积小,速度快,并且不依赖其他插件. 官网下载:http://olado.github.io ...

  3. 解决不走onActivityResult方法

    最近在开发公司项目,在使用startActivityForResult关联俩个Activity中,发现A跳转到B,B设置setResult之后,A没有执行onActivityResult,查找一下,发 ...

  4. 【uva10779】收集者的难题

    按照题意建模就行了. #include<bits/stdc++.h> #define naive 0 #define inf 1000000007 using namespace std; ...

  5. BZOJ 2460: [BeiJing2011]元素 贪心,线性基

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2460 解法:从大到小排序,依次贪心的添加到当前集合就可以了,需要动态维护线性基.用拟阵证明 ...

  6. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

  7. C基础 内存越界和内存监测的简单处理

    引言 突然感觉要出去走走了, 醒了后 刷完牙就在联系coding, 不知不觉到了 黄昏. 看看天, 打开灯. 又感觉到了 夜夜夜夜 . 13年到北京务工, 遇到一批批NB的同龄人物. 一块工作, 一块 ...

  8. 深入浅出Node.js(一) - 初识Node.js

    1.Node.js将Javascript解决不确定性所使用的事件驱动方式引入了进来,因为JS是一门事件驱动的语言,旨在能够对外界的事件作出响应; 2.Node.js中,所有的有关异步的操作,都在同步操 ...

  9. C++11——Use auto keyword in C++11

    版权声明:本文系原创,转载请注明来源. Compile your program with: g++ -std=c++ -o target_name filen_ame.cpp or: g++ -st ...

  10. powershell常用操作

    创建文件 New-Item -path $file_path -itemtype file 创建目录 New-Item -path $dir_path -type directory 删除目录 Rem ...