poj 3225 Help with Intervals 线段树lazy-tag求解区间运算

//poj 3225
#include <iostream>
using namespace std;
#define lx (x<<1)
#define rx ((x<<1)|1)
#define MID ((l+r)>>1)
const int MAXN=140000;
int cover[MAXN<<2];
int XOR[MAXN<<2];
bool vis[MAXN+10];

void FXOR(int x)
{
	if(cover[x]!=-1) cover[x]^=1;
	else XOR[x]^=1;
}

void pushDown(int x)
{
	if(cover[x]!=-1){
		cover[lx]=cover[rx]=cover[x];
		XOR[lx]=XOR[rx]=0;
		cover[x]=- 1;
	}
	if(XOR[x]){
		FXOR(lx);
		FXOR(rx);
		XOR[x]=0;
	}
}

void update(int L,int R,char op,int l,int r,int x)
{
	if(L<=l&&r<=R){
		if(op=='U'){
			cover[x]=1;
			XOR[x]=0;
		}else if(op=='D'){
			cover[x]=0;
			XOR[x]=0;
		}else if(op=='C') FXOR(x);
		else if(op=='S') FXOR(x);	
		return ;
	}
	pushDown(x);	
	if(L<=MID) update(L,R,op,l,MID,lx);
	else if(op=='I'||op=='C') cover[lx]=XOR[lx]=0;
	if(MID<R) update(L,R,op,MID+1,r,rx);
	else if(op=='I'||op=='C') cover[rx]=XOR[rx]=0;
}

void query(int l,int r,int x)
{
	if(cover[x]==1){
		for(int i=l;i<=r;++i)
			vis[i]=true;
		return ;
	}
	if(cover[x]==0)
		return ;
	if(l==r) 
		return ;
	pushDown(x);
	query(l,MID,lx);
	query(MID+1,r,rx);		
}

int main()
{
	int a,b;
	char op,lchar,rchar;
	while(scanf("%c %c%d,%d%c\n",&op,&lchar,&a,&b,&rchar)==5){
		a<<=1;
		b<<=1;
		if(lchar=='(') ++a;
		if(rchar==')') --b;
		update(a,b,op,0,MAXN,1);
	}
	query(0,MAXN,1);
	int s=-1,e;
	bool flag=false;
	for(int i=0;i<=MAXN;++i){
		if(vis[i]==1){
			if(s==-1)
				s=i;
			e=i;
		}else{
			if(s!=-1){
				if(flag) printf(" ");
				printf("%c%d,%d%c",s&1?'(':'[',s/2,(e+1)/2,e&1?')':']');
				s=-1;
				flag=true;
			}
		}
	}
	if(!flag) printf("empty set");
	puts(""); 
	return 0;	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值