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. Java——表格

    import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; i ...

  2. Ad-Hoc命令不熟悉的选项

    -f #并发线程数,默认5个线程 --private-key #指定秘钥文件 -k #--ask-pass SSH:认证密码 -K, #--ask-sudo-pass sudo:用户的密码(--sud ...

  3. web页面的加载顺序

    1.页面顺序 一个典型的web页面由于三个部分组成:html.css和JS.执行的顺序是: 在构造完HTML的dom结构时.触发DOMContentLoaded事件. 整个执行过程安装html的顺序来 ...

  4. zencart分类页产品页去掉url中的id号

    最近公司新上的网站被seo指出要修改url,去掉url中产品id.由于我们用的是zencart框架,装了 Ultimate SEO URLs 插件,所以在网上应该有这方面的资料,本文主要参考资料: 原 ...

  5. p命名空间的使用(不推荐用)

    xmlns:p="http://www.springframework.org/schema/p" p:没有xsd文件,直接加上面那句就好了 <!-- singleton和p ...

  6. 由Collections.unmodifiableList引发的重构

    原文  http://www.cnblogs.com/persist-confident/p/4516741.html 今天阅读源码的时候,无意中看到了Collections.unmodifiable ...

  7. 使用supervisor监控进程

    在linux下监控进程,可以使用inittab,最近找到了supervisor,也很好用,记录一下:1.系统要安装python,并安装与之对应的setuptools,下载地址在此2.安装:# sh s ...

  8. 导入excel错误:外部表不是预期的格式 解决方案

    环境:win7+iis7+Office2007 在asp.net网站中导出Excel文件后,再把文件导入到数据库中. 读取Excel文件时,打开连接出错. 错误为:外部表不是预期的格式 解决:检查了一 ...

  9. shell学习之路:流程控制(for)

    for循环的语法: 1.  for 变量 in 值1 值2 值3.... do 程序 done 例如:下列脚本会分别打印4次 分别是morning noon afternoon evening的值 # ...

  10. vijos1741 观光公交 (贪心)

    https://www.vijos.org/p/1741 P1741观光公交 请登录后递交 标签:NOIP提高组2011[显示标签]   描述 风景迷人的小城Y市,拥有n个美丽的景点.由于慕名而来的游 ...