博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces 566 D.Restructuring Company
阅读量:4521 次
发布时间:2019-06-08

本文共 3167 字,大约阅读时间需要 10 分钟。

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.

Example
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 大体的题意是分三种 第一种是合并x和y,第二是合并x到y的元素,第三就是输出x,y是否合并了 很容易想到要用并查集,最初使用并查集第三个测试点超时,于是想第二个要求比较耗时间,但是x到y的元素都得合并,不能省去(也不会),但是可能会出现一些区间包含已经合并的区间这样的话就应该略过这些区间,于是想到在用一个数组用来记录此元素之后该扫描的编号 可还是超时,再注意到把cin改成scanf就好了 代码:
#include 
#include
using namespace std;int emp[200001],refer[200001],n,q,ins,x,y,l=999999,r=0;///refer 指向下一个没合并的区间 防止再扫描已经扫描的区间int ini(){ for(int i=1;i<=n;i++) emp[i]=i,refer[i]=i+1;}int getf(int a){ if(emp[a]==a)return a; return emp[a]=getf(emp[a]);}void merge(int a,int b){ int aa=getf(a),bb=getf(b); emp[bb]=aa;}int main(){ scanf("%d%d",&n,&q); ini(); for(int i=0;i
y)swap(x,y);///区间方向不定 for(int i=x+1;i<=y;i=r) { merge(i-1,i); r=refer[i]; refer[i]=refer[y]; } } else { if(getf(x)==getf(y))cout<<"YES"<

 

转载于:https://www.cnblogs.com/8023spz/p/7208905.html

你可能感兴趣的文章
pycharm 快捷键
查看>>
Linux常用命令
查看>>
AutoFac IoC DI 依赖注入
查看>>
.net中的设计模式---单例模式
查看>>
安装程序工具 (Installutil.exe)22
查看>>
python 学习(pip工具的安装)
查看>>
博客园在我的博客添加点击小心心特效
查看>>
如何简单解释 MapReduce算法
查看>>
微软Office Online服务安装部署(二)
查看>>
从 0 到 1 实现 React 系列 —— 1.JSX 和 Virtual DOM
查看>>
面向接口编程详解(二)——编程实例
查看>>
解决java.lang.NoClassDefFoundError: org/apache/log4j/Level
查看>>
端口号
查看>>
mysql for macOS安装
查看>>
iOS中的KeyChain的用途
查看>>
jquery与checkbox的checked属性的问题
查看>>
HDU5092——Seam Carving(动态规划+回溯)(2014上海邀请赛重现)
查看>>
java 格式化字符串
查看>>
[.Net]轻量ORM——Dapper
查看>>
语言基础
查看>>