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. 【BZOJ】1601: [Usaco2008 Oct]灌水

    [算法]最小生成树 [题解] 想到网络流,但是好像不能处理流量和费用的关系. 想到最短路,但好像不能处理重复选边的问题. 每条边只需要选一次,每个点就要遍历到,可以想到最小生成树. 建超级源向每个点连 ...

  2. 爬虫--Urllib库详解

    1.什么是Urllib? 2.相比Python2的变化 3.用法讲解 (1)urlopen urlllb.request.urlopen(url,data=None[timeout,],cahle=N ...

  3. 20151024_001_C#基础知识(静态与非静态的区别,值类型和引用类型,堆和栈的区别,字符串的不可变性,命名空间)

    1:我们把这些具有相同属性和相同方法的对象进行进一步的封装,抽象出来类这个概念. 类就是个模子,确定了对象应该具有的属性和方法. 对象是根据类创建出来的. 2:类:语法 [public] class ...

  4. .net XmlHelper xml帮助类

    using System.Data; using System.IO; using System.Xml; using System.Xml.Serialization; /// <summar ...

  5. 【转】debian下的update-rc.d的使用

    在Linux系统下,一个Services的启动.停止以及重启通常是通过/etc/init.d目录下的脚本来控制的.然而,在启动或改变运行级别时, 是在/etc/rcX.d中来搜索脚本.其中X是运行级别 ...

  6. C++学习之路(五):复制构造函数与赋值运算符重载

    之前没有细想过两者的区别,今天对此进行简要记录,后续完善补充. 复制构造函数是在类对象被创建时调用的,但是赋值运算符是被已经存在的对象调用完成赋值操作. 复制构造函数只在对象实例化时才被调用,即在复制 ...

  7. Phoenix批量修改数据

    很简单的一个东西,查了挺久的,浪费了很多的时间 直接用Upsert Into Select就可以了 例:把tables表中cloumn2列等于bbb的都改成aaa Upsert Into Table  ...

  8. Cent os FTP配置

    原文:http://www.aicoffees.com/itshare/412261137.html

  9. 我的新博客地址http://xxxbw.github.io/

    最近在学github,在github搭了个博客,以后也会使用另外一个博客.有兴趣的小伙伴可以看看~ 地址:http://xxxbw.github.io/

  10. c语言中数组,指针数组,数组指针,二维数组指针

    1.数组和指针 ] = {,,,,};// 定义数组 // 1. 指针和数组的关系 int * pa = array; pa = array; // p[0] == *(p+0) == array[0 ...