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.

Example

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

ac代码:

#include<stdio.h>
int f[],next[];
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy) f[fy]=fx;
}
int main()
{
int n,q,x,y,z,i,j;
scanf("%d%d",&n,&q);
for(i=;i<=n;i++){
f[i]=i;
next[i]=i+;
}
for(i=;i<=q;i++){
scanf("%d%d%d",&x,&y,&z);
if(x==) join(y,z);
else if(x==){
int fz=find(z);
for(j=y;j<=z;){
f[find(j)]=fz;
int t=j;
j=next[j];
next[t]=next[z]; //区间合并
}
}
else{
if(find(y)==find(z)) printf("YES\n");
else printf("NO\n");
}
}
return ;
}

CodeForces - 566D Restructuring Company 并查集的区间合并的更多相关文章

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

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

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

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

  3. VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集

    D. Restructuring Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

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

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

  5. CodeForces - 357C Knight Tournament 伪并查集(区间合并)

    Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...

  6. BZOJ4668: 冷战 [并查集 按秩合并]

    BZOJ4668: 冷战 题意: 给定 n 个点的图.动态的往图中加边,并且询问某两个点最早什 么时候联通,强制在线. 还可以这样乱搞 并查集按秩合并的好处: 深度不会超过\(O(\log n)\) ...

  7. bzoj2733 / P3224 [HNOI2012]永无乡(并查集+线段树合并)

    [HNOI2012]永无乡 每个联通块的点集用动态开点线段树维护 并查集维护图 合并时把线段树也合并就好了. #include<iostream> #include<cstdio&g ...

  8. 【bzoj4668】冷战 并查集按秩合并+朴素LCA

    题目描述 1946 年 3 月 5 日,英国前首相温斯顿·丘吉尔在美国富尔顿发表“铁幕演说”,正式拉开了冷战序幕. 美国和苏联同为世界上的“超级大国”,为了争夺世界霸权,两国及其盟国展开了数十年的斗争 ...

  9. BZOJ 3910 并查集+线段树合并

    思路: 1. 并查集+线段树合并 记得f[LCA]==LCA的时候 f[LCA]=fa[LCA] 2.LCT(并不会写啊...) //By SiriusRen #include <cstdio& ...

随机推荐

  1. 【转载】FAT32文件系统详解

    硬盘是用来存储数据的,为了使用和管理方便,这些数据以文件的形式存储在硬盘上.任何操作系统都有自己的文件管理系统,不同的文件系统又有各自不同的逻辑组织方式.例如:常见的文件系统有FAT,NTFS,EXT ...

  2. websotrom 2016.2 license Server

    license server” 输入:http://114.215.133.70:41017 仅供学习测试使用,支持正版.

  3. cuda9,cuda8分享百度云下载

    一.文件名称: md5-cuda9cuda-repo-ubuntu1704-9-0-local_9.0.176-1_amd64.debcuda-repo-ubuntu1604-9-0-local_9. ...

  4. Oracle 一行拆分为多行

    测试数据: CREATE TABLE t (str VARCHAR2(30)); INSERT INTO t VALUES ( 'X,Y,Z' ); INSERT INTO t VALUES ( 'X ...

  5. 从TFS中的现有项目复制一份作为新项目,导致提交的服务器无法加载

    解决方案: 1.编辑 .csproj文件,改为自己的名字 2.取消解绑

  6. CSS的两种盒模型

    盒模型一共有两种模式,一种是标准模式,另一种就是怪异模式. 当你用编辑器新建一个html页面的时候你一定会发现最顶上都会有一个DOCTYPE标签,例如: <!DOCTYPE HTML PUBLI ...

  7. 原来浏览器原生支持JS Base64编码解码 outside of the Latin1 range

    原来浏览器原生支持JS Base64编码解码 « 张鑫旭-鑫空间-鑫生活 https://www.zhangxinxu.com/wordpress/2018/08/js-base64-atob-bto ...

  8. mongodb学习之:主从复制

    在sql server能够做到读写分离,双机热备份和集群部署,这些在mongodb也能做到.首先来看主从复制.我们就在一台电脑上进行操作 第一步:分别建立master和slave两个文件夹 第二步:开 ...

  9. ORA-02298: 无法验证 (PNET.POST_CLOB_FK) - 未找到父项关键字

    在运行以下语句的时候,报错如下: ALTER TABLE PN_POST ADD CONSTRAINT POST_CLOB_FK FOREIGN KEY (POST_BODY_ID) REFERENC ...

  10. 微信 jssdk 逻辑在 vue 中的运用

    微信 jssdk 在 vue 中的简单使用 import wx from 'weixin-js-sdk'; wx.config({ debug: true, appId: '', timestamp: ...