CF566#D Restructuring Company (并查集---合并区间操作)

Posted wx62a8776062513

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF566#D 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. Lets 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. Lets use team(person) to represent a team where person person works. A crisis manager can make decisions of two types:

  1. Merge departmentsteam(x) andteam(y) into one large department containing all the employees ofteam(x) andteam(y), wherexandy(1 ≤x,yn) — are numbers of two of some company employees. Ifteam(x) matchesteam(y), then nothing happens.
  2. Merge departmentsteam(x),team(x+ 1), ...,team(y), wherexandy(1 ≤xyn) — 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.

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个点q个操作。“1” 代表合并u v两个点,“2” 代表合并u,v及其中间的所有点,“3”代表查询这两个点是否在一个区间内。

解题报告:

并查集的区间合并操作。

错误代码:()

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAX = 200000 + 5;
int n,m;
int f[MAX];//关系集合
int uu[MAX];//uu[i]记录的是下一个不在当前集合区间内的点

int getf(int v)

return f[v] == v? v:f[v]=getf(f[v]);

void merge(int u,int v)

int t1,t2;
t1=getf(u);
t2=getf(v);
if(t1!=t2)

f[t2]=t1;


int join(int u,int v)

int t1,t2;
t1=getf(u);
t2=getf(v);
if(t1==t2)return 0;
else return 1;

void init()
for(int i = 1; i<=n; i++)
f[i]=i;uu[i]=i+1;//u[i]记录的是下一个不在当前集合区间内的点


int main()

int op,u,v,tmp;
while(~scanf("%d%d",&n,&m) )
int sum=0;
init();
while(m--)
scanf("%d %d %d",&op,&u,&v);
if(op == 1)
merge(u,v);

else if(op == 2)
for(int i = u; i<v; i=tmp)
tmp = uu[i];
merge(i,i+1);
uu[i]=uu[v];



else
if(getf(u) == getf(v) )
printf("YES\\n");

else printf("NO\\n");



return 0;

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAX = 200000 + 5;
int n,m;
int f[MAX];//关系集合
int uu[MAX];//uu[i]记录的是下一个不在当前集合区间内的点

int getf(int v)

if(f[v]!=v)
f[v]=getf(f[v]);
return f[v];

void merge(int u,int v)

int t1,t2;
t1=getf(u);
t2=getf(v);
if(t1!=t2)

f[t2]=t1;


int join(int u,int v)

int t1,t2;
t1=getf(u);
t2=getf(v);
if(t1==t2)return 0;
else return 1;

void init()
for(int i = 1; i<=n; i++)
f[i]=i;uu[i]=i+1;//u[i]记录的是下一个不在当前集合区间内的点


int main()

int op,u,v,tmp;
while(~scanf("%d%d",&n,&m) )
int sum=CF566E Restoring Map

CF566C Logistical Questions(10-1)

javascript Nested_Object_Restructuring

APP测试之-网址

Restructuring Company和Almost Union-Find 并查集的区间合并与并查集的删除

Codeforces Round #566 (Div. 2)