http://codeforces.com/contest/566/problem/D

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 somedepartment. 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 useteam(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.

最麻烦就是第2种情况了,合并一个区间。

但是合并一次过后,它就是属于同一个组了,那么可以设tonext[i]表示第i个人所属的组,的下一个组,就是下一个和它合并的组。

开始的时候tonext[i] = i + 1

那么如果合并[x, y]区间,首先合并x和tonext[x],然后合并tonext[x] 和 tonext[tonext[x]]

那么回溯的时候顺便更新个tonext,以后合并就是跳着合并了,不会一个个暴力合并。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
int fa[maxn];
int tonext[maxn];
int tofind(int x) {
if (fa[x] == x) return x;
else return fa[x] = tofind(fa[x]);
}
void tomerge(int u, int v) {
u = tofind(u);
v = tofind(v);
fa[v] = u;
} void dfs(int be, int en, int cur, int pre) {
if (cur > en) {
tonext[pre] = cur;
return;
}
tomerge(pre, cur);
dfs(be, en, tonext[cur], cur);
tonext[pre] = tonext[cur];
} void work() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = ; i <= n; ++i) {
fa[i] = i;
tonext[i] = i + ;
}
for (int i = ; i <= q; ++i) {
int op, x, y;
scanf("%d%d%d", &op, &x, &y);
if (op == ) {
tomerge(x, y);
} else if (op == ) {
dfs(x, y, tonext[x], x);
} else {
if (tofind(x) == tofind(y)) {
printf("YES\n");
} else printf("NO\n");
}
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

D. Restructuring Company 并查集 + 维护一个区间技巧的更多相关文章

  1. VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集

    D. Restructuring Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  2. codeforces 566D D. Restructuring Company(并查集)

    题目链接: D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  3. CodeForces 566D Restructuring Company (并查集+链表)

    题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...

  4. CodeForces - 566D Restructuring Company 并查集的区间合并

    Restructuring Company Even the most successful company can go through a crisis period when you have ...

  5. hihoCoder #1291 : Building in Sandbox 逆向处理+并查集维护

    /** 题目:#1291 : Building in Sandbox 链接:https://hihocoder.com/problemset/problem/1291 题意:就是一个三维的空间里,按照 ...

  6. [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]

    题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...

  7. Codeforces325 D【并查集维护连通性】

    参考:大牛blog 思路: 因为是环,所以可以复制一下图,先判断一下和他是不是和与他相邻的8个之一的一个障碍使得构成了一个环,环就是一个连通,用并查集维护即可: 如果没有就ans++,然后并把这个点加 ...

  8. 2019牛客暑期多校训练营(第八场)E:Explorer(LCT裸题 也可用线段树模拟并查集维护连通性)

    题意:给定N,M,然后给出M组信息(u,v,l,r),表示u到v有[l,r]范围的通行证有效.问有多少种通行证可以使得1和N连通. 思路:和bzoj魔法森林有点像,LCT维护最小生成树.  开始和队友 ...

  9. The Preliminary Contest for ICPC Asia Xuzhou 2019 【 题目:so easy】{并查集维护一个数的下一个没有被删掉的数} 补题ING

    题意:给[1,n],n个数,有两种操作: 1 x,删去x2 x,查询还未被删去的数中大于等于x的最小的数是多少. input: output: 做法:按照并查集的方法压缩路径 代码: #include ...

随机推荐

  1. listen 77

    Your Dog Wants YOUR Food Does your puppy turn his nose up at his own chow—because he wants some of w ...

  2. hdu-5858 Hard problem(数学)

    题目链接: Hard problem Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Othe ...

  3. HihoCoder1672 : 区间问题([Offer收割]编程练习赛41)(贪心)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定N个区间[Ai, Bi],请你找到包含元素最少的整数集合S,使得每个区间都至少有两个整数在S中. 例如给定三个区间[1 ...

  4. 【Python】File IO

    1.numpy.genfromtxt() numpy.genfromtxt() CSV文件很容易被numpy类库的genfromtxt方法解析 2.

  5. 解决mongodb查询慢的问题

    最近项目上一直在用mongodb作为数据库,mongodb有他的优势,文档型类json格式存储数据,修改起来比传统的关系型数据库更方便,但是最近在用mongodb出现了查询缓慢的问题,我用命令行查询, ...

  6. bzoj 4555 [Tjoi2016&Heoi2016] 求和 —— 第二类斯特林数+NTT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4555 关于第二类斯特林数:https://www.cnblogs.com/Wuweizhen ...

  7. Elasticsearch搜索引擎版本配置

    简要描述: 搜索引擎版本配置 产品 版本号 ES版本要求 说明 PHP =5.5.38     Java =1.8.0_73   用于支持ES Elasticsearch =2.3.5   搜索引擎 ...

  8. Advanced R之数据结构

    看了几本R语言语法相关的书籍,感觉都不怎么好,在实际使用过程中仍然遇到很多难以理解的问题,后来看了Hadley Wickham的Advanced R,好多问题迎刃而解,今天重温了该书的第一章即数据结构 ...

  9. 运用flask、flask-restful开发rest风格的接口,并使用蓝图增加代码的延展性和可扩展性。

    本人做为一个测试人员,之前也有写过,想要测试好接口,那必须要知道如何开发一个接口的重要性. 之前也写过通flask或者flask-retful开发接口,但那些只是一些最简单的demo,不具有很好延展性 ...

  10. 2.5玩转xargs

    我们可以利用管道将一个命令的stdout(标准输出)重定向到另一个命令的stdin(标准输入).有些命令只能以命令行参数的形式接受数据,而无法通过stdin接受数据流.这时候就没法使用管道.那么xar ...