CodeForces

来源:互联网 发布:php curl 跳转页面 编辑:程序博客网 时间:2024/05/18 02:46

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 0001 ≤ 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.

Examples
input
8 63 2 51 2 53 2 52 4 72 1 23 1 7
output
NOYESYES

这个题操作一合并区间并不难做,把父亲节点做一下合并就好了,关键是操作二,这个总不能去合并区间每一个元素吧。

我们发现,其实对应的这几个操作产生的所有区间都是连续的,所以我们完全可以记录每个元素所在元素的右边界(或者左边界)。当我们对一个范围内区间合并的时候,总能从他的右边界开始合并。

具体代码说更清楚:


#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<stack>using namespace std;typedef struct {int r;//元素的右边界int p;//父亲节点} node;node a[200005];int find(int x)//寻找父亲节点{int r = x;while(r!= a[r].p){r = a[r].p;}while(a[x].p!= r)//压缩路径{int temp = a[x].p;a[x].p = r;x = temp;}return r;}void init(int n)//初始化{for(int i = 1;i<= n;i++){a[i].p = i;a[i].r = i+1;}}int main(){int n,m;scanf("%d %d",&n,&m);init(n);int x,y,o;while(m--){scanf("%d %d %d",&o,&x,&y);if(o == 1){a[find(x)].p = a[find(y)].p;//把两个父亲节点合并一下就可以}else if(o == 2){int fy = a[find(y)].p;while(x<= y){int temp = a[x].r;//把x的右边界先记录下来a[find(x)].p = fy;//把x的祖先设置为fya[x].r = a[y].r;//把要合并的区间的最右端点的右边界设置为x的右边界x = temp;//直接跳到右边界}}else {if(find(x) == find(y))printf("YES\n");else printf("NO\n");}}return 0;}


还有一篇跟这个差不多的题,不同做法,有兴趣看下。Almost Union-Find

ACM好题心得



原创粉丝点击