Restructuring Company CodeForces - 566D

题目:

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 xand 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 x and 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 000, 1 ≤ 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 y work 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.

扫描二维码关注公众号,回复: 2612397 查看本文章

Examples

input

Copy

8 6
3 2 5
1 2 5
3 2 5
2 4 7
2 1 2
3 1 7

output

Copy

NO
YES
YES

题意:根据输入的数字进行相应的操作,1是将之后的两位数连在一起,2是将之后的两个数所在的区间合并,3是判断之后的两个数是否在同一区间里。

1.3操作没有什么难度,难就难在了区间合并,需要加一个next数组,对每个元素所在集合的右端进行记录。

ac代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<stack>
#include<queue>
#define maxn 300100
using namespace std;
//next指向下一个没合并的区间 
int far[maxn],next[maxn];
int n,q;
void init()
{
	for(int i=1;i<=n;i++)
	{
		far[i]=i;
		next[i]=i+1;
	}
}
int find(int x)
{
	if(x==far[x])
		return x;
	return far[x]=find(far[x]);
}
void merge(int a,int b)
{
	int pa=find(a);
	int pb=find(b);
	if(pa!=pb)
		far[pb]=pa;
}
int main()
{
	scanf("%d%d",&n,&q);
	init();
	for(int i=0;i<q;i++)
	{
		int ans,a,b,r=0;
		scanf("%d%d%d",&ans,&a,&b);
		if(ans==1)
			merge(a,b);
		else if(ans==2)
		{
			if(a>b) swap(a,b);
			for(int i=a+1;i<=b;i=r)
			{
				merge(i-1,i);
				r=next[i];
				next[i]=next[b];
			}
		}
		else
		{
			if(find(a)==find(b))
				printf("YES\n");
			else
				printf("NO\n");
		}
	}
	return 0;
}

if(a>b) swap(a,b);//保证右端点是大端,才能以右端点为准,进行区间合并

for(int i=a+1;i<=b;i=r)

{

       merge(i-1,i);//直接连接下一个区间

       r=next[i]; //将r移到合并后区间的右端

       next[i]=next[b];//该数的右端点直接跑到b区间的右端点

}

猜你喜欢

转载自blog.csdn.net/qq_42505741/article/details/81120923
今日推荐