题目:

洛谷3224

分析:

这题一看\(n\leq100000\)的范围就知道可以暴力地用\(O(nlogn)\)数据结构乱搞啊……

每个联通块建一棵Splay树,查询就是Splay查询第k大的模板,建桥的时候就把两个联通块的Splay进行“启发式合并”

本来以为启发式合并是什么高端的东西,tzh神犇给我说就是把小的推倒然后暴力插入到大的里面2333

初始我给每个岛都建了一棵Splay树,合并\(a\), \(b\)两岛 (\(size_a<size_b\)) 时将\(a\)树插入到\(b\)树中,然后删除\(a\)树。合并两联通块同理。注意由于除了并查集祖先的树,该联通块中其他的树都已经被删除,所以要用并查集辅助记录哪个岛(\(b\))“代表”这个联通块

建桥时一定要判断两个点是否已经在同一联通块内,合并空树会RE!!!

我SB啊因为这个调一下午

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; namespace zyt
{
const int N = 100010;
int n, m, rank[N];
struct Splay
{
struct node
{
int val, id, size;
node *fa, *s[2];
node(node *const f, const int v, const int i):fa(f), val(v), id(i)
{
s[0] = s[1] = NULL;
size = 1;
}
}*head;
void update(node *rot)
{
rot->size = 1;
if (rot->s[0])
rot->size += rot->s[0]->size;
if (rot->s[1])
rot->size += rot->s[1]->size;
}
bool dir(const node *rot)
{
return rot == rot->fa->s[1];
}
void rotate(node *rot)
{
node *f = rot->fa, *ff = f->fa;
bool d = dir(rot);
rot->fa = ff;
if (ff)
ff->s[dir(f)] = rot;
else
head = rot;
f->s[d] = rot->s[!d];
if (rot->s[!d])
rot->s[!d]->fa = f;
f->fa = rot;
rot->s[!d] = f;
update(f);
update(rot);
}
void splay(node *rot, const node *goal = NULL)
{
while (rot && rot->fa != goal)
{
node *f = rot->fa, *ff = f->fa;
if (ff)
if (dir(rot) ^ dir(f))
rotate(rot), rotate(rot);
else
rotate(f), rotate(rot);
else
rotate(rot);
}
}
void insert(const int id, const int val)
{
node *rot = head;
if (rot == NULL)
head = new node(NULL, val, id);
else
{
while (1)
{
if (val < rot->val)
if (rot->s[0])
rot = rot->s[0];
else
{
rot->s[0] = new node(rot, val, id);
splay(rot->s[0]);
break;
}
else
if (rot->s[1])
rot = rot->s[1];
else
{
rot->s[1] = new node(rot, val, id);
splay(rot->s[1]);
break;
}
}
}
}
int find_kth(const int k)
{
int tmp = k;
node *rot = head;
while(1)
{
if (!rot)
return -1;
if ((!rot->s[0] && tmp == 1) || (rot->s[0] && tmp == rot->s[0]->size + 1))
return rot->id;
if (rot->s[0] && tmp <= rot->s[0]->size)
rot = rot->s[0];
else
{
if (rot->s[0])
tmp -= rot->s[0]->size;
tmp--;
rot = rot->s[1];
}
}
}
friend void merge(Splay &a, Splay &b);
friend void del(node *rot, Splay &d);
}island[N];
inline void merge(Splay &a, Splay &b)
{
if (a.head->size > b.head->size)
swap(a, b);
del(a.head, b);
a.head = NULL;
}
void del(Splay::node *rot, Splay &d)
{
d.insert(rot->id, rot->val);
if (rot->s[0])
del(rot->s[0], d);
if (rot->s[1])
del(rot->s[1], d);
delete rot;
}
int p[N];
int find(const int x)
{
return x == p[x] ? x : p[x] = find(p[x]);
}
void work()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d", &rank[i]);
p[i] = i;
island[i].insert(i, rank[i]);
}
while (m--)
{
int a, b;
scanf("%d%d", &a, &b);
int x = find(a), y = find(b);
if (x == y)
continue;
p[x] = y;
merge(island[x], island[y]);
}
int q;
scanf("%d", &q);
while (q--)
{
char c;
int a, b;
do{c = getchar();} while (c != 'B' && c != 'Q');
scanf("%d%d", &a, &b);
if (c == 'B')
{
int x = find(a), y = find(b);
if (x == y)
continue;
p[x] = y;
merge(island[x], island[y]);
}
else
printf("%d\n", island[find(a)].find_kth(b));
}
}
}
int main()
{
zyt::work();
return 0;
}

【洛谷3224/BZOJ2733】[HNOI2012]永无乡 (Splay启发式合并)的更多相关文章

  1. [BZOJ2733] [HNOI2012] 永无乡 (splay启发式合并)

    Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...

  2. 【BZOJ-2733】永无乡 Splay+启发式合并

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2048  Solved: 1078[Submit][Statu ...

  3. BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]

    2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...

  4. 洛谷.3224.[HNOI2012]永无乡(Splay启发式合并)

    题目链接 查找排名为k的数用平衡树 合并时用启发式合并,把size小的树上的所有节点插入到size大的树中,每个节点最多需要O(logn)时间 并查集维护连通关系即可 O(nlogn*insert t ...

  5. BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)

    不难...treap + 启发式合并 + 并查集 搞搞就行了 --------------------------------------------------------------------- ...

  6. BZOJ2733[HNOI2012]永无乡——线段树合并+并查集+启发式合并

    题目描述 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...

  7. bzoj2733: [HNOI2012]永无乡 线段树合并

    永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛. ...

  8. 【bzoj2733】[HNOI2012]永无乡 Treap启发式合并

    题目描述 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...

  9. 【洛谷 P3224】 [HNOI2012]永无乡(Splay,启发式合并)

    题目链接 启发式合并就是暴力合并把小的合并到大的里,一个一个插进去. 并查集维护连通性,同时保证并查集的根就是所在Splay的根,这样能省去很多操作. #include <cstdio> ...

随机推荐

  1. c# 缓存!

    做项目的时候获取所有城市的时候,发现每次去获取都花费了很多时间,所以用缓存方法让效率更高! 这是我做的例子,如下: public class CacheGetCity { /// <summar ...

  2. 自己写一个HashMap

    package cn.aresoft; /** * HashMap原理 * * @author develp * HashMap是一种以键值对存储数据的数据结构,简单的来说是这样.内部怎么实现的呢?实 ...

  3. [NOI2000] 单词查找树

    ★★   输入文件:trie.in   输出文件:trie.out   简单对比 时间限制:1 s   内存限制:128 MB 在进行文法分析的时候,通常需要检测一个单词是否在我们的单词列表里.为了提 ...

  4. linux命令与技巧

    1.模糊查询:find / -name '*Eclipse*'2.获得管理员权限:sudo -i

  5. Mutual Training for Wannafly Union #6

    A =w= B QvQ C 题意:有长度为n的序列(n<=5e5),求满足条件的a,b,c,d的组数,要求满足条件:min([a,b])<=min([c,d]),a<=b<c& ...

  6. Java获取系统环境变量(System Environment Variable)和系统属性(System Properties)以及启动参数的方法

    系统环境变量(System Environment Variable): 在Linux下使用export $ENV=123指定的值.获取的方式如下: Map<String,String> ...

  7. 使用Vundle管理配置Vim的插件

    1.介绍: 安装需要Git,触发git clone,默认将每一个指定特定格式插件的仓库复制到~/.vim/bundle/. 搜索需要Curl支持. Windows用户请直接访问Windows setu ...

  8. 只用html和css画出不等分圆盘,不用canvas

    <style> .box{height: 200px;width: 200px;border-radius: 100%; overflow: hidden; margin: 200px; ...

  9. jquery simple modal

    窗体API定义丰富,而且使用也很容易上手.官方地址:http://www.ericmmartin.com/projects/simplemodal/从官方下载插件,在文件中引用<script t ...

  10. linux下nginx+svn

    http://fengqi.me/unix/23.html 因为没有什么可以定制的, 所以svn直接使用系统自带的包管理软件安装, 以centos系列为例, 命令如下: yum install sub ...