题目链接:

D. Restructuring Company

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 xand 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. Iftype = 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 题意: 三种操作,type==1,把x,y合并到一个集合里面;type==2,把x,x+1,x+2...y合并到一个集合里;type==3,query x和y是否在一个集合里; 思路: 很显然是并查集,只是type==2是对应的是区间操作,但发现如果是在一个集合里了还要操作就是浪费,可以把已经在一个区间的压缩,用一个pre数组,pre[i]表示数i的前面和i不是一个集合的与i最近的数;这样可以一次合并后下一次直接跳过中间多余的; AC代码:
/*
2014300227 566D - 25 GNU C++11 Accepted 249 ms 3732 KB
*/
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+;
typedef long long ll;
int n,p[N],pre[N],q,type,x,y;
int findset(int x)
{
if(x == p[x])return x;
return p[x] = findset(p[x]);
}
int main()
{
scanf("%d%d",&n,&q);
for(int i = ;i <= n;i++)
{
p[i] = i;
pre[i] = i-;
}
while(q--)
{
scanf("%d%d%d",&type,&x,&y);
if(type == )
{
if(findset(x) == findset(y))printf("YES\n");
else printf("NO\n");
}
else if(type == )
{
int fx = findset(x),fy = findset(y);
p[fx] = fy;
}
else
{
int r;
for(int i = y;i >= x;i = r)
{
r = pre[i];
if(r<x)break;
p[findset(r)] = p[findset(i)];
pre[i] = pre[r];
}
}
}
return ;
}

codeforces 566D D. 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 Restructuring Company 并查集的区间合并

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

  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 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  6. Codeforces 731C:Socks(并查集)

    http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...

  7. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  8. Codeforces 1027F Session in BSU - 并查集

    题目传送门 传送门I 传送门II 传送门III 题目大意 有$n​$门科目有考试,第$i​$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)​$,要求每门科目至少参加 ...

  9. CodeForces - 455C Civilization (dfs+并查集)

    http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...

随机推荐

  1. Android · 广告走灯

    layout <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ...

  2. Oracle 连接、会话数的查看,修改

    http://blog.csdn.net/xiaoyao6650/article/details/4027041 查看processes #当前的连接数 select count(*) from v$ ...

  3. Delphi列表控件TListView定位到某一行。

    ListView1.Item[100].Focused = true; //定位到索引为100的行ListView1.Item[100].Selected = true; ListView1.Item ...

  4. WampServer无法直接打开myprojects的解决方法

    https://jingyan.baidu.com/article/7e4409533ace042fc1e2ef40.html

  5. Cannot create __weak reference in file using manual reference counting

    Xcode更新到7.3后会出现NSObject+MJProperty.h报Cannot create __weak reference in file using manual reference c ...

  6. Android开发:LocationManager获取经纬度及定位过程(附demo)

    在Android开发其中.常常须要用到定位功能,尤其是依赖于地理位置功能的应用.非常多人喜欢使用百度地图,高德地图提供的sdk.开放API,可是在只须要经纬度,或者城市,街道地址等信息.并不须要提供预 ...

  7. c++ 通过数据流方式实现文件拷贝

    #include "stdafx.h"#include <iostream>#include<fstream>using namespace std;voi ...

  8. php总结1 ——php简介、工作原理、运行环境、文件构成、语法结构、注释

    1.1 PHP  超文本预处理程序.实际就是制作网站的脚本程序 1.2 运行环境: wamp——windowns+apache+mySQL+php    常用于开发.学习和研究 lamp ——linu ...

  9. 我的Java开发学习之旅------>Workspace in use or cannot be created, choose a different one.--错误解决办法

    今天使用Eclipse时,突然卡死了,然后我强制关闭了Eclipse,再重新打开的时候就报错了,错误如下: Workspace in use or cannot be created, choose  ...

  10. Error: Failed to fetch plugin E:_My_File______workMyCodemyCodecordova-workspaceplugman-testMyMath via registry. Probably this is either a connection problem, or plugin spec is incorrect.

    $ cordova plugin add E:\_My_File_____\_work\MyCode\myCode\cordova-workspace\plugman-test\MyMath --sa ...