【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 ...
随机推荐
- jquery根据接口返回的值来设置asp:CheckBoxList的选中值
接口返回一个json的值,然后通过jquery来选中asp:CheckBoxList相应选中的值 <asp:CheckBoxList runat="server" Repea ...
- STM8S汇编代码分析
转载:http://blog.csdn.net/u010093140/article/details/50021897使用STVD建立完汇编工程项目之后(具本建立方法可以看我的另一篇博文http:// ...
- express + jqPaginator 分页展示内容
写在前面的话 分页展示内容也是我们在页面开发中经常会遇到的需求 前端页面利用jqPaginator这个jquery插件来编写 后端利用mysql存储数据 开始敲代码 回顾sql知识 首先让我们回顾一下 ...
- 使用VUE开发微信小程序
使用 mpvue 开发小程序,你将在小程序技术体系的基础上获取到这样一些能力: 彻底的组件化开发能力:提高代码复用性完整的 Vue.js 开发体验方便的 Vuex 数据管理方案:方便构建复杂应用快捷的 ...
- CodeForces-1007A Reorder the Array 贪心 田忌赛马
题目链接:https://cn.vjudge.net/problem/CodeForces-1007A 题意 给个数组,元素的位置可以任意调换 问调换后的元素比此位置上的原元素大的元素个数最大多少 思 ...
- /etc/rsyncd.conf
[root@backup ~]# cat /etc/rsyncd.conf #Rsync server#created by oldboy ##rsyncd.conf start##uid = rsy ...
- 笔记本win2008 r2的hyper-v安装centos
一.i5以上cpu支持虚拟化,不过默认是关闭的,先到bios设置里把虚拟功能打开: 二.“服务器管理器”->“角色”里安装hyper-v并重启: 三.设置无线网络桥接,有线就不需要了,具体如下: ...
- Qt5的插件机制(1)--Qt 框架中的插件载入机制概述
概述 Qt的源代码中通过 Q<pluginType>Factory.Q<pluginType>Plugin 和 Q<pluginType> 这三个类实现了Qt的插件 ...
- iPhone4怎样鉴别翻新机
加入杂志 步骤 1 2 3 4 5 6 由于iPhong4s的不给力,中国内地上市时间又尚未确定,造成近期iPhone4的价格涨了一大截,随之而来的就是大量的翻新机出现在市场上,那么 怎样判断自己手中 ...
- iOS 时间类经常用法
//当前日前日期 NSDate *today = [NSDate date]; //时区 NSTimeZone *zone = [NSTimeZone systemTimeZone]; //设置间隔 ...