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 ...
随机推荐
- javascript中this的使用
终于知道某些大神在写js插件的时候为什么第一句都是"var that=this",来看看下面的这个例子,大家都会懂啦: <script type="text/jav ...
- JS 获取Button控件的提交类型
<script type="text/javascript"> <!--获取button控件的类型---> function isAuditOrCancel ...
- SCOI2009windy数
数位DP,还不怎么会…… 其中calc函数的计算分为三部分: 第一部分:统计最高位为0的情况,或者说不足最高位位数的数的个数 第二部分:统计最高位为1到a[len]-1的情况,直接调用数组即可 第三部 ...
- JPA-一对多关系
JPA中,一对多关系使用@OneToMany标示 关系维护端: package com.yl.demo1.bean.oneTomany; import javax.persistence.Cascad ...
- 怎么学数学[How to Study Math]
- 那些跟钱有关的事儿 z
这是两段朴实的创业笔记,作者是王信文,2009年南京大学本科毕业,2009年9月到2013年3月在腾讯上海的互动娱乐部门工作,2013年3月到现在和几个前同事一起创立了莉莉丝游戏(手游刀塔传奇是他们后 ...
- the type initializer for '' threw an exception
the type initializer for '' threw an exception 问题:程序启动时初始化主窗口类时,弹出该错误.调查:查看类的构造函数是否会有异常抛出.解决:去掉类的构造函 ...
- HDU 1495 非常可乐 BFS搜索
题意:有个为三个杯子(杯子没有刻度),体积为s,n,m,s=m+n, 刚开始只有体积为s的杯子装满可乐,可以互相倒,问你最少的次数使可乐均分,如果没有结果,输出-1; 分析:直接互相倒就完了,BFS模 ...
- [OFBiz]开发 一
1.使用Eclipse3.7.1 + subclipse plugins 1.8.2(svn client)http://subclipse.tigris.org/servlets/ProjectDo ...
- 中文Win7下成功安装calabash-android步骤
Calabash-android是支持android的UI自动化测试框架,网上看见很多同学说,安装calabash比较费劲,特别是Windows下安装,也没有一个详细的安装手册可供参考.正好,今天在W ...