Restructuring Company

Even the most successful company can go through a crisis period when you have to make a hard decision — to restructure, discard and merge departments, fire employees and do other unpleasant stuff. Let's consider the following model of a company.

There are n people working for the Large Software Company. Each person belongs to some department. Initially, each person works on his own project in his own department (thus, each company initially consists of n departments, one person in each).

However, harsh times have come to the company and the management had to hire a crisis manager who would rebuild the working process in order to boost efficiency. Let's use team(person) to represent a team where person person works. A crisis manager can make decisions of two types:

  1. Merge departments team(x) and team(y) into one large department containing all the employees of team(x) and team(y), where x and y (1 ≤ x, y ≤ n) — are numbers of two of some company employees. If team(x) matches team(y), then nothing happens.
  2. Merge departments team(x), team(x + 1), ..., team(y), where x and y(1 ≤ x ≤ y ≤ n) — the numbers of some two employees of the company.

At that the crisis manager can sometimes wonder whether employees xand y (1 ≤ x, y ≤ n) work at the same department.

Help the crisis manager and answer all of his queries.

Input

The first line of the input contains two integers n and q (1 ≤ n ≤ 200 0001 ≤ q ≤ 500 000) — the number of the employees of the company and the number of queries the crisis manager has.

Next q lines contain the queries of the crisis manager. Each query looks like type x y, where . If type = 1 or type = 2, then the query represents the decision of a crisis manager about merging departments of the first and second types respectively. If type = 3, then your task is to determine whether employees x and ywork at the same department. Note that x can be equal to y in the query of any type.

Output

For each question of type 3 print "YES" or "NO" (without the quotes), depending on whether the corresponding people work in the same department.

Example
Input
8 6
3 2 5
1 2 5
3 2 5
2 4 7
2 1 2
3 1 7
Output
NO
YES
YES

这道题直接用并查集暴力合并会超时,所以需要在第二种情况下进行优化,只要对这个区间中的一个元素合并过,就可以直接跳到这个区间的末端

那么先挂学长的博客:

点击打开链接http://blog.csdn.net/cillyb/article/details/75000393

#include<cstdio>
void p(int a);
void mer(int a,int b);
int find(int a);
int pre[200010]={0},an[200010]={0};
int main()
{
	int n,q;
	scanf("%d%d",&n,&q);
	p(n);
	while(q--)
	{
		int num,x,y,i,z;
		scanf("%d",&num);
		if(num==1)
		{
			scanf("%d%d",&x,&y);
			if(x==y)
				continue;
			if(pre[x]!=pre[y]) 
			mer(x,y);
		}
		else if(num==2)
		{
			scanf("%d%d",&x,&y);
			if(x==y)
				continue;
			if(y<x)
			{
				z=x;
				x=y;
				y=z;
			}
			for(i=x;i<y;i=z)
			{
				if(pre[i]!=pre[i+1]) 
					mer(i,i+1);
				z=an[i];
				an[i]=y;				
			}
			
		}
		else if(num==3)
		{
			scanf("%d%d",&x,&y);
			if(x==y)
				printf("YES\n");
			else
			{
				if(find(x)!=find(y))
					printf("NO\n");
				else
					printf("YES\n");
			}
		}
	}
	return 0;
}
void p(int a)
{
	int i;
	for(i=1;i<=a;i++)
	{
		pre[i]=i;
		an[i]=i+1;
	}
}
void mer(int a,int b)
{
	int fa,fb;
	fa=find(a);
	fb=find(b);
	if(fa!=fb)
		pre[fb]=fa;
}
int find(int a)
{
	int r;
	r=a;
	while(r!=pre[r])
		r=pre[r];
	while(a!=pre[a])
	{
		int z;
		z=a;
		a=pre[a];
		pre[z]=r;
	}
	return r;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值