【CF566#D】 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 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   !

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442047.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

dg oracle 切换模式_谈谈dg切换涉及的概念:switchover和failover区别

概述今天有朋友问了一个问题&#xff1a;switchover和failover之间的区别&#xff0c;有点懵逼&#xff0c;居然把这忘记了&#xff0c;这里总结下两者之间的一些区别。DG架构DG切换时注意点&#xff1a;1、确认主库和从库间网络连接通畅&#xff1b;2、确认没有活动的会话连接…

gis影像格式img转为ecw_医学影像图片格式

&#xff08;1&#xff09;Analyze格式&#xff1a;Analyze格式储存的每组数据组包含2个文件&#xff0c;一个为数据文件&#xff0c;其扩展名为.img&#xff0c;包含二进制的图像资料&#xff1b;另外一个为头文件&#xff0c;扩展名为.hdr&#xff0c;包含图像的元数据。&…

case when then else多个条件_sqlserver条件分支case when使用教程

在sqlserver的条件分支case when有两种写法&#xff1a;1)case 字段 when 值 then 返回值 when 值2 then 返回值2 end2)case when 条件1 then 返回值1 when 条件2 then 返回值2 end方法步骤&#xff1a;1.打开“SQL Server Management Studio”管理工具&#xff0c;创建一张测试…

hashmap value占用空间大小_【Java集合框架002】原理层面:HashMap全解析

一、前言二、HashMap2.1 HashMap数据结构 HashMap线程不安全 哈希冲突2.1.1 HashMap数据结构学习的时候&#xff0c;先整体后细节&#xff0c;HashMap整体结构是 底层数组链表 &#xff0c;先记住&#xff0c;再开始看下面的HashMap相关知识点&#xff1a;底层数据结构&#…

升级bios_华硕B350PLUS升级BIOS更换AMD 3900X步骤

首先CPU更换看下面这张图&#xff0c;注意CPU上的小三角的位置。这块主板如果BIOS没有升级直接更换AMD 3900X的CPU会有点不亮显示器的问题&#xff0c;所以下面讲下华硕B350PLUS这块主板是如何更新到最新的BIOS驱动的(需要在原来老的CPU基础上先升级&#xff0c;升级完再更换39…

实体类 接口_spring-boot-route(五)整合Swagger生成接口文档

目前&#xff0c;大多数公司都采用了前后端分离的开发模式&#xff0c;为了解决前后端人员的沟通问题&#xff0c;后端人员在开发接口的时候会选择使用swagger2来生成对应的接口文档&#xff0c;swagger2提供了强大的页面调试功能&#xff0c;这样可以有效解决前后端人员沟通难…

bootstrap-table 新增可编辑行_现代Web开发堆栈工具DevExtreme 新增Gantt组件,助力项目管理...

点击“了解更多”获取DevExpress DevExtreme v19.2正式版下载DevExtreme拥有高性能的HTML5 / JavaScript小部件集合&#xff0c;使您可以利用现代Web开发堆栈(包括React&#xff0c;Angular&#xff0c;ASP.NET Core&#xff0c;jQuery&#xff0c;Knockout等)构建交互式的Web应…

关抢占 自旋锁_互斥锁、自旋锁、读写锁、悲观锁、乐观锁的应用场景

前言生活中用到的锁&#xff0c;用途都比较简单粗暴&#xff0c;上锁基本是为了防止外人进来、电动车被偷等等。但生活中也不是没有 BUG 的&#xff0c;比如加锁的电动车在「广西 - 窃格瓦拉」面前&#xff0c;锁就是形同虚设&#xff0c;只要他愿意&#xff0c;他就可以轻轻松…

dvwa详解_DVWA(六):XSSReflected 反射型XSS全等级详解

XSS 概念&#xff1a;由于web应用程序对用户的输入过滤不严&#xff0c;通过html注入篡改网页&#xff0c;插入恶意脚本&#xff0c;从而在用户浏览网页时&#xff0c;控制用户浏览器的一种攻击。XSS类型&#xff1a;Reflected(反射型)&#xff1a;只是简单的把用户输入的数据反…

直线的端点画垂线的lisp_【以课说法】线段、射线、直线

课例概况课例点晴你的课堂是碎片化的知识教学&#xff0c;还是结构化的问题探究&#xff1f;如何把碎片化的知识教学变成结构化的问题探究&#xff1f;实施路径就是问题串&#xff0c;用问题串统整知识点&#xff1b;围绕问题串&#xff0c;从问题引发、问题探究&#xff0c;直…

ehchache验证缓存过期的api_Ehcache缓存配置

Cache的配置很灵活&#xff0c;官方提供的Cache配置方式有好几种。你可以通过声明配置、在xml中配置、在程序里配置或者调用构造方法时传入不同的参数。你可以将Cache的配置从代码中剥离出来&#xff0c;也可以在使用运行时配置&#xff0c;所谓的运行时配置无非也就是在代码中…

alert 回调_JavaScript中到底什么时候回调函数Callback

什么是回调函数Callback简单的理解&#xff1a;回调函数是在另一个函数执行完毕后执行的函数 - 因此名称为call back。复杂的理解&#xff1a;在JavaScript中&#xff0c;函数是对象。因此&#xff0c;函数可以将函数作为参数&#xff0c;并且可以由其他函数返回。执行此操作的…

finereport文本框如何实现多值查询_如何实现参数级联查询

参数级联查询是查询控件之间的一种互动方式&#xff0c;比如在某个下拉框选定选项后&#xff0c;另一个下拉框里的选项范围会随之变化。润乾报表提供了多种编辑风格&#xff0c;每种编辑风格都有丰富的属性&#xff0c;以此为基础实现参数级联查询也很简单。下面就通过一个例子…

【HDU - 5015 】233 Matrix (矩阵快速幂)

题干&#xff1a; In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 233…

mysql datetime month不走索引_like百分号加前面一定不走索引吗?一不小心就翻车,关于mysql索引那些容易错的点...

like百分号加前面一定不走索引吗&#xff1f;正常来讲&#xff0c;我们都知道在mysql的like查询中&#xff0c;百分号加在关键词后面是走索引的&#xff0c;比如 select * like "张三%"&#xff0c;而百分号在前面是不走索引的&#xff0c;比如 select * like "…

ACM竞赛、数论内容常用的定理(求解(a/b)%c,乘法逆元,费马小定理)

如果b与c互素&#xff0c;则(a/b)%ca*b^((c)-1)%c其中是欧拉函数。或者(a/b)%ca*b^(c-2)%c 如果b与c不互素&#xff0c;则(a/b)%c(a%bc)/b 对于b与c互素和不互素都有(a/b)%c(a%bc)/b成立 乘法逆元用扩展欧几里得定理&#xff1a; 例题&#xff1a;ZOJ - 3609 题干&#xf…

自定义菜单url不能带_微服务架构【SpringBoot+SpringCloud+VUE】五 || 实战项目微信公众号自定义开发...

本章主要讲解微信公众号自定义菜单、微信网页开发、模板消息推送等功能的实现&#xff1b;发福利了&#xff0c;下方关注公众号&#xff0c;就能免费获取项目源码1、自定义菜单开发前需要了解以下几点&#xff1a;1、微信公众号的自定义菜单最多包括3个一级菜单&#xff0c;每个…

C语言编程中关于负数的%运算的判定。

如果 % 两边的操作数都为正数&#xff0c;则结果为正数或零&#xff1b;如果 % 两边的操作数都是负数&#xff0c;则结果为负数或零。C99 以前&#xff0c;并没有规定如果操作数中有一方为负数&#xff0c;模除的结果会是什么。C99 规定&#xff0c;如果 % 左边的操作数是正数&…

无数种求逆元的方法总结

乘法逆元 对于缩系中的元素&#xff0c;每个数a均有唯一的与之对应的乘法逆元x&#xff0c;使得ax≡1(mod n) 一个数有逆元的充分必要条件是gcd(a,n)1&#xff0c;此时逆元唯一存在 逆元的含义&#xff1a;模n意义下&#xff0c;1个数a如果有逆元x&#xff0c;那么除以a相当于…

python两个元组相加_《第5章 Python 列表与元组》5.1.3 序列相加(Adding)!

《高中信息技术 Python编程》 教学案《第5章 Python 列表与元组》 5.1.3 序列相加(Adding)&#xff01;06:151 #使用加号可以进行序列的连接操作&#xff0c;输入如下&#xff1a;2 >>>[1,2,3][4,5,6]3 [1,2,3,4,5,6]4 >>>a[1,2]5 >>>b[5,6]6 >&…