对应POJ题目:点击打开链接
Time Limit: 6000MS | Memory Limit: 131072KB | 64bit IO Format: %I64d & %I64u |
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 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和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
倍化区间处理开闭区间的问题。因为普通的线段树实际处理的并非真正的区间,而是一系列点,相当于处理一个向量。这个问题需要处理的是真正的区间,所以应该有一个主导思想就是,把区间点化!对于待处理区间[a,b](暂时不考虑开闭),对其边界均乘2。若区间左开则对左界值+1,若区间右开,则对右界-1!
如:[2,3]会倍增为[4,6],[2,3)会倍增为[4,5],(2,3]会倍增为[5,6],(2,3)将倍增为[5,5],我们这时可以看到,对于普通线段树无法处理的线段如(x,x+1)将被点化为[2*x+1,2*x+1]!这个问题得到比较完美的解决
然后基本就是这样了,C语言的scanf()输入比C++的cin快很多。。。
#include<cstdio> #include<cstdlib> #include<cmath> #include<map> #include<queue> #include<stack> #include<vector> #include<algorithm> #include<cstring> #include<string> #include<iostream> #define ms(x,y) memset(x,y,sizeof(x)) #define N 65536<<1 const int MAXN=1000+10; const int INF=1<<30; using namespace std; int cover[N<<2]; bool vis[N+10]; bool Xor[N<<2]; void down(int rt) { if(cover[rt]!=-1){//为覆盖标记 Xor[rt<<1]=0;//Xor标记失去作用,清除它 Xor[rt<<1|1]=0;//Xor标记失去作用,清除它 cover[rt<<1]=cover[rt]; cover[rt<<1|1]=cover[rt]; cover[rt]=-1; } else if(Xor[rt]){//为Xor标记 if(cover[rt<<1]!=-1) cover[rt<<1]^=1; else Xor[rt<<1]^=1; if(cover[rt<<1|1]!=-1) cover[rt<<1|1]^=1; else Xor[rt<<1|1]^=1; Xor[rt]=0; } } void updata(int rt, int left, int right, int l, int r, int mark) { if(l>r || l<0) return; if(left == l && right == r){ #if 1 if(mark==1) {cover[rt]=1; Xor[rt]=0;}//覆盖为1 else if(mark==0) {cover[rt]=0; Xor[rt]=0;}//覆盖为0 #endif else{//Xor if(cover[rt]!=-1) cover[rt]^=1;//该区间全为1或全全为0 else Xor[rt]^=1;//改变Xor标记 } return; } down(rt); int mid=(left + right)>>1; if(mid >= r) updata(rt<<1, left, mid, l, r, mark); else if(mid < l) updata(rt<<1|1, mid+1, right, l, r, mark); else{ updata(rt<<1, left, mid, l, mid, mark); updata(rt<<1|1, mid+1, right, mid+1, r, mark); } } void query(int rt, int left, int right) { if(cover[rt]==1){ for(int i=left; i<=right; i++) vis[i]=1; return; } else if(cover[rt]==0) return; down(rt); if(left==right) return; int mid=(left + right)>>1; query(rt<<1, left, mid); query(rt<<1|1, mid+1, right); } int main() { //freopen("in.txt","r",stdin); char op,lp,rp; int l,r; while(~scanf("%s %c%d,%d%c", &op,&lp,&l,&r,&rp)) { l<<=1; r<<=1; if(lp=='(') l++; if(rp==')') r--; if(op=='U') updata(1,0,N,l,r,1); else if(op=='I'){ updata(1,0,N,0,l-1,0); updata(1,0,N,r+1,N,0); } else if(op=='D') updata(1,0,N,l,r,0); else if(op=='C'){ updata(1,0,N,0,l-1,0); updata(1,0,N,r+1,N,0); updata(1,0,N,l,r,2); } else if(op=='S') updata(1,0,N,l,r,2); } query(1, 0, N); #if 0 for(int i=0; i<=100; i++) cout<<vis[i]; cout<<endl; #endif bool ok=1; bool yes=0; bool y=0; for(int i=0; i<=N; i++){ if(vis[i] && ok){ if(y) printf(" "); y=1; yes=1; ok=0; if(i&1) printf("(%d,", i>>1); else printf("[%d,", i>>1); } if(vis[i] && vis[i+1]==0 && ok==0){ ok=1; if(i&1) printf("%d)", (i>>1)+1); else printf("%d]", i>>1); } } if(yes==0) printf("empty set"); printf("\n"); return 0; }