CodeForces - 566D Restructuring Company 并查集的区间合并
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:
- 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.
- 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
8 6
3 2 5
1 2 5
3 2 5
2 4 7
2 1 2
3 1 7
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 并查集的区间合并的更多相关文章
- CodeForces 566D Restructuring Company (并查集+链表)
题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...
- codeforces 566D D. Restructuring Company(并查集)
题目链接: D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- 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 ...
- D. Restructuring Company 并查集 + 维护一个区间技巧
http://codeforces.com/contest/566/problem/D D. Restructuring Company time limit per test 2 seconds m ...
- CodeForces - 357C Knight Tournament 伪并查集(区间合并)
Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...
- BZOJ4668: 冷战 [并查集 按秩合并]
BZOJ4668: 冷战 题意: 给定 n 个点的图.动态的往图中加边,并且询问某两个点最早什 么时候联通,强制在线. 还可以这样乱搞 并查集按秩合并的好处: 深度不会超过\(O(\log n)\) ...
- bzoj2733 / P3224 [HNOI2012]永无乡(并查集+线段树合并)
[HNOI2012]永无乡 每个联通块的点集用动态开点线段树维护 并查集维护图 合并时把线段树也合并就好了. #include<iostream> #include<cstdio&g ...
- 【bzoj4668】冷战 并查集按秩合并+朴素LCA
题目描述 1946 年 3 月 5 日,英国前首相温斯顿·丘吉尔在美国富尔顿发表“铁幕演说”,正式拉开了冷战序幕. 美国和苏联同为世界上的“超级大国”,为了争夺世界霸权,两国及其盟国展开了数十年的斗争 ...
- BZOJ 3910 并查集+线段树合并
思路: 1. 并查集+线段树合并 记得f[LCA]==LCA的时候 f[LCA]=fa[LCA] 2.LCT(并不会写啊...) //By SiriusRen #include <cstdio& ...
随机推荐
- Active Directory虚拟机搭建域控服务器环境
前言 还是和上一章一样,痛苦过后还是记录下给后来人提供便利为妙. 虚拟机选择:建议Hyper-V或者VMware 系统选择:建议WIindows Server 2003及以上 我这里是使用VMware ...
- springboot中tomcat找不到jsp页面【转载】
这个原理还没搞明白,只知道是内嵌的tomcat找jsp时默认不读取resources目录,但是具体的默认读取的是哪个目录,打了一下午断点我也没找到.... 修改方式,添加配置修改tomcat的读取目录 ...
- java创建文件夹以及文件
java在创建文件的过程中如果改文件的路径不存在: 会出现下面这种情况 java.io.IOException: 系统找不到指定的路径. at java.io.WinNTFileSystem.crea ...
- A Windows GUI for Appium
A Windows GUI for Appium If you are new to Appium then please see the Getting started guide for more ...
- 7-5 打印选课学生名单(25 point(s)) 【排序】
7-5 打印选课学生名单(25 point(s)) 假设全校有最多40000名学生和最多2500门课程.现给出每个学生的选课清单,要求输出每门课的选课学生名单. 输入格式: 输入的第一行是两个正整数: ...
- 转回java,项目遇到的环境相关问题记录
fastjson解析报错,兼容java8的time包:需要升级fastjson版本到1.2.9 https://www.oschina.net/question/129411_142776 j ...
- 10个常见的 Android 新手误区
在过去十年的移动开发平台中,作为资深的移动开发人员,我们认为Android平台是一个新手最广为人知的平台.它不仅是一个廉价的工具,而且有着良好的开发社区,以及从所周知的编程语言(Java),使得开发A ...
- Eclipse 插件使用
1. AmaterasUML:UML 类图(class diagram) 注意这里是先编写好代码,通过插件根据代码逻辑关系生成类图: 安装AmaterasUML前,需要先安装 GEF,采用 eclip ...
- June 26,程序破解
1.android程序破解练习初级 方法一: 文件名:KeygenMe#1.apk工具:ApktoolGui v2.0 Final 先用ApktoolGui v2.0 Final反编译成java通过查 ...
- Java笔记(八)
GUI: awt和swing: java.awt:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能.属于重量级控件. javax.swing:在awt的基 ...