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:

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.
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
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个人,后来需要对一些部门进行合并,有3种操作,1 x y 是将x 和 y部门进行合并,2 x y是指将 x 到 y 所有的部门都合并,3 x y是询问x y是否是一个部门。这个题目很明显是要用并查集的,但是,如果2操作很多,而且每次都是一个一个的进行合并,那么一定会TLE的,所以我们就想到需要一个记录该点之后一直到和他一个部门的标号是多少,如果找到该点,那么和这个点一个部门的就可以直接跳过了,直接到该点记录的标号的下一个继续合并就可以了。

AC代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

int n, p;
int pre[200010], nex[200010];

int Find(int x)
{
    int r = x;
    while(pre[r] != r) r = pre[r];
    int i = x, j;
    while(pre[i] != r)
    {
        j = pre[i];
        pre[i] = r;
        i = j;
    }
    return r;
}

void join(int a, int b)
{
    int x = Find(a), y = Find(b);
    pre[y] = x;
    return ;
}

int main()
{
    cin >> n >> p;
    for(int i = 1; i <= n; i++)
        pre[i] = i, nex[i] = i+1;
    while(p--)
    {
        int cmd, x, y, to;
        scanf("%d%d%d", &cmd, &x, &y);
        if(cmd == 1) join(x, y);
        else if(cmd == 2)
        {
            for(int i = x+1; i <= y; i = to)
            {
                join(i-1, i);
                to = nex[i];
                nex[i] = nex[y]; //他要和y成为一个部门,所以他下一个不是自己部门的就是y的下一个
            }
        }
        else puts(Find(x)==Find(y) ? "YES" : "NO");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值