题意:给定 3 种操作,

第一种 1 u v 把 u 和 v 合并

第二种 2 l r 把 l - r 这一段区间合并

第三种 3 u v 判断 u 和 v 是不是在同一集合中。

析:很容易知道是用并查集来做,但是如果单纯的用并查集,肯定是要超时的,所以要用链表,如果合并了,就把链表指向,

这样就搞定了这个题。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int p[maxn], nxt[maxn]; int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } int main(){
scanf("%d %d", &n, &m);
for(int i = 0; i <= n; ++i) p[i] = i, nxt[i] = i + 1;
while(m--){
int op, u, v;
scanf("%d %d %d", &op, &u, &v);
if(op == 1){
int x = Find(u);
int y = Find(v);
if(x != y) p[y] = x;
}
else if(op == 2){
int x = Find(v);
while(true){
if(u > v) break;
int y = Find(u);
if(x != y) p[y] = x;
int t = u;
u = nxt[u];
nxt[t] = v+1;
}
}
else printf("%s\n", Find(u) == Find(v) ? "YES" : "NO");
}
return 0;
}

  

CodeForces 566D Restructuring Company (并查集+链表)的更多相关文章

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

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

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

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

  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 #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  6. Codeforces 556D Restructuring Company

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

  7. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

  8. 【CF878C】Tournament set+并查集+链表

    [CF878C]Tournament 题意:有k个项目,n个运动员,第i个运动员的第j个项目的能力值为aij.一场比赛可以通过如下方式进行: 每次选出2个人和一个项目,该项目能力值高者获胜,败者被淘汰 ...

  9. Codeforces 859E Desk Disorder 并查集找环,乘法原理

    题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...

随机推荐

  1. 观后感|当幸福来敲门 The Pursuit of Happyness

    更好的阅读体验请点击:当幸福来敲门 The Pursuit of Happyness 看到时光机点亮的那一刻,我想儿子克里斯托夫正在侏罗纪的世界内探险,看着山川河流,穿梭在恐龙的脚下,在山洞中安稳的度 ...

  2. Process使用

    最近在一个项目中,需要在C#中调用cmd窗口来执行一个命令,使用到了Process这个类,使用过程中遇到不少问题,好在终于解决了.赶紧记录下来. Process process = new Proce ...

  3. [IIS] 不能加载类型System.ServiceModel.Activation.HttpModule

    Could not load type ‘System.ServiceModel.Activation.HttpModule’ from assembly ‘System.ServiceModel, ...

  4. linux中普通用户修改密码出现(passwd:Authentication token manipulation error)

    如果在linux中,不管是root用户还是普通用户登录后,修改自己的密码,出现---passwd:Authentication token manipulation error---错误的解决办法: ...

  5. Network(lca暴力)

    Network Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submi ...

  6. Spring学习八

    1: Tomcat容器四个等级? Container, Engine,  Servlet容器, Context 真正管理Servlet的容器是Context容器:一个context对应一个web工程. ...

  7. json例子--bai

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. web 应用 及 补充

    Highcharts 绘图配置 的函数及参数 web页面文本框修饰器 --- KindEditor web页面 之 超人性的点赞与狂踩 web页面 之 图片上传 web页面 之 评论盖楼 jQuery ...

  9. appium python版api

    打印上下文 driver.contexts 打印当前上下文 driver.context driver.current_context 切换上下文 driver.switch_to.context(' ...

  10. python window使用paramiko简单监控数据指标数据采集

    #!/usr/bin/python #-*- coding: utf-8 -*- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...