博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3225 Help with Intervals(线段树+区间的交集,差集,补集,并集)好难的题目,一天了,真是纠结
阅读量:4035 次
发布时间:2019-05-24

本文共 5422 字,大约阅读时间需要 18 分钟。

好题,真难,都看一天了,总算是AC了,真够纠结的啊,参考博客

1、

2、题目大意:

给一个空的区间,经过给定的五种操作,输出最终的结果,五种操作分别是

Command Semantics
U T SST
I T SST
D T SST
C T STS
S T SST

思路:

我们一个一个操作来分析:(用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互换

成段覆盖的操作很简单,比较特殊的就是区间0/1互换这个操作,我们可以称之为异或操作

很明显我们可以知道这个性质:当一个区间被覆盖后,不管之前有没有异或标记都没有意义了
所以当一个节点得到覆盖标记时把异或标记清空
而当一个节点得到异或标记的时候,先判断覆盖标记,如果是0或1,直接改变一下覆盖标记,不然的话改变异或标记

开区间闭区间只要数字乘以2就可以处理(偶数表示端点,奇数表示两端点间的区间)

线段树功能:update:成段替换,区间异或 query:简单hash

3、题目:

Help with Intervals
Time Limit: 6000MS   Memory Limit: 131072K
Total Submissions: 9343   Accepted: 2216
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 AB {
x : xA or xB}
Intersection AB {
x : xA and xB}
Relative complementation AB {
x : xA but x B}
Symmetric difference AB (AB) ∪ (BA)

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 SST
I T SST
D T SST
C T STS
S T SST

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, bZ, 0 ≤ ab ≤ 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

, frkstyc

 

4、AC代码:

#include
#include
#include
using namespace std;#define N 131075#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int cover[N*4],XOR[N*4];int vis[N];void fxor(int rt){ if(cover[rt]!=-1) cover[rt]^=1; else XOR[rt]^=1;}void pushDown(int rt){ if(cover[rt]!=-1) { cover[rt<<1]=cover[rt<<1|1]=cover[rt]; XOR[rt<<1]=XOR[rt<<1|1]=0; cover[rt]=-1; } if(XOR[rt]) { fxor(rt<<1); fxor(rt<<1|1); XOR[rt]=0; }}void update(char op,int L,int R,int l,int r,int rt){ if(L<=l && R>=r) { if(op=='U') { cover[rt]=1; XOR[rt]=0; } else if(op=='D') { cover[rt]=0; XOR[rt]=0; } if(op=='C' || op=='S') { fxor(rt); } return ; } pushDown(rt); int m=(l+r)>>1; if(L<=m) update(op,L,R,lson); else if(op=='I' || op=='C') { cover[rt<<1]=0; XOR[rt<<1]=0; } if(R>m) update(op,L,R,rson); else if(op=='I' || op=='C') { cover[rt<<1|1]=0; XOR[rt<<1|1]=0; }}void query(int l,int r,int rt){ if(cover[rt]==1) { for(int i=l; i<=r; i++) vis[i]=1; return ; } else if(cover[rt]==0) return ; pushDown(rt); int m=(l+r)>>1; query(lson); query(rson);}int main(){ char op,c1,c2,c; int a,b; while(scanf("%c %c%d,%d%c",&op,&c1,&a,&b,&c2)!=EOF) { getchar(); a=a<<1; b=b<<1; if(c1=='(') a++; if(c2==')') b--; if(a<=b) update(op,a,b,0,N,1); else { if(op=='I' || op=='C') cover[1]=XOR[1]=0; } } query(0,N,1); int s=-1,e,flag=0; for(int i=0; i<=N; i++) { if(vis[i]==1) { if(s==-1) s=i; e=i; } else if(s!=-1) { if(flag==0) { if(s%2==0) printf("[%d,",s>>1); else printf("(%d,",s>>1); if(e%2==0) printf("%d]",(e+1)>>1); else printf("%d)",(e+1)>>1); flag=1; } else { printf(" "); if(s%2==0) printf("[%d,",s>>1); else printf("(%d,",s>>1); if(e%2==0) printf("%d]",(e+1)>>1); else printf("%d)",(e+1)>>1); } s=-1; } } if(flag==1) printf("\n"); else printf("empty set\n"); return 0;}

 

转载地址:http://nwcdi.baihongyu.com/

你可能感兴趣的文章
慢慢欣赏linux 内核模块引用
查看>>
kprobe学习
查看>>
慢慢欣赏linux phy驱动初始化2
查看>>
慢慢欣赏linux CPU占用率学习
查看>>
2020年终总结
查看>>
linux内核学习(4)建立正式内核的页式内存映射, 以x86 32位模式为例
查看>>
Homebrew指令集
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>