【VK Cup 2015 - Finals D】Restructuring Company
【题目链接】: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的更多相关文章
- 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 ...
- 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 ...
- cf.VK CUP 2015.B.Mean Requests
Mean Requests time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...
- 【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) C】
[链接] 我是链接,点我呀:) [题意] 给你一个字符串s. 让你在其中的某一些位置进行操作.. 把[1..i]和[i+1..n]翻转. 使得里面01交替出现的那种子串的长度最长. [题解] 可以用a ...
- 【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A】 Doggo Recoloring
[链接] 我是链接,点我呀:) [题意] 你可以把出现次数大于1的颜色换成其他颜色. 问你最后能不能全都变成同一种颜色 [题解] 判断一下有没有出现次数大于1的就好. 有的话.显然可以一直用它变颜色. ...
- 【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] [题解] 求出第一个数对的两个数他们有哪些质因子. ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- 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 ...
- 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 ...
随机推荐
- 命令行神器 cmder
下载地址:http://cmder.net/ 修改命令提示符λ为$ 进入解压后的 cmder 的目录,进入 vendor,打开 clink.lua 文件. 修改 local cmder_prompt ...
- 【原创】关于class.forname
连接数据库前都要调用一下class.forname("driverName");然后使用DriverMnager获取连接,这是为什么呢? 首先jdbc标准要求,每个驱动必须向Dri ...
- HDU 1241 Oil Deposits【DFS】
解题思路:第一道DFS的题目--- 参看了紫书和网上的题解-- 在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时, ...
- shell-3.bash的基本功能:输入输出重定向
1. 2. 3. 4.
- 3DS MAX玩家必看!70个提高渲染速度的小技巧
3DS MAX玩家必看!70个提高渲染速度的小技巧 (注:节省RAM不一定会加快渲染速度.请同学们根据实际情况加以利用.) 1. 尽量限制Ploygon数量,越少渲染速度越快 2. 如果使用Vray, ...
- 关于目标检测 Object detection
NO1.目标检测 (分类+定位) 目标检测(Object Detection)是图像分类的延伸,除了分类任务,还要给定多个检测目标的坐标位置. NO2.目标检测的发展 R-CNN是最早基于C ...
- NOIp2018模拟赛四十三
有了昨天的经验,不慌,开题先看source ******** 再看看题,看到C题标题: ******** 有毒... B题的“显然”50分结论推了我一个小时,然后就弃疗了... 成绩:0+50+5=5 ...
- 安装 glusterfs yum源报错
yum install glusterfs-server yum 一直报错 把/etc/yum.repos.d 备份 删除了所有文件,从测试机192..168.59.128上同步过来 一直报错 已加载 ...
- 紫书 例题8-5 UVa11054(等价转换)
这道题用到了等价转换的思想 所有要运到a1的酒, 都要经过a2, 所以不如把a2的值改成a1+a2,然后依次以此类推. #include<cstdio> #include<cmath ...
- [ZJOI2008]骑士(基环树,树形dp)
[ZJOI2008]骑士 题目描述 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的 ...