【题目链接】: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. asp.net 连接字符串的多种写法

    一.使用OleDbConnection对象连接OLE DB数据源 1.连接Access 数据库 Access 2000: “provider=Microsoft.Jet.Oledb.3.5;Data ...

  2. [arc067f]yakiniku restaurants

    题意: n家饭店,m张餐票,第i家和第i+1家饭店之间的距离是$A_i$,在第i家饭店用掉第j张餐票会获得$B_{i,j}$的好感度,但是从饭店i走到饭店j会有$dis_{i,j}$的代价,可以从任意 ...

  3. js递归获取html页面所有标签

    js原生递归获取,直接源码 : <script> var child = document.children; var arr = [];//用来存放获取到的所有的标签 function ...

  4. 【jQuery04】折叠树

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 【Computer Vision】图像单应性变换/投影/仿射/透视

    一.基础概念 1. projective transformation  = homography = collineation. 2. 齐次坐标:使用N+1维坐标来表示N维坐标,例如在2D笛卡尔坐标 ...

  6. 【Computer Vision】角点检测和匹配——Harris算子

    一.基本概念 角点corner:可以将角点看做两个边缘的交叉处,在两个方向上都有较大的变化.具体可由下图中分辨出来: 兴趣点interest point:兴趣点是图像中能够较鲁棒的检测出来的点,它不仅 ...

  7. apache源码编译安装

    源码安装apche 下载apache的源码包文件 访问http://mirror.bit.edu.cn/apache/httpd/,复制如下gz文件的链接地址,并使用wget下载到本地 wget -P ...

  8. PHP 变量作用域

    以下为 PHP 中的各种变量在底层实现中是如何存储的. 变量: $temp = 'temp'; $temp2 = $temp; // key p *executor_globals.symbol_ta ...

  9. 转:让MySQL支持emoji表情

    转自:http://www.cnblogs.com/suifu/p/5848269.html 公司有新要求,ios客户端要上线评论中可以使用emoji表情的功能,在mysql 5.5 之前,UTF-8 ...

  10. 在Windows上面安装多个Memcached

    在Windows上面安装多个Memcached sc create "memcached Server3" start= auto binPath= "D:\memcac ...