VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集 set 二分
来源:互联网 发布:手机什么聊天软件好 编辑:程序博客网 时间:2024/05/17 03:56
【题目链接】点击打开链接
【题意】
题意
给出 n 个元素,q 次操作,操作类型有三种
1 x y :将x 和 y 合并到一个集合
2 x y : 将x,x+1,x+2,...,y 合并到一个集合
3 x y : 询问x 和 y 是否处于同一个集合
数据
1 <= n <= 200000,1<=q<=500000
接下来q行每行三个整数 type x y (1 <= x,y <= n),注意在任何一种操作中 x 都有可能等于 y
输入
8 6
3 2 5
1 2 5
3 2 5
2 4 7
2 1 2
3 1 7
输出
NO
YES
YES
【解题方法】单点合并,这个可以用并查集轻松搞定,那么成段合并呢?注意到这个题只有合并,意思就是说我们用set 来搞的话,合并之后删除对应点,那么这样做的复杂度有保证吗?注意到一共有n个点,每个点最多只能进和出set一次,那么这样的复杂度可以保证是O(n * logn)的。那么查询段的位置,我们可以在set里面二分即可。
【AC代码】
////Created by BLUEBUFF 2016/1/9//Copyright (c) 2016 BLUEBUFF.All Rights Reserved//#pragma comment(linker,"/STACK:102400000,102400000")#include <ext/pb_ds/assoc_container.hpp>#include <ext/pb_ds/tree_policy.hpp>#include <ext/pb_ds/hash_policy.hpp>#include <set>#include <map>#include <queue>#include <stack>#include <cmath>#include <cstdio>#include <time.h>#include <cstdlib>#include <cstring>#include <sstream> //isstringstream#include <iostream>#include <algorithm>using namespace std;//using namespace __gnu_pbds;typedef long long LL;typedef pair<int, LL> pp;#define REP1(i, a, b) for(int i = a; i < b; i++)#define REP2(i, a, b) for(int i = a; i <= b; i++)#define REP3(i, a, b) for(int i = a; i >= b; i--)#define CLR(a, b) memset(a, b, sizeof(a))#define MP(x, y) make_pair(x,y)const int maxn = 200010;const int maxm = 2e5;const int maxs = 10;const int maxp = 1e3 + 10;const int INF = 1e9;const int UNF = -1e9;const int mod = 1e9 + 7;//int gcd(int x, int y) {return y == 0 ? x : gcd(y, x % y);}//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;//headset <int> s;set <int>::iterator it1, it2;int fa[maxn];int find_set(int x){ if(x == fa[x]) return x; return fa[x] = find_set(fa[x]);}void union_set(int x, int y){ int fx = find_set(x), fy = find_set(y); if(fx != fy) fa[fx] = fy;}int main(){ int n, q; while(scanf("%d%d", &n, &q) != EOF) { s.clear(); REP2(i, 1, n) fa[i] = i, s.insert(i); while(q--){ int cmd, x, y; scanf("%d%d%d", &cmd, &x, &y); if(cmd == 1){ union_set(x, y); } else if(cmd == 2){ it1 = s.upper_bound(x); while(it1 != s.end() && *it1 <= y){ union_set(x, *it1); it2 = it1++; s.erase(it2); } } else{ if(find_set(x) == find_set(y)){ printf("YES\n"); } else{ printf("NO\n"); } } } } return 0;}
0 0
- VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集 set 二分
- VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集 stl应用
- VK Cup 2015 - Finals, online mirror
- VK Cup 2015 - Finals, online mirror F. Clique in the Divisibility Graph 数论
- VK Cup 2015 - Finals, online mirror F - Clique in the Divisibility Graph dp
- Matching Names(Trie树)-(VK Cup 2015 - Finals, online mirror)
- CodeForces 566 D.Restructuring Company(并查集)
- CodeForces 566 D.Restructuring Company(并查集)
- 【并查集】Codeforces 566D Restructuring Company
- Bubble Cup 8 - Finals [Online Mirror]
- Bubble Cup 9 - Finals [Online Mirror] 题解
- Codeforces Problemset 212D(VK Cup 2012 Finals (unofficial online-version))
- Codeforces Bubble Cup 8 - Finals [Online Mirror] 解题报告
- Bubble Cup 8 - Finals [Online Mirror]C. Party
- VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) B. Work Group 树形dp
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)D. Office Keys(二分+贪心)
- VK Cup 2012 Finals E
- Codeforces 566D Restructuring Company
- iMindMap调整打印设置的方法
- 浅谈广播、组播
- Java线程安全和非线程安全
- 定时自动打开网页并截图
- 学习笔记 03 --- JUC原子类
- VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集 set 二分
- 文章解析1
- 易用的深度学习框架Keras简介
- Java线程安全和非线程安全
- 引号在kibana中的作用
- MDK软件调试的逻辑分析窗口
- [CSAPP] 链接(一)
- 349. Intersection of Two Arrays
- 常用命令