VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集
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:
- 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 x and y work at the same department. Note that x can be equal to y in the query of any type.
Output
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 并查集的更多相关文章
- codeforces 566D D. Restructuring Company(并查集)
题目链接: D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- D. Restructuring Company 并查集 + 维护一个区间技巧
http://codeforces.com/contest/566/problem/D D. Restructuring Company time limit per test 2 seconds m ...
- CodeForces - 566D Restructuring Company 并查集的区间合并
Restructuring Company Even the most successful company can go through a crisis period when you have ...
- 【VK Cup 2015 - Finals D】Restructuring Company
[题目链接]:http://codeforces.com/problemset/problem/566/D [题意] 给你n个人; 一开始每个人都隶属于一个部门; 之后给你q个操作; 3种操作类型; ...
- 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 ...
- CodeForces 566D Restructuring Company (并查集+链表)
题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...
- 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 ...
- 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][ ...
- Bubble Cup 11 - Finals [Online Mirror, Div. 1]题解 【待补】
Bubble Cup 11 - Finals [Online Mirror, Div. 1] 一场很好玩的题啊! I. Palindrome Pairs 枚举哪种字符出现奇数次. G. AI robo ...
随机推荐
- ubuntu程序安装方法
以前一直使用window,今天安装了一个ubuntu系统(如果有同学也想装,建议装英文版的),因为以前ubuntu系统用的不多,所以安装软件就是一个问题. 就以安装chrome来说吧: 1.在Goog ...
- Struts1和Struts2都有什么区别?
总的来说,Struts1 的 Action 是单例模式,因此开发者必须保证它是线程安全的或是同步的,因为Struts 1中每个Action仅有一个实例来处理所有的请求. 但是在用Struts ...
- 函数buf_LRU_free_block
/******************************************************************//** Try to free a block. If bpag ...
- 对DataTable里数据进行排序
在日常开发过程中,有一个DataTable集合,里面有很多字段,现在要求针对某一列进行排序,如果该列为数字的话,进行ASC即可实现,但是该字段类型为string,此时排序就有点不正确了. 我也不多废话 ...
- Zepto picLazyLoad Plugin,图片懒加载的Zepto插件
嗯,学着国外人起名字Zepto picLazyLoad Plugin确实看起来高大上,其实js代码没几句,而且我每次写js都捉襟见肘,泪奔--- 图片懒加载有很多js插件,非常著名的属jQuery的L ...
- 如何在一个frame中调用另一个frame中的javascript函数
1.htm <script language="javascript">function test(){alert("测试")}</scrip ...
- 【 D3.js 高级系列 — 1.0 】 文本的换行
在 SVG 中添加文本是使用 text 元素.但是,这个元素不能够自动换行,超出的部分就显示不出来了,怎么办呢? 高级系列开篇前言 从今天开始写高级系列教程.还是那句话,由于本人实力有限,不一定保证入 ...
- jquery滚动条
查看demo: 下载Demo
- C# 中的 ref 和 out 的意义和使用方法
原文C# 中的 ref 和 out 的意义和使用方法 向方法传递一个实参时,对应的形参会用实参的一个副本来初始化,不管形参是值类型(例如 int),可空类型(int?),还是引用类型,这一点都是成立的 ...
- MySQL中间层 Atlas
Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bu ...