zoukankan      html  css  js  c++  java
  • codeforces 566D D. Restructuring Company(并查集)

    题目链接:

    D. Restructuring Company

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Even the most successful company can go through a crisis period when you have to make a hard decision — to restructure, discard and merge departments, fire employees and do other unpleasant stuff. Let's consider the following model of a company.

    There are n people working for the Large Software Company. Each person belongs to some department. Initially, each person works on his own project in his own department (thus, each company initially consists of n departments, one person in each).

    However, harsh times have come to the company and the management had to hire a crisis manager who would rebuild the working process in order to boost efficiency. Let's use team(person) to represent a team where person person works. A crisis manager can make decisions of two types:

    1. Merge departments team(x) and team(y) into one large department containing all the employees of team(x) and team(y), where xand y (1 ≤ x, y ≤ n) — are numbers of two of some company employees. If team(x) matches team(y), then nothing happens.
    2. Merge departments team(x), team(x + 1), ..., team(y), where x and y (1 ≤ x ≤ y ≤ n) — the numbers of some two employees of the company.

    At that the crisis manager can sometimes wonder whether employees x and y (1 ≤ x, y ≤ n) work at the same department.

    Help the crisis manager and answer all of his queries.

    Input

    The first line of the input contains two integers n and q (1 ≤ n ≤ 200 000, 1 ≤ q ≤ 500 000) — the number of the employees of the company and the number of queries the crisis manager has.

    Next q lines contain the queries of the crisis manager. Each query looks like type x y, where . If type = 1 or type = 2, then the query represents the decision of a crisis manager about merging departments of the first and second types respectively. Iftype = 3, then your task is to determine whether employees x and y work at the same department. Note that x can be equal to y in the query of any type.

    Output

    For each question of type 3 print "YES" or "NO" (without the quotes), depending on whether the corresponding people work in the same department.

    Examples
    input
    8 6
    3 2 5
    1 2 5
    3 2 5
    2 4 7
    2 1 2
    3 1 7
    output
    NO
    YES
    YES


    题意:

    三种操作,type==1,把x,y合并到一个集合里面;type==2,把x,x+1,x+2...y合并到一个集合里;type==3,query x和y是否在一个集合里;


    思路:

    很显然是并查集,只是type==2是对应的是区间操作,但发现如果是在一个集合里了还要操作就是浪费,可以把已经在一个区间的压缩,用一个pre数组,pre[i]表示数i的前面和i不是一个集合的与i最近的数;这样可以一次合并后下一次直接跳过中间多余的;


    AC代码:

    /*
    2014300227 566D - 25 GNU C++11 Accepted 249 ms 3732 KB

    */
    #include <bits/stdc++.h> using namespace std; const int N=2e5+4; typedef long long ll; int n,p[N],pre[N],q,type,x,y; int findset(int x) { if(x == p[x])return x; return p[x] = findset(p[x]); } int main() { scanf("%d%d",&n,&q); for(int i = 1;i <= n;i++) { p[i] = i; pre[i] = i-1; } while(q--) { scanf("%d%d%d",&type,&x,&y); if(type == 3) { if(findset(x) == findset(y))printf("YES "); else printf("NO "); } else if(type == 1) { int fx = findset(x),fy = findset(y); p[fx] = fy; } else { int r; for(int i = y;i >= x;i = r) { r = pre[i]; if(r<x)break; p[findset(r)] = p[findset(i)]; pre[i] = pre[r]; } } } return 0; }







  • 相关阅读:
    shell编程基础(六): 透彻解析查找命令find
    shell编程基础(五): 正则表达式及其使用
    shell编程基础(三): 位置参数与shell脚本的输入输出
    shell编程基础(二): shell脚本语法之分支语句和循环语句
    shell编程基础(一): 基本变量和基本符号
    SpringCloud学习(一):微服务简介
    ubuntu 安装bazel
    numpy reshape resize用法
    L0、L1及L2范数
    linux常用的搜索命令
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5350501.html
Copyright © 2011-2022 走看看