POJ3225--Help with Intervals

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:

OperationNotation

Definition

UnionA ∪ B{x : x ∈ A or x ∈ B}
IntersectionA ∩ B{x : x ∈ A and x ∈ B}
Relative complementationA − B{x : x ∈ A but x ∉ B}
Symmetric differenceA ⊕ 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:

CommandSemantics
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] (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)
勉强终于A了。但姿势不够好。1600MS。
注意(3,3)这种类型的数据。开闭区间只需将一个点拆成两个点就能实现
【 2*u  (2*u+1  )2*v-1 】2*v
/*
U其实就是区间置1
D其实就是区间置0
I就是0到a-1 b+1到65535置0
C就是区间取反(只剩中间部分)
S就是区间取反(好像和C相同,整条)
如果是弄个异或标记的话,需要执行异或的时候
先看看这个区间是不是整段被覆盖,是就改覆盖值。
*/
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
using namespace std;
#define maxn 140000
#define lson 2*id,l,mid
#define rson 2*id+1,mid+1,r
vector <int> ans;
struct ST
{
	int l,r;
	int set,orr;//用来标记是否有置值和异或
}st[4*maxn];//因为每个点都要拆成2个
void buildtree(int id,int l,int r)
{
	st[id].l=l;
	st[id].r=r;
	if(l==r)
	{
		st[id].set=st[id].orr=0;
		return;
	}
	int mid=(l+r)>>1;
	buildtree(lson);
	buildtree(rson);
	st[id].set=st[id].orr=0;
}
void PushDown(int id)
{
	if(st[id].set!=-1)
	{
		st[2*id].set=st[2*id+1].set=st[id].set;
		st[2*id].orr=st[2*id+1].orr=0;
		st[id].set=-1;
	}
	if(st[id].orr)
	{
		if(st[2*id].set!=-1)
		{
			st[2*id].set^=1;
		}
		else st[2*id].orr^=1;
		if(st[2*id+1].set!=-1)
		{
			st[2*id+1].set^=1;
		}
		else st[2*id+1].orr^=1;
		st[id].orr=0;
	}
}
void update(int id,int l,int r,int ope)//这个来置0置1吧
{
	if(st[id].l==l && st[id].r==r)
	{
		if(ope!=2)
		{
			st[id].set=ope;
			st[id].orr=0;
			return;
		}
		else
		{
			if(st[id].set != -1)
			{
				st[id].set=st[id].set^1;
			}
			else st[id].orr=st[id].orr^1;
			return;
		}
	}
	PushDown(id);
	if(st[2*id].r >= r)
	{
		update(2*id,l,r,ope);
		return;
	}
	if(st[2*id+1].l <= l)
	{
		update(2*id+1,l,r,ope);
		return;
	}
	update(2*id,l,st[2*id].r,ope);
	update(2*id+1,st[2*id+1].l,r,ope);
}
void query(int id,int l,int r)
{
	if(st[id].set==1 || (st[id].set==0 && st[id].orr))
	{
		ans.push_back(l);
		ans.push_back(r);
		return;
	}
	if(l==r)return;
	PushDown(id);
	query(2*id,l,st[2*id].r);
	query(2*id+1,st[2*id+1].l,r);
}
int main()
{
	char oper,lef,rig;
	int u,v;
	buildtree(1,1,maxn-3000);
	while(cin>>oper)
	{
		cin>>lef;
		scanf("%d",&u);
		getchar();
		scanf("%d",&v);
		cin>>rig;
		//所有的点都要拆成两个点。
		u++;v++;
		if(lef=='(')
		{
			if(rig==')')
			{
				if(oper=='U')
					if(u<v)update(1,2*u+1,2*v-1,1);//
				if(oper=='D')
					if(u<v)update(1,2*u+1,2*v-1,0);
				if(oper=='I')
				{
					update(1,1,2*u,0);
					update(1,2*v,maxn-3000,0);/
				}
				if(oper=='C')//区间取反,只剩下中间部分
				{
					if(u<v)update(1,2*u+1,2*v-1,2);
					update(1,1,2*u,0);
					update(1,2*v,maxn-3000,0);//
				}
				if(oper=='S')
				{
					if(u<v)update(1,2*u+1,2*v-1,2);
				}
			}
			else
			{
				if(oper=='U')
					if(u<v)update(1,2*u+1,2*v,1);
				if(oper=='D')
					if(u<v)update(1,2*u+1,2*v,0);
				if(oper=='I')
				{
					update(1,1,2*u,0);//
					update(1,2*v+1,maxn-3000,0);
				}
				if(oper=='C')//区间取反,只剩下中间部分
				{
					if(u<v)update(1,2*u+1,2*v,2);
					update(1,1,2*u,0);
					update(1,2*v+1,maxn-3000,0);
				}
				if(oper=='S')
				{
					if(u<v)update(1,2*u+1,2*v,2);
				}
			}
		}
		else
		{
			if(rig==')')
			{
				if(oper=='U')
					if(u<v)update(1,2*u,2*v-1,1);
				if(oper=='D')
					if(u<v)update(1,2*u,2*v-1,0);
				if(oper=='I')
				{
					update(1,1,2*u-1,0);
					update(1,2*v,maxn-3000,0);
				}
				if(oper=='C')//区间取反,只剩下中间部分
				{
					if(u<v)update(1,2*u,2*v-1,2);
					update(1,1,2*u-1,0);
					update(1,2*v,maxn-3000,0);
				}
				if(oper=='S')
				{
					if(u<v)update(1,2*u,2*v-1,2);
				}
			}
			else
			{
				if(oper=='U')update(1,2*u,2*v,1);
				if(oper=='D')update(1,2*u,2*v,0);
				if(oper=='I')
				{
					update(1,1,2*u-1,0);
					update(1,2*v+1,maxn-3000,0);
				}
				if(oper=='C')//区间取反,只剩下中间部分
				{
					update(1,2*u,2*v,2);
					update(1,1,2*u-1,0);
					update(1,2*v+1,maxn-3000,0);
				}
				if(oper=='S')
				{
					update(1,2*u,2*v,2);
				}
			}
		}
	}
	//这样的话ans里面估计是按顺序排的
	query(1,1,maxn-3000);
	int t=ans.size()/2;
	for(int i=2;i<ans.size();i+=2)
	{
		if(ans[i]==ans[i-1] || ans[i]==ans[i-1]+1)
		{
			ans[i-1]=-1;
			ans[i]=-1;
			t--;
		}
	}
	int k=1;
	for(int i=0;i<ans.size();i++)
	{
		if(ans[i]==-1)continue;
		int fuck=ans[i]/2-1;
		if(k&1)
		{
			if(ans[i]&1)//如果是个奇数
			{
				printf("(%d,",fuck);
			}
			else printf("[%d,",fuck);
		}
		else
		{
			if(ans[i]&1)
			{
				printf("%d)",(ans[i]+1)/2-1);
			}
			else 
			{
				printf("%d]",(ans[i]+1)/2-1);
			}
			if(k/2==t)printf("\n");
			else printf(" ");
		}
		k++;
	}
	if(k==1)printf("empty set\n");
	return 0;
}



 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值