【题目链接】: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. (转载)ListView与ScrollView冲突的4种解决方案

    问题解决方案1.手动设置ListView高度    经过测试发现,在xml中直接指定ListView的高度,是可以解决这个问题的,但是ListView中的数据是可变的,实际高度还需要实际测量.于是手动 ...

  2. (转载)android控件之WebView控件缩小

    android控件之WebView控件缩小 作者: 字体:[增加 减小] 类型:转载 时间:2013-05-16我要评论 发现这个控件挺好用,能自已控制进度条,而且这个控件的功能非常壮大,先上个简单的 ...

  3. 使用xshell连接本地虚拟机中的Linux问题

    xshell 连接虚拟机中Linux报错: Could not connect to '192.168.8.120' (port 22):Connection failed. 原因:虚拟机中Linux ...

  4. 【摘录】JDBC Master Slave(JDBC方式的JMS集群)

    JDBC Master Slave First supported in ActiveMQ version 4.1 If you are using pure JDBC and not using t ...

  5. swift语言点评十三-Lazy

    Lazy Stored Properties A lazy stored property is a property whose initial value is not calculated un ...

  6. pthread_cleanup_push vs Autorelease VS 异常处理

    黑幕背后的Autorelease http://www.cnblogs.com/feng9exe/p/7239552.html objc_autoreleasePoolPush的返回值正是这个哨兵对象 ...

  7. SpringBoot学习笔记(15)----SpringBoot使用Druid

    直接访问Druid官网wiki,有详细教程,地址如下: SpringBoot支持Druid地址:https://github.com/alibaba/druid/tree/master/druid-s ...

  8. 前端那些事之----jQuery

    1.jquery是什么     一个js的框架,可以方便的使用js 2 什么是jQuery对象     是由jQuery封装后的DOM对象     注意:与DOM对象的方法不同,不可以混用,但是可以相 ...

  9. [洛谷P1892][codevs2597]团伙

    题目大意:有n个强盗,他们有这样的关系:1.朋友的朋友是朋友:2.敌人的敌人是朋友. 两个人是朋友,则他们在一个团伙中,是敌人则在不同团伙中. 现在给出一些朋友或敌人的关系,问最多有多少团伙.输入保证 ...

  10. 单机Mongo复制集安装配置(数据库版本:4.x)

      官方文档: https://docs.mongodb.com/manual/tutorial/deploy-replica-set-with-keyfile-access-control/#dep ...