D. Restructuring Company

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/566/problem/D

Description

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.

Sample Input

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

Sample Output

NO
YES
YES

HINT

题意

有n个人,都在不同的部门,然后有三个操作

1.合并x,y所在的部门

2.合并l,l+1....r所在的部门

3.查询x,y是否在同一个部门

题解:

并查集直接维护就好了

注意维护区间更新的时候,跳着更新就好了

代码:

#include<stdio.h>
#include<iostream>
using namespace std;
#define maxn 205000
int fa[maxn];
int step[maxn];
int fin(int x)
{
if(fa[x]!=x)
fa[x]=fin(fa[x]);
return fa[x];
}
int main()
{
int n,q;scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
fa[i]=i,step[i]=i-;
for(int i=;i<=q;i++)
{
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op==)
fa[fin(x)]=fa[fin(y)];
else if(op==)
{
int t = step[y];
for(int j=y;j>=x;j=t)
{
t = step[j];
fa[fin(j)]=fa[fin(x)];
step[j]=step[x];
}
}
else
{
if(fin(x)==fin(y))printf("YES\n");
else printf("NO\n");
}
}
}

VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集的更多相关文章

  1. codeforces 566D D. Restructuring Company(并查集)

    题目链接: D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  2. D. Restructuring Company 并查集 + 维护一个区间技巧

    http://codeforces.com/contest/566/problem/D D. Restructuring Company time limit per test 2 seconds m ...

  3. CodeForces - 566D Restructuring Company 并查集的区间合并

    Restructuring Company Even the most successful company can go through a crisis period when you have ...

  4. 【VK Cup 2015 - Finals D】Restructuring Company

    [题目链接]:http://codeforces.com/problemset/problem/566/D [题意] 给你n个人; 一开始每个人都隶属于一个部门; 之后给你q个操作; 3种操作类型; ...

  5. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)A B C 水 并查集 思路

    A. Bear and Big Brother time limit per test 1 second memory limit per test 256 megabytes input stand ...

  6. CodeForces 566D Restructuring Company (并查集+链表)

    题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...

  7. Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!

    VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...

  8. Bubble Cup 12 - Finals Online Mirror, unrated, Div. 1

    Bubble Cup 12 - Finals Online Mirror, unrated, Div. 1 C. Jumping Transformers 我会状压 DP! 用 \(dp[x][y][ ...

  9. Bubble Cup 11 - Finals [Online Mirror, Div. 1]题解 【待补】

    Bubble Cup 11 - Finals [Online Mirror, Div. 1] 一场很好玩的题啊! I. Palindrome Pairs 枚举哪种字符出现奇数次. G. AI robo ...

随机推荐

  1. Android开发之极光推送基本步骤

    [转]:http://wyong.blog.51cto.com/1115465/1402842 两天在研究推送的问题,后来确定了用极光推送,本人将整个过程整理一下: 1.到极光官网注册账号:https ...

  2. Pomelo服务器琐碎方法

    1.获取客户端ip地址:session__session__.__socket__remoteAddress.ip 2.日志文件无法打印到文件,删除node_modules/pomelo/node_m ...

  3. php discuz框架接口不能正常访问的问题

    本人php小白,无php编程基础,直接上php服务器部署,后果很严重.....所以务必看完请给”顶“给评论,以表示对小白的鼓励和赞赏! 关于discuz框架,独自加班,废寝忘食,然已无力吐槽..... ...

  4. HNOI2004打鼹鼠(LIS)

    大水题…… 不过通过这题我们应该养成一个好习惯:好好看清题…… 竟然没有看到时限 10sec…… var i,j,n,m,ans:longint; f,time,x,y:..] of longint; ...

  5. PopupWindow-弹窗的界面

      1 效果图 2 知识点 PopupWindow(View contentView, int width, int height) //创建一个没有获取焦点.长为width.宽为height,内容为 ...

  6. DB2创建序列

    一.创建序列 序列是按照一定的规则生产的数值,序列的作用非常的大,比如银行交易中的流水号,就是记录每笔交易的关键字段. 通过create sequence语句创建序列,具体语法如下: >> ...

  7. sharepoint 2010 页面添加footer方法 custom footer for sharepoint 2010 master page

    转:http://blog.csdn.net/chenxinxian/article/details/8720893 在sharepoint 2010的页面中,我们发现,没有页尾,如果我们需要给页面添 ...

  8. C#百万数据查询超时问题

    用c#从百万数据中筛选一些信息时,经常会出现程序连接超时的错误,常见的错误很多,例如:Timeout expired. The timeout period elapsed prior to comp ...

  9. HDU 3007 Buried memory & ZOJ 1450 Minimal Circle

    题意:给出n个点,求最小包围圆. 解法:这两天一直在学这个神奇的随机增量算法……看了这个http://soft.cs.tsinghua.edu.cn/blog/?q=node/1066之后自己写了好久 ...

  10. HDU 5699 货物运输 二分判定

    转自:http://blog.csdn.net/jtjy568805874/article/details/51480479 #include <cstdio> #include < ...