zoukankan      html  css  js  c++  java
  • VK Cup 2015

    D. Restructuring Company

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/566/problem/D

    Description

    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 x and 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. If type = 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.

    Sample Input

    8 6
    3 2 5
    1 2 5
    3 2 5
    2 4 7
    2 1 2
    3 1 7

    Sample Output

    NO
    YES
    YES

    HINT

    题意

     有n个人,都在不同的部门,然后有三个操作

    1.合并x,y所在的部门

    2.合并l,l+1....r所在的部门

    3.查询x,y是否在同一个部门

    题解:

    并查集直接维护就好了

    注意维护区间更新的时候,跳着更新就好了

    代码:

    #include<stdio.h>
    #include<iostream>
    using namespace std;
    #define maxn 205000
    int fa[maxn];
    int step[maxn];
    int fin(int x)
    {
        if(fa[x]!=x)
            fa[x]=fin(fa[x]);
        return fa[x];
    }
    int main()
    {
        int n,q;scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
            fa[i]=i,step[i]=i-1;
        for(int i=1;i<=q;i++)
        {
            int op,x,y;
            scanf("%d%d%d",&op,&x,&y);
            if(op==1)
                fa[fin(x)]=fa[fin(y)];
            else if(op==2)
            {
                int t = step[y];
                for(int j=y;j>=x;j=t)
                {
                    t = step[j];
                    fa[fin(j)]=fa[fin(x)];
                    step[j]=step[x];
                }
            }
            else
            {
                if(fin(x)==fin(y))printf("YES
    ");
                else printf("NO
    ");
            }
        }
    }
  • 相关阅读:
    字符串提取数字/汉字/英文字母
    CHARINDEX,PATINDEX,STUFF函数
    raiserror的用法
    数据库备份与还原(通过命令)
    查询某个字段属于哪些表
    设备驱动基础1:设备模型之总线,驱动,设备
    设备模型之kobject,kset及其关系
    模拟电路创新设计
    cdev、udev
    PCB阻抗调节
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4860195.html
Copyright © 2011-2022 走看看