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. codeforces 622E E. Ants in Leaves(贪心+dfs)

    题目链接: E. Ants in Leaves time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. kettle及数据库导数_20160920

    一.kettle是什么. Kettle是一款国外开源的ETL( Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).转换(transform).加载 ...

  3. 【Python】数组排序

    1.numpy库:argsort() argsort函数返回的是数组值从小到大的索引值(升序排列) 一维: In [1]: import numpy as np In [2]: x = np.arra ...

  4. 【LeetCode】017. Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  5. 设计四个线程,其中两个线程每次对j加1,另外两个线程每次对j减1

    public class ManyThreads2 { private int j = 0; public synchronized void inc() { j++; System.out.prin ...

  6. boost::function 通过boost::bind调用类成员函数

    1. 首先引用boost::function和boost::bind的头文件和库: #include "boost/bind.hpp" #include "boost/f ...

  7. TTS API 使用

    #define SPCAT_VOICES           L"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices" ...

  8. VijosP1100:加分二叉树

    描述 设一个n个节点的二叉树tree的中序遍历为(l,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都有一 ...

  9. 常见的post提交数据类型

    原文地址:https://www.cnblogs.com/yoyoketang/p/6771696.html 常见的post提交数据类型有四种: 1.第一种:application/json:这是最常 ...

  10. ubuntu 18.04 设置中文输入法

    有下面几种常用选择: IBus拼音:sudo apt-get install ibus-pinyin IBUS五笔:sudo apt-get install ibus-table-wubi 谷歌拼音输 ...