D. Restructuring Company

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/566/problem/D

Description

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.

Sample Input

8 6
3 2 5
1 2 5
3 2 5
2 4 7
2 1 2
3 1 7

Sample Output

NO
YES
YES

HINT

题意

有n个人,都在不同的部门,然后有三个操作

1.合并x,y所在的部门

2.合并l,l+1....r所在的部门

3.查询x,y是否在同一个部门

题解:

并查集直接维护就好了

注意维护区间更新的时候,跳着更新就好了

代码:

#include<stdio.h>
#include<iostream>
using namespace std;
#define maxn 205000
int fa[maxn];
int step[maxn];
int fin(int x)
{
if(fa[x]!=x)
fa[x]=fin(fa[x]);
return fa[x];
}
int main()
{
int n,q;scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
fa[i]=i,step[i]=i-;
for(int i=;i<=q;i++)
{
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op==)
fa[fin(x)]=fa[fin(y)];
else if(op==)
{
int t = step[y];
for(int j=y;j>=x;j=t)
{
t = step[j];
fa[fin(j)]=fa[fin(x)];
step[j]=step[x];
}
}
else
{
if(fin(x)==fin(y))printf("YES\n");
else printf("NO\n");
}
}
}

VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集的更多相关文章

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

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

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

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

  3. CodeForces - 566D Restructuring Company 并查集的区间合并

    Restructuring Company Even the most successful company can go through a crisis period when you have ...

  4. 【VK Cup 2015 - Finals D】Restructuring Company

    [题目链接]:http://codeforces.com/problemset/problem/566/D [题意] 给你n个人; 一开始每个人都隶属于一个部门; 之后给你q个操作; 3种操作类型; ...

  5. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)A B C 水 并查集 思路

    A. Bear and Big Brother time limit per test 1 second memory limit per test 256 megabytes input stand ...

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

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

  7. Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!

    VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...

  8. Bubble Cup 12 - Finals Online Mirror, unrated, Div. 1

    Bubble Cup 12 - Finals Online Mirror, unrated, Div. 1 C. Jumping Transformers 我会状压 DP! 用 \(dp[x][y][ ...

  9. Bubble Cup 11 - Finals [Online Mirror, Div. 1]题解 【待补】

    Bubble Cup 11 - Finals [Online Mirror, Div. 1] 一场很好玩的题啊! I. Palindrome Pairs 枚举哪种字符出现奇数次. G. AI robo ...

随机推荐

  1. 武汉北大青鸟解读2016年10大IT热门岗位

    武汉北大青鸟解读2016年10大IT热门岗位 2016年1月5日 13:37 北大青鸟 这是IT从业者的辉煌时代,IT行业的失业率正处在历史的低点,而且有的岗位——例如网络和安全工程师以及软件开发人员 ...

  2. 函数buf_LRU_free_block

    /******************************************************************//** Try to free a block. If bpag ...

  3. Sencha touch Panel之间的跳转(如不使用TabPanel或者Carousel控件而产生跳转的动画效果)

    常规的Sencha touch 应用都是"header content footer"结构,这样的结构无疑将使用TabPanel来实现,而且TabPanel肯定是card布局,这样 ...

  4. CSS和SVG中的剪切——clip-path属性和<clipPath>元素

    剪切是什么 剪切是一个图形化操作,你可以部分或者完全隐藏一个元素.被剪切的元素可以是一个容器也可以是一个图像元素.元素的哪些部分显示或隐藏是由剪切的路径来决定的. 剪切路径定义了一个区域,在这个区域内 ...

  5. 《深入Java虚拟机学习笔记》- 第18章 finally子句

    本章主要介绍字节码实现的finally子句.包括相关指令以及这些指令的使用方式.此外,本章还介绍了Java源代码中finally子句所展示的一些令人惊讶的特性,并从字节码角度对这些特征进行了解释. 1 ...

  6. andorid 进度条

    SeekBar类似于ProgressBar,但是ProgressBar的主要功能是让用户知道目前的状态,而SeekBar的功能在于让用户调整进度,举个例子,在音乐播放器中,可以通过调整SeekBar来 ...

  7. google学术反向代理及IPV6免流量上网【教育网BUPT】

    google反向代理 google https://awk.so/ 学术反向代理 https://awk.so/scholar/?hl=zh-CN 2015年1.1号开始流量计费,2元/G 无VPS用 ...

  8. Lua语法

    1.Lua保留的关键字: and,bread,do,else,elseif,end,false,for,function,if,in,local,nil,not,or,repeat,return ,t ...

  9. linux 安装mongodb

    Linux 安装mongodb 1.下载mongodb linux wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon- ...

  10. Clean Code – Chapter 2: Meaningful Names

    Use Intention-Revealing Names The name should tell you why it exists, what it does, and how it is us ...