【CodeForces-566D】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 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 xand 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.

Examples

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

题意:

有n个人,他们的编号为1~n,接下来有q条信息。信息包含三个内容 a b c,a为1时,是b c两个人所在部门合并合并,a为2是[b,c]这个区间中的所有人所在部门合并,a为3时,时查询b c在不在同一个部门。

思路:

这是一道并查集的题目,1,3操作容易实现,但2操作容易超时,一开始我直接写了个for循环遍历区间中的所有元素,结果超时了。看了网上大佬的题解后,我知道了,这样做有重复的步骤,比如说已经将3到7这些人合并了,但在进行1到9区间合并时,又重复合并了一遍,所以有一部分可以不用遍历。因此用一个数组a记录每个人后面的第一个不和他属于同一个部门的人的下标,在进行2操作时把这个区间中所有人的值都改为y,这样不论从哪里进入这个区间,下一步都会直接跳到区间结束的下一个。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring> 
#include<queue>
#include<stack> 
#include<string.h>
#include<set>
using namespace std;
int f[200005];
int a[200005];
int n,q;
void init()
{
	for(int i=0;i<=n;i++)
	{
		f[i]=i;
		a[i]=i+1;
	}
	
}
int getf(int v)
{
	if(f[v]!=v)
	f[v]=getf(f[v]);
	return f[v];
}
void merge(int u,int v)
{
	int t1=getf(u);
	int t2=getf(v);
	if(t1!=t2)
	{
		f[t2]=t1;
	}
}
int join(int u,int v)
{
	int t1=getf(u);
	int t2=getf(v);
	if(t1==t2)
	{
		return 1;
	}
	else return 0;
}
int main()
{
	scanf("%d%d",&n,&q);
	init();
	int ty,x,y;
	for(int i=0;i<q;i++)
	{
		scanf("%d%d%d",&ty,&x,&y);
		if(ty==1)
		{
			merge(x,y);
		}
		else if(ty==2)
		{
			int temp;
			for(int j=x+1;j<=y;j=temp)
			{
				merge(j-1,j);
				temp=a[j];
				a[j]=a[y];//这个地方写a[j-1]会超时
			}
		}
		else if(ty==3)
		{
			if(join(x,y))
			{
				printf("YES\n");
			}
			else printf("NO\n");
		 } 
	}
	return 0;
 } 

 

 

CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值