CodeForces 566D Restructuring Company
来源:互联网 发布:mac sdk 环境变量配置 编辑:程序博客网 时间:2024/06/14 06:01
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 somedepartment. Initially, each person works on his own project in his own department (thus, each company initially consists ofn 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 useteam(person) to represent a team where personperson works. A crisis manager can make decisions of two types:
- Merge departments team(x) andteam(y) into one large department containing all the employees ofteam(x) and team(y), where x andy (1 ≤ x, y ≤ n) — are numbers of two of some company employees. Ifteam(x) matches team(y), then nothing happens.
- Merge departments team(x), team(x + 1), ..., team(y), wherex 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.
The first line of the input contains two integers n andq (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 liketype x y, where . Iftype = 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 employeesx and y work at the same department. Note thatx can be equal to y in the query of any type.
For each question of type 3 print "YES" or "NO" (without the quotes), depending on whether the corresponding people work in the same department.
8 63 2 51 2 53 2 52 4 72 1 23 1 7
NOYESYES
并查集。第一个操作很好写,第二个操作如果一个个来的话容易超时,我们可以让每一个团队等于它后面的那一个团队,然后合并的时候就可以省去一些步骤
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int tree[200010],team[200010];int find(int x){return x==tree[x]?x:tree[x]=find(tree[x]);}void merge(int x,int y){int a=find(x);int b=find(y);if(a!=b) tree[a]=b;}int main(){//ios::sync_with_stdio(0);int n,m;cin>>n>>m;for(int i=0;i<200010;i++){tree[i]=i;team[i]=i+1;}while(m--){int op,x,y;//cin>>op>>x>>y;scanf("%d%d%d",&op,&x,&y);if(op==1) merge(x,y);else if(op==2){int flag=0;for(int i=x+1;i<=y;){merge(i-1,i);flag=team[i];team[i]=team[y];i=flag;}}else{if(find(x)==find(y)) cout<<"YES"<<endl;else cout<<"NO"<<endl;}}}
- Codeforces 566D Restructuring Company
- CodeForces 566D Restructuring Company
- CodeForces 566 D.Restructuring Company(并查集)
- CodeForces 566 D.Restructuring Company(并查集)
- 【并查集】Codeforces 566D Restructuring Company
- codeforces Restructuring Company
- Restructuring Company CodeForces
- Restructuring Company
- Restructuring Company
- VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集 stl应用
- VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集 set 二分
- CodeForces-771D-Bear and Company
- Kefa and Company Codeforces#321
- CodeForces 580BKefa and Company
- Codeforces 794C. Naming Company
- Codeforces Round #321 B Kefa and Company
- CodeForces 39B Company Income Growth
- codeforces-580B Kefa and Company(二分)
- RocketMQ——Producer篇:其他功能介绍
- 让你的App,一键换肤吧
- 就 3 点,提升工作效率
- springcloud(七):配置中心svn示例和refresh
- NSSearchField如何响应键盘事件?
- CodeForces 566D Restructuring Company
- Hbase设计以及优化
- 为什么需要指针,不要可以吗
- 老毛桃地址
- Ajax实现原理详解
- ((NULL) null).printNULL();((NULL) null).printnull();
- MySQL数据库SYS CPU高的可能性分析
- EventBus讲解
- 多态