CodeForces 566D(并查集&合并区间)

D. Restructuring Company
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 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
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

题意 n个公司,初始是每一个所有都是独立的
m次操作
1 x y; 把x和y合并
2 x y; 把x,x+1,,,,,,y-1,y,所有的合并
3 x y; 询问x,y是否为一家;

关键在于操作2。如果每次都暴力从x到y所有的检查边并合并,会超时;这里参考了其他人的博客,另外设一个数组存放往右侧数第一个不属于它的数的id,这样在合并时,就不用重复检查直接跳到未合并的位置

#include
#include
#include
#include
using namespace std;
const int maxn = 200000+5;
int per[maxn];
int book[maxn];//记录右侧第一个不属于它的id
int n,m,a,b,c;
int Find(int x)
{
    int cx=x;
    while(cx!=per[cx]){
        cx=per[cx];
    }
    int i=x,j;
    while(i!=cx){
        j=per[i];
        per[i]=cx;
        i=j;
    }
    return cx;
}
void join(int x,int y)
{
    int cx=Find(x),cy=Find(y);
    if(cx!=cy){
        per[cy]=cx;
    }

}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
        per[i]=i,book[i]=i+1;
    for(int i=1;i<=m;i++){
        scanf("%d %d %d",&a,&b,&c);
        if(a==1){
            join(b,c);
        }
        else if(a==2){
            int to=0;
            > for(int j=b+1;j<=c;j=to){
            >     join(j,j-1);
            >     to=book[j];
            >     book[j]=book[c];//当一段区间结束时,每一个的右侧第一个不属于的将和c相同
            >     }

        }
        else {
            if(Find(b)==Find(c)) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

你可能感兴趣的:(并查集)