Codeforces 566 D. Restructuring Company
Description
一开始有 \(n\) 个元素,可以进行几个操作.
合并 \(x,y\) .
合并 \(x,x+1,...,y\) .
询问 \(x,y\) 是否在一个集合中.
Sol
并查集+链表.
第二个询问,一直用链表找 \(pre\) 优化一下就可以了.
另外 51Nod 1525 重组公司.
Code
#include<cstdio>
#include<utility>
#include<algorithm>
#include<iostream>
using namespace std; const int N = 200005; int n,q;
int f[N],pre[N]; inline int in(int x=0,char ch=getchar()){ while(ch>'9' || ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x; }
int find(int x){ return f[x]==x?x:f[x]=find(f[x]); }
int main(){
n=in();
for(int i=1;i<=n;i++) f[i]=i,pre[i]=i-1;
for(int q=in(),t,x,y,r1,r2;q--;){
t=in(),x=in(),y=in();
switch(t){
case 1:x=find(x),y=find(y),f[y]=x;break;
case 2:
for(r1=find(x),r2=y;y>=x;y=pre[y]) f[find(y)]=r1;
pre[r2]=y;break;
default:if((x=find(x)) == (y=find(y))) puts("YES");
else puts("NO");break;
}
}return 0;
}
Codeforces 566 D. Restructuring Company的更多相关文章
- codeforces 566D D. Restructuring Company(并查集)
题目链接: D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- CodeForces - 566D Restructuring Company 并查集的区间合并
Restructuring Company Even the most successful company can go through a crisis period when you have ...
- 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 ...
- D. Restructuring Company 并查集 + 维护一个区间技巧
http://codeforces.com/contest/566/problem/D D. Restructuring Company time limit per test 2 seconds m ...
- Codeforces 556D Restructuring Company
传送门 D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes input ...
- CodeForces 566D Restructuring Company (并查集+链表)
题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...
- 【VK Cup 2015 - Finals D】Restructuring Company
[题目链接]:http://codeforces.com/problemset/problem/566/D [题意] 给你n个人; 一开始每个人都隶属于一个部门; 之后给你q个操作; 3种操作类型; ...
- 【codeforces 794C】Naming Company
[题目链接]:http://codeforces.com/contest/794/problem/C [题意] 有n个位置; 两个人; 每个人都有n个字符组成的集合s1,s2(可以有重复元素); 然后 ...
- codeforces 794 C. Naming Company(贪心)
题目链接:http://codeforces.com/contest/794/problem/C 题意:有两个人每个人都有一个长度为n的字符串,两人轮流拿出一个字符串,放在一个长度为n的字符串的指定位 ...
随机推荐
- orancle的安装和配置
1.安装 Oracle 版本:Oracle Database 10g Release 2 (10.2.0.1) 下载地址: http://www.oracle.com/technology/softw ...
- VS2013快捷键大全
Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL ...
- osharp3使用经验:整合DbContextScope 文章 1
osharp3的事务处理是跳过savechangeing方法来控制的,没有DbContextScope专业 DbContextScope管理dbcontext的优劣本文不讨论 整合过程: 1.在.Da ...
- ES6箭头函数
1. 无参数 var sum = () => 1 + 2; // 等同于: var sum = function() { return 1 + 2; } 2. 返回单个值 var reflect ...
- python 日志收集系统
服务器端: #!/usr/bin/env python # -*- coding:utf-8 -*- import socket ip_port = ('0.0.0.0',9999) sk = soc ...
- 开源项目管理平台*redmine*的架设
yum -y install ruby yum install rubygems gem install heroku gem install rack -v=1.0.1 gem install ru ...
- Android Studio-设置快速修复错误提示代码
File-Settings-keyMap-show intention actions.
- [CentOs7]安装mysql(2)
摘要 之前安装过一次mysql,最后配置,发现在本地无法连接,重启服务的时候一直卡在那里不动,感觉是安装的过程出问题,最后没办法还是卸载了,然后重新安装一下. [CentOs7]安装mysql Mys ...
- dwz 多选删除
<li><a title="确实要删除这些用户吗?" target="selectedTodo" postType="string& ...
- mapreduce 自定义数据类型的简单的应用
本文以手机流量统计为例: 日志中包含下面字段 现在需要统计手机的上行数据包,下行数据包,上行总流量,下行总流量. 分析:可以以手机号为key 以上4个字段为value传传递数据. 这样则需要自己定义一 ...