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. caffeModels--models-caffes-大全

    caffe的伯克利主页:http://caffe.berkeleyvision.org/caffe的github主页:https://github.com/BVLC/caffe caffe的model ...

  2. 转载---- 使用opencv源码自己编制android so库的过程

    http://blog.csdn.net/lantishua/article/details/21182965 工作需要,在Android上使用OpenCV.opencv当前的版本(2.4.8)已经有 ...

  3. win10下rose2003安装与破解(图解)

    系统刷成了win10的,因为选择的是全新安装的方式,所以开发工具又得又一次安装了,rose尽管好用.但是安装破解还是有点麻烦,这里整理一下.备用,下回就不须要去网上搜索了. 安装文件下载地址:链接: ...

  4. Leetcode 002-Search Insert Position

    #Given a sorted array and a target value, return the index if the target is found. If not, return th ...

  5. 判断一个IP地址是否是本局域网内地址

    //        /// <summary>        /// 判断一个IP地址是否是本局域网内地址,是返回true 否则返回false,        /// </summa ...

  6. mybatis入门(四)

    mybatis入门 需求:根据id查询用户的信息 mysql数据库: CREATE TABLE `user` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `us ...

  7. linux环境下远程访问ftp

    sftp ftpuser@127.0.0.1 使用sftp命令 ftpuser是用户名 127.0.0.1是目标ip 亲测好使.

  8. Centos7利用kvm搭建Windows虚拟机

    这几天玩了一下kvm虚拟化,真的很有意思,我把这几天踩的坑,还有收获,都记录下来,作为以后的复习和检查. 首先说一下我的基本逻辑,我有一台win7的笔记本,我的底层虚拟化是使用VMWare构建的Cen ...

  9. SIP学习笔记 -- RFC 3261

    1.SDP (rfc 4566)    1)用于交换参数    2)内容分三部分Session description, Time description and Media description ...

  10. spring boot 使用redis 及redis工具类

    1-添加maven依赖 2-添加redis配置 3-工具类 1-添加maven依赖 实际上是封装了jedis <!-- redis 依赖--> <dependency> < ...