【题目链接】:http://codeforces.com/problemset/problem/566/D

【题意】



给你n个人;

一开始每个人都隶属于一个部门;

之后给你q个操作;

3种操作类型;

1.把x和y所在的部门的所有人都并在一个新的部门

2.把x..y这个区间范围里面的人所在的部门的所有人都并在一个部门;

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

【题解】



并查集;

这里把x..y并在一起;

相当于有y-x个合并操作

①for (int i = x+1;i<=y;i++) merge(i-1,i);

而且显然这个合并操作是不可逆的,合并了就不会改变

这样我们就能找到节省时间的策略;

即如果下次又进行了这个2操作;

且又到了x这个人,则我们可以跳过x+1..y这一段人的①操作

且进行过①操作之后

无论进行到x+1..y中的哪一个人,下一个进行merge(i,i-1)的都可以直接跳到y+1那个人;

用一个nex数组记录某个人合并完之后要跳到哪一个操作就好;

这样就能解决这个2操作了;

而对于1操作,直接合并就是了;



【Number Of WA】



1



【反思】



cin,cout是真的慢…

1个WA是判断写错了。。

下次测试的时候要把所有的操作都试一遍。

尝试从暴力算法中找到优化的方案。



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e5; int n,q,f[N+100],nex[N+100]; int ff(int x){
if (f[x]==x)
return x;
else
return f[x] = ff(f[x]);
} void hebing(int x,int y){
for (int i = x+1;i <= y; ){
int r1 = ff(i),r2 = ff(i-1);
if (r1!=r2){
f[r1] = r2;
}
int temp = nex[i];
nex[i] = nex[y];
i = temp;
}
} int main(){
//Open();
Close();
cin >> n >> q;
rep1(i,1,n)
f[i] = i,nex[i] = i + 1;
rep1(i,1,q){
int x,y,z;
cin >> x >> y >> z;
if (x == 3){
if (ff(y)!= ff(z)){
cout << "NO" << endl;
}else{
cout << "YES" << endl;
}
}else if (x==1){
int r1 = ff(y),r2 = ff(z);
if (r1!=r2)
f[r1] = r2;
}else
hebing(y,z);
}
return 0;
}

【VK Cup 2015 - Finals D】Restructuring Company的更多相关文章

  1. 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 ...

  2. Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!

    VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...

  3. cf.VK CUP 2015.B.Mean Requests

    Mean Requests time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...

  4. 【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) C】

    [链接] 我是链接,点我呀:) [题意] 给你一个字符串s. 让你在其中的某一些位置进行操作.. 把[1..i]和[i+1..n]翻转. 使得里面01交替出现的那种子串的长度最长. [题解] 可以用a ...

  5. 【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A】 Doggo Recoloring

    [链接] 我是链接,点我呀:) [题意] 你可以把出现次数大于1的颜色换成其他颜色. 问你最后能不能全都变成同一种颜色 [题解] 判断一下有没有出现次数大于1的就好. 有的话.显然可以一直用它变颜色. ...

  6. 【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) B】Weakened Common Divisor

    [链接] 我是链接,点我呀:) [题意] 给你n个数对(ai,bi). 让你求一个大于1的数字x 使得对于任意的i x|a[i] 或者 x|b[i] [题解] 求出第一个数对的两个数他们有哪些质因子. ...

  7. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

  8. cf.VK CUP 2015.C.Name Quest(贪心)

    Name Quest time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  9. VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) E. Correcting Mistakes 水题

    E. Correcting Mistakes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

随机推荐

  1. 3dmax实例教程-使用3ds Max 创建一个完整的场景

    本篇教程讲述了利用3ds max创建一个完整的场景. 灵感来源:当我在遇到一些事情睡不着觉的时候我便在努力想象一些别的事情,于是我便想到了这个场景,其实对于我的这个角色我即没有参考图也没有草稿图,有的 ...

  2. IPv6第二层寻址,IPv6接口要求

    1. IPv6第二层寻址 IPV6地址以两种方式与第2层地址相关.第一种方式是IPV6独有的,提供了从第2层地址构建接口ID的机制.第二种方式对IPv4和IPV6都是一样的,提供了将一个IP组播地址映 ...

  3. 解决wps的ppt演示不能打开的问题libbz2.so.1.0

      安装 wps-office-10.1.0.5707-1.a21.x86_64 无法打开ppt 其他正常

  4. 紫书 习题8-4 UVa 11491 (贪心)

    题意:给你一个数, 要求删去一些数字, 使得剩下的数字最大. 这道题用贪心解决. 大家想一想, 两个数比较大小, 肯定先比较第一位的数,然后依次比较第二位,以此类推. 既然我们要保证最后的数字最大, ...

  5. solr环境搭建&基本使用

    分步指南 solr服务与tomcat整合 solr使用配置步骤 solr使用 推荐分词工具 相关的文章 一.Solr服务与tomcat整合 1.solr相关版本下载路径:http://archive. ...

  6. HDU 1827 Summer Holiday(强连通)

    HDU 1827 Summer Holiday 题目链接 题意:中文题 思路:强连通缩点,每一个点的权值为强连通中最小值,然后入度为0的点就是答案 代码: #include <cstdio> ...

  7. SCN 时间戳的相互转换

    SQL> select * from v$version where rownum=1; BANNER --------------------------------------------- ...

  8. [Transducer] Create a Sequence Helper to Transduce Without Changing Collection Types

    A frequent use case when transducing is to apply a transformation to items without changing the type ...

  9. windows环境利用apache 配置虚拟主机

    windows环境利用apache 配置虚拟主机 1.改动http.host #LoadModule vhost_alias_module modules/mod_vhost_alias.so #In ...

  10. NSTimer解除循环引用

    NSTimer作为一个经常使用的类,却有一个最大的弊病,就是会强引用target.造成调用timer很麻烦.稍有不慎就造成内存泄漏. 下面就是为解决问题做的封装. 直接上代码: #import < ...