题意:区间操作,交,并,补等
思路:
我们一个一个操作来分析:(用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
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<functional>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 131072;
const int inf = 1<<30;//0x7f;
typedef __int64 LL;
int Xor[maxn<<2]; //取反标记
bool Hash[maxn+5];
struct node
{
int cov;
}tree[maxn<<2];
void FXor( int rt ) //取反
{
if( tree[rt].cov != -1 ) tree[rt].cov ^= 1;
else Xor[rt] ^= 1;
}
void Pushdown( int rt )
{
if( tree[rt].cov != -1 ){
tree[rt<<1].cov = tree[rt<<1|1].cov = tree[rt].cov;
Xor[rt<<1] = Xor[rt<<1|1] = 0;
tree[rt].cov = -1;
}
if( Xor[rt] ){
FXor(rt<<1);
FXor(rt<<1|1);
Xor[rt] = 0;
}
}
void updata( int rt,int ld,int rd,char op,int s,int e )
{
if( s <= ld && rd <= e )
{
if( op == 'U' ){
tree[rt].cov = 1;
Xor[rt] = 0;
}
else if( op == 'D' )
tree[rt].cov = Xor[rt] = 0;
else if( op == 'C' || op == 'S' )
FXor(rt);
return;
}
Pushdown(rt);
int mid = (ld+rd)>>1;
if( s <= mid ) updata( rt<<1,ld,mid,op,s,e );
else if( op == 'I' || op == 'C' )
tree[rt<<1].cov = Xor[rt<<1] = 0;
if( mid < e ) updata( rt<<1|1,mid+1,rd,op,s,e );
else if( op == 'I' || op == 'C' )
tree[rt<<1|1].cov = Xor[rt<<1|1] = 0;
}
void query( int rt,int ld,int rd ){
if ( tree[rt].cov == 1) {
for ( int it = ld ; it <= rd ; it ++) {
Hash[it] = true;
}
return ;
} else if ( tree[rt].cov == 0 ) return ;
Pushdown(rt);
int mid = (ld + rd) >> 1;
query( rt<<1,ld,mid );
query( rt<<1|1,mid+1,rd );
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
char op,lch,rch;
int s,e;
tree[1].cov = Xor[1] = 0;
while( scanf("%c %c%d,%d%c\n",&op,&lch,&s,&e,&rch) != EOF )
{
s <<= 1; e <<= 1;
if( lch == '(' ) s ++;
if( rch == ')' ) e --;
if( s > e ){
if( op == 'C' || op == 'I' )
tree[1].cov = Xor[1] = 0;
}
else
{
updata( 1,0,maxn,op,s,e );
}
}
query( 1,0,maxn );
bool flag = 0;
s = -1,e;
for( int i = 0; i <= maxn; i ++ )
{
if( Hash[i] ){
if( s == -1 ) s = i;
e = i;
}
else{
if( s != -1 ){
if( flag ) printf(" ");
flag = true;
printf("%c%d,%d%c",s&1?'(':'[' , s>>1 , (e+1)>>1 , e&1?')':']');
s = -1;
}
}
}
if ( !flag ) printf("empty set");
puts("");
return 0;
}