题干:

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

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个点q个操作。“1” 代表合并u v两个点,“2” 代表合并u,v及其中间的所有点,“3”代表查询这两个点是否在一个区间内。

解题报告:

并查集的区间合并操作。

错误代码:()

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAX = 200000 + 5;
int n,m;
int f[MAX];//关系集合
int uu[MAX];//uu[i]记录的是下一个不在当前集合区间内的点   int getf(int v)
{return f[v] == v? v:f[v]=getf(f[v]);
}
void merge(int u,int v)
{int t1,t2;t1=getf(u);t2=getf(v);if(t1!=t2){f[t2]=t1;}
}
int join(int u,int v)
{int t1,t2;t1=getf(u);t2=getf(v);if(t1==t2)return 0;else return 1;
}
void init() {for(int i = 1; i<=n; i++) {f[i]=i;uu[i]=i+1;//u[i]记录的是下一个不在当前集合区间内的点 }
}
int main()
{int op,u,v,tmp; while(~scanf("%d%d",&n,&m) ) {int sum=0;init();while(m--) {scanf("%d %d %d",&op,&u,&v); if(op == 1) {merge(u,v);}else if(op == 2) {for(int i = u; i<v; i=tmp) {tmp = uu[i];merge(i,i+1);uu[i]=uu[v];}}else {if(getf(u) == getf(v) ) {printf("YES\n");}else printf("NO\n");}}}   return 0;
}

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAX = 200000 + 5;
int n,m;
int f[MAX];//关系集合
int uu[MAX];//uu[i]记录的是下一个不在当前集合区间内的点   int getf(int v)
{if(f[v]!=v)f[v]=getf(f[v]);return f[v];
}
void merge(int u,int v)
{int t1,t2;t1=getf(u);t2=getf(v);if(t1!=t2){f[t2]=t1;}
}
int join(int u,int v)
{int t1,t2;t1=getf(u);t2=getf(v);if(t1==t2)return 0;else return 1;
}
void init() {for(int i = 1; i<=n; i++) {f[i]=i;uu[i]=i+1;//u[i]记录的是下一个不在当前集合区间内的点 }
}
int main()
{int op,u,v,tmp; while(~scanf("%d%d",&n,&m) ) {int sum=0;init();while(m--) {scanf("%d %d %d",&op,&u,&v); if(op == 1) {merge(u,v);}else if(op == 2) {for(int i = u + 1; i<=v; i=tmp) {tmp = uu[i];merge(i-1,i);uu[i]=uu[v];}}else {if(getf(u) == getf(v) ) {printf("YES\n");}else printf("NO\n");}}} return 0;
}

总结:

1.看清输出YES 还是Yes   !

【CF566#D】 Restructuring Company (并查集---合并区间操作)相关推荐

  1. 算法(并查集--合并集合)

    并查集–合并集合 一共有n个数,编号是1~n,最开始每个数各自在一个集合中. 现在要进行m个操作,操作共有两种: "M a b",将编号为a和b的两个数所在的集合合并,如果两个数已 ...

  2. HDU 2473 Junk-Mail Filter(并查集的删除操作)

    题目地址:HDU 2473 这题曾经碰到过,没做出来. .如今又做了做,还是没做出来. ... 这题涉及到并查集的删除操作.想到了设一个虚节点,可是我把虚节点设为了要删除的点的父节点.一直是栈溢出,目 ...

  3. Almost Union-Find UVA - 11987(并查集的删除操作)

    题意:求出每个集合的元素个数,及总和,给出三个操作: 1 将含有a元素和b元素的集合合并:2 将a元素放入含有b元素的集合中:3 输出a元素所在集合的元素个数及总和: 思路:正常并查集,与并查集元素的 ...

  4. 并查集的一般操作 ③

    RT 题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友. 两个强盗是同一 ...

  5. *【HDU - 2473】Junk-Mail Filter (并查集--删点操作)

    题干: Recognizing junk mails is a tough task. The method used here consists of two steps:  1) Extract ...

  6. 树形结构 —— 并查集 —— 并查集的删除操作

    对于删除操作,在完美的并查集中(所有节点都直接连接在根节点上),理论上只要把要删除的节点的上级重新指向自己就可以了. 但是实际情况中,并查集形成的树的形态都是不可预估的,如果直接将一个节点指向自己可能 ...

  7. P1196 [NOI2002] 银河英雄传说 (并查集 合并

    添加链接描述 #include<bits/stdc++.h> using namespace std; const int N=5e5+9; int fa[N],sum[N],front[ ...

  8. 并查集板子:acwing836. 合并集合

    文章目录 并查集原理 并查集代码实现 并查集原理 并查集的常用操作: 将两个集合合并 询问两个元素是否在同一个集合中 并查集在近乎O(1)的时间内完成以上两个操作. 基本原理:每个集合用一棵树表示.树 ...

  9. 【CodeForces - 371D】Vessels(思维,元素合并,并查集)

    题干: There is a system of n vessels arranged one above the other as shown in the figure below. Assume ...

最新文章

  1. CloudStack无法添加模板和iso
  2. ASP:在静态页面中显示文章被阅读的次数
  3. c语言程序装萝卜,萝卜花园练习win7系统安装SkyDrive的图文步骤
  4. Java之for和while的内容
  5. go restful 安全_Go语言构建 RESTful Web 服务
  6. 每天2小时,吃透 985博士总结的这份目标检测、卷积神经网络和OpenCV学习资料笔记(20G高清/PPT/代码)...
  7. dsoframer java_DSOFramer的使用
  8. STM32f4应用层学习之路(零基础学习STM单片机要注意的是什么? 如何从一个小白入门,你需要掌握哪些知识? 有哪些适合新手的单片机项目?)
  9. Docker系列(8) Docker网络(3)-- 单机Docker网络配置
  10. 免费图床-树洞外链-阿离图床
  11. Padavan挂载SMB共享及编译ffmpeg
  12. 【QA】数学符号 word输入问题 在word里面怎么输入字母头顶上的那个小尖儿
  13. 《PNG文件格式》(二)PNG文件格式分析
  14. 游戏公司奇葩富豪身家仅次许家印,征集长腿美女生娃,女友房产超百套
  15. 如何压缩带有mdw安全文件机制和密码的的access数据库
  16. 移动端:M站和APP的区别
  17. IBM助力中国企业跨越数据临界点 走进认知商业时代
  18. Anaconda和pip换源
  19. 原创| Python中“等于“到底用 == 还是 is ?
  20. ​​​​​​​PowerPoint快捷键

热门文章

  1. [程序员面试金典][JAVA][第02.01题][移除重复节点][Set][双指针]
  2. [Leedcode][JAVA][第136题][第137题][只出现一次的数字][位运算][HashSet][HashMap]
  3. 怎样创建两个菜单JAVA_java – 如何创建一个菜单的JButton?
  4. 常见笔顺错误的字_最全汉字书写笔顺规则
  5. 幅值与峰峰值的计算_电厂振动测量、计算基础及汽轮机组振动标准!
  6. odbc远程连接mysql_无法使用unixodbc,libmyodbc连接到远程mysql服务器
  7. 在过程中要正式批准可交付成果_干货!软考高项项目管理知识体系5大过程组47个过程...
  8. visualvm远程监控jvm_大型企业JVM实战:优化及面试热点分析
  9. phpstudy(自己电脑主机做服务器,手机网站界面打不开)
  10. 940B. Our Tanya is Crying Out Loud