题目:

洛谷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. python re模块与正则

    1. re模块 1.1 转义符 正则表达式中的转义符在python的字符串中也刚好有转移的作用,但是正则表达式中的转义符和字符串中的转义符并没关系,且还容易有冲突. 为了避免这种冲突,我们所有的正则都 ...

  2. 3. Python中的分支判断、循环

    本文利用的是Python 3.x版本,建议学习3.x版本 Python中的分支判断.循环 1. 分支条件判断 1.1 比较操作 以下是数之间常见的比较操作,例如5>3就是数学意义上的比较,5是大 ...

  3. 洛谷 3285 [JLOI2014]松鼠的新家

    [题解] 给出一条路径,问树上的点被经过了几次. 显然树剖之后树上差分就好了. #include<cstdio> #include<algorithm> #define N 3 ...

  4. PAT 1137 Final Grading

    For a student taking the online course "Data Structures" on China University MOOC (http:// ...

  5. 在eclipse中如何在大量项目中查找指定文件

    在eclipse中如果希望在大量的项目中寻找指定的文件可不是一件轻松的事,还好eclipse提供了强大的搜索功能. 我们可以通过通配符或正则表达式来设定查寻条件,下面是操作示例: ctrl+h 打开搜 ...

  6. JavaSE 学习笔记之网络编程(二十三)

    端口: 物理端口: 逻辑端口:用于标识进程的逻辑地址,不同进程的标识:有效端口:0~65535,其中0~1024系统使用或保留端口. java 中ip对象:InetAddress. import ja ...

  7. [luoguP2983] [USACO10FEB]购买巧克力Chocolate Buying(贪心)

    传送门 按价格排序后贪心 ——代码 #include <cstdio> #include <iostream> #include <algorithm> int n ...

  8. poj 3164 最小树形图模板!!!

    /* tle十几次,最后发现当i从1开始时,给环赋值时要注意啊! 最小树形图 */ #include<stdio.h> #include<string.h> #include& ...

  9. android安卓程序源码---高仿微信源码

    先截几张图: 部份源代码如下所示: package cn.buaa.myweixin; import java.util.ArrayList; import android.os.Bundle; im ...

  10. The Pilots Brothers' refrigerator DFS+枚举

    Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player ...