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. 爬虫2 url管理器 url_manager.py

    #coding:utf8 class UrlManager(object): def __init__(self): self.new_urls = set() self.old_urls = set ...

  2. swiper 技巧

    全屏广告高度 ,加上属性. html, body { position: relative; height: 100%; } 停止自行播放 mySwiper.stopAutoplay(); 锁住状态, ...

  3. Google

    1. Google Play: Google Play是谷歌官方的的应用市场, Google Play 服务通常会在 Android 装置上自动更新. http://baike.baidu.com/l ...

  4. Robot Framework--02 菜单栏&工具栏

    转自:http://blog.csdn.net/tulituqi/article/details/7584795 我把RIDE的界面大致分了四个区域:菜单栏.工具栏.案例及资源区.工作区,如下图 菜单 ...

  5. mysql 索引及其原理

    mysql 索引 KEY与INDEX的区别: KEY is something on the logical level, describes your table and database desi ...

  6. RabbitMQ 用户角色详解

    RabbitMQ的用户角色分类:none.management.policymaker.monitoring.administrator RabbitMQ各类角色描述:none不能访问 managem ...

  7. html、css杂记

    1:浮动 <div style="float: left"> 2:清除浮动,把父div撑起来 <div style="clear:both"& ...

  8. hdu4888 Redraw Beautiful Drawings 最大流+判环

    hdu4888 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/6553 ...

  9. TTFB-首字节时间简介

    百度站长工具里看到有一个"首字节时间"的建议,第一次听说,还真不知道是什么东东.百度站长工具里面的解释是:"浏览器开始收到服务器响应数据的时间=后台处理时间+重定向时间, ...

  10. mysql 删除重复数据,并保存最新一条数据

    删除重复行 DELETE FROM ecm_member_login_session WHERE (number , client_code) IN ( ) AND update_time NOT I ...