题目链接:

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. 我是这样使用template.js来异步渲染数据的demo

    直接来代码吧! <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  2. DevOps 初学者的入门指南

    原文地址:http://blog.csdn.net/FIRim/article/details/52681704 当我们谈到 DevOps 时,可能讨论的是:流程和管理,运维和自动化,架构和服务,以及 ...

  3. html中图片上传预览的实现

    本地图片预览 第一种方法 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type& ...

  4. CentOS中文乱码的问题

    修改CentOS 6.4 root用户的系统默认语言设置 最近用Virtual Box 虚拟了一个CentOS系统,版本6.4,安装时使用简体中文.发现用普通用户登录的时候 设置语言环境为Englis ...

  5. substring,subsequence,charAt执行效率的不同

    package com.java.tencent; public class T_2_longestPalindrome { public String test1(String s){ long s ...

  6. IPv4地址(一)概述

    IPv4地址的长度是多少? IPv4地址是如何表示的? IPv4地址的构成以及每一部分所起到的作用和占的位数特点? IPv4地址长度为32位. IPv4地址分为两部分:网络号和主机号 网络号部分惟一地 ...

  7. erlang的随机数 及 random:uniform()函数

    每次调用会更新进程字典里的random_seed变量,这样在同一个进程内每次调用random:uniform()时,随机数种子都不同,所以生成的随机数都不一样(调用完random:uniform()后 ...

  8. 一些编译php时的configure 参数

    一些编译php时的configure 参数 ./configure –prefix=/usr/local/php php 安装目录 –with-apxs2=/usr/local/apache/bin/ ...

  9. java ResultSet获得总行数

    在Java中,获得ResultSet的总行数的方法有以下几种. 第一种:利用ResultSet的getRow方法来获得ResultSet的总行数 Statement stmt = con.create ...

  10. python cookbook第三版学习笔记七:python解析csv,json,xml文件

    CSV文件读取: Csv文件格式如下:分别有2行三列. 访问代码如下: f=open(r'E:\py_prj\test.csv','rb') f_csv=csv.reader(f) for f in ...