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的更多相关文章

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

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

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

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

  3. 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 ...

  4. D. Restructuring Company 并查集 + 维护一个区间技巧

    http://codeforces.com/contest/566/problem/D D. Restructuring Company time limit per test 2 seconds m ...

  5. Codeforces 556D Restructuring Company

    传送门 D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes input ...

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

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

  7. 【VK Cup 2015 - Finals D】Restructuring Company

    [题目链接]:http://codeforces.com/problemset/problem/566/D [题意] 给你n个人; 一开始每个人都隶属于一个部门; 之后给你q个操作; 3种操作类型; ...

  8. 【codeforces 794C】Naming Company

    [题目链接]:http://codeforces.com/contest/794/problem/C [题意] 有n个位置; 两个人; 每个人都有n个字符组成的集合s1,s2(可以有重复元素); 然后 ...

  9. codeforces 794 C. Naming Company(贪心)

    题目链接:http://codeforces.com/contest/794/problem/C 题意:有两个人每个人都有一个长度为n的字符串,两人轮流拿出一个字符串,放在一个长度为n的字符串的指定位 ...

随机推荐

  1. Jquery 学习之基础一

    1.添加一个CSS类 $("button").click(function(){  $("#div1").addClass("important bl ...

  2. MySQL学习笔记——存储过程

  3. Tomcat配置并启用HTTPS

    参考文献:http://www.cnblogs.com/xdp-gacl/p/3744053.html#blogTitle2 概述:用sun公司提供的keytool(位置为<JAVA_HOME& ...

  4. lightbox图片展示效果

    1.lightbox 头部导入: <script type="text/javascript" src="../Public/Js/prototype.js&quo ...

  5. JQuery.getJSON 从aspx页面返回JSON数据

    public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventAr ...

  6. ASP.NET技巧:教你制做Web实时进度条

    网上已经有很多Web进度条的例子,但是很多都是估算时间,不能正真反应任务的真实进度.我自己结合多线程和ShowModalDialog制做了 一个实时进度条,原理很简单:使用线程开始长时间的任务,定义一 ...

  7. Kafka——分布式消息系统

    Kafka——分布式消息系统 架构 Apache Kafka是2010年12月份开源的项目,采用scala语言编写,使用了多种效率优化机制,整体架构比较新颖(push/pull),更适合异构集群. 设 ...

  8. Python之路【第八篇】python实现线程池

    线程池概念 什么是线程池?诸如web服务器.数据库服务器.文件服务器和邮件服务器等许多服务器应用都面向处理来自某些远程来源的大量短小的任务.构建服务器应用程序的一个过于简单的模型是:每当一个请求到达就 ...

  9. Objective-C 链式语法的实现

    对于 Objective-C 的语法,喜欢的人会觉得它是如此的优雅,代码可读性强,接近自然语言,开发者在调用大多数方法时不需要去查看注释或文档,通常只凭借方法名就可以大致知道这个方法的作用,可以理解为 ...

  10. thinkphp-许愿墙-2

    在数组中,也可以使用函数,如: $data = array( 'username'=> I('username','', 'htmlspecailchars'), 'content'=> ...