题目大意:给你$n$个点,每个点有权值$k$,现有两种操作:

1. $B\;x\;y:$将$x,y$所在联通块合并
2. $Q\;x\;k:$查询第$x$个点所在联通块权值第$k$小是哪个数

题解:线段树合并,权值线段树上二分即可

卡点:

C++ Code:

#include <cstdio>
#include <cctype>
namespace __IO {
namespace R {
int x, ch;
inline int read() {
ch = getchar();
while (isspace(ch)) ch = getchar();
for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);
return x;
}
inline char readc() {
ch = getchar();
while (!isalpha(ch)) ch = getchar();
return ch;
}
}
}
using __IO::R::read;
using __IO::R::readc; #define maxn 100010
#define N (maxn * 50) int n, m;
int w[maxn], ret[maxn];
int rt[maxn], lc[N], rc[N], sz[N], idx; int f[maxn];
int find(int x) {return x == f[x] ? x : (f[x] = find(f[x]));} void insert(int &rt, int l, int r, int num) {
if (!rt) rt = ++idx;
sz[rt]++;
if (l == r) return ;
int mid = l + r >> 1;
if (num <= mid) insert(lc[rt], l, mid, num);
else insert(rc[rt], mid + 1, r, num);
} int __merge(int x, int y) {
if (!x || !y) return x | y;
sz[x] += sz[y];
lc[x] = __merge(lc[x], lc[y]);
rc[x] = __merge(rc[x], rc[y]);
return x;
}
void merge(int a, int b) {
int x = find(a), y = find(b);
if (x == y) return ;
rt[x] = __merge(rt[x], rt[y]);
f[y] = x;
}
int __query(int rt, int l, int r, int k) {
if (sz[rt] < k) return -1;
if (l == r) return l;
int mid = l + r >> 1;
if (sz[lc[rt]] >= k) return __query(lc[rt], l, mid, k);
else return __query(rc[rt], mid + 1, r, k - sz[lc[rt]]);
}
int query(int x, int k) {
x = find(x);
int res = __query(rt[x], 1, n, k);
return ~res ? ret[res] : -1;
} int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) {
w[i] = read(), ret[w[i]] = f[i] = i;
insert(rt[i], 1, n, w[i]);
}
for (int i = 0, a, b; i < m; i++) {
a = read(), b = read();
merge(a, b);
}
int q = read();
while (q --> 0) {
char op = readc();
int x = read(), y = read();
if (op == 'B') merge(x, y);
else printf("%d\n", query(x, y));
}
return 0;
}

  

[洛谷P3224][HNOI2012]永无乡的更多相关文章

  1. 洛谷 P3224 [HNOI2012]永无乡 解题报告

    P3224 [HNOI2012]永无乡 题目描述 永无乡包含 \(n\) 座岛,编号从 \(1\) 到 \(n\) ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 \(n\) 座岛排名,名次用 ...

  2. 洛谷P3224 [HNOI2012]永无乡(线段树合并+并查集)

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

  3. 洛谷 P3224 [HNOI2012]永无乡

    题面 永无乡包含 \(n\) 座岛,编号从 \(1\) 到 \(n\) ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 \(n\) 座岛排名,名次用 \(1\) 到 \(n\) 来表示.某些岛 ...

  4. 2018.08.11 洛谷P3224 [HNOI2012]永无乡(线段树合并)

    传送门 给出n个带点权的点,支持连边和查询连通块第k大. 这个貌似就是一道线段树合并的裸板啊... 代码: #include<bits/stdc++.h> #define N 100005 ...

  5. 【洛谷P3224】永无乡 并查集+Splay启发式合并

    题目大意:给定 N 个点的图,点有点权,初始有一些无向边,现在有 Q 个询问,每个询问支持动态增加一条无向边连接两个不连通的点和查询第 X 个点所在的联通块中权值第 K 大的是哪个点. 题解:学会了平 ...

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

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

  7. 线段树合并+并查集 || BZOJ 2733: [HNOI2012]永无乡 || Luogu P3224 [HNOI2012]永无乡

    题面:P3224 [HNOI2012]永无乡 题解: 随便写写 代码: #include<cstdio> #include<cstring> #include<iostr ...

  8. P3224 [HNOI2012]永无乡 题解

    P3224 [HNOI2012]永无乡 题解 题意概括 有若干集合,每个集合最初包含一个值,和一个编号1~n.两个操作:合并两个集合,查询包含值x的集合中第k大值最初的集合编号. 思路 维护集合之间关 ...

  9. bzoj2733 / P3224 [HNOI2012]永无乡(并查集+线段树合并)

    [HNOI2012]永无乡 每个联通块的点集用动态开点线段树维护 并查集维护图 合并时把线段树也合并就好了. #include<iostream> #include<cstdio&g ...

随机推荐

  1. Ruby 基础教程1-4

    1.对象 数值对象 字符串对象 数组对象,散列对象 正则表达式对象 时间对象 文件对象 符号对象 2.类 Numeric String Array Hash Regexp File Symbol 3. ...

  2. InnoDB意向锁和插入意向锁

      Preface       Last night one buddy in tech wechat group asked "what's intention locks of Inno ...

  3. div布局方案整理

    实际项目开发过程中遇到页面 DIV 左右布局的需求:左侧 DIV 固定宽度,右侧 DIV 自适应宽度,填充满剩余页面,由此引申出本文的几种解决方案 1 左侧 DIV 设置 float 属性为 left ...

  4. 接口测试工具postman(八)上传文件接口

    涉及到选择文件的接口,在[Body]页签下,key选择File选项,会显示“选择文件”按钮,选择本地的文件

  5. Python学习笔记(一)一一一环境安装错误总结

    第三方库安装 1   windows存在多个版本的python,pip安装Python库失败 解决方案:进入对应官网下载安装包,步骤:1 下载安装包到C:\Python36\Lib\site-pack ...

  6. git配置github链接

    1.百度git官网-下载最新版git 2.一路默认下一步安装 3.打开 git bash here 命令行 4.注册github账号(用自己的邮箱就可以,不会英文可以用谷歌翻译)注册成功后建立项目 5 ...

  7. 原生js常用方法

    原生JavaScript设置cookie值 function setCookie(name, value, Hours) { var d = new Date(); var offset = 8; v ...

  8. 关于java中“使用了未经检查或不安全的操作、有关详细信息,请使用 ——Xlint:unchecked重新编译”

    今天看<算法 第4版>排序章节时,发现了一个了一个小问题.先贴一下代码: public class Selection{ public static void main(String[]a ...

  9. 并行程序模拟(Concurrency Simulator, ACM/ICPC World Finals 1991,Uva210)

    任务介绍 你的任务是模拟n个程序的并行运算.(按照输入编号为1~n)的并行执行. 代码实现 #define LOCAL #include<bits/stdc++.h> using name ...

  10. Some good articles

    https://alligator.io/vuejs/introduction-render-functions/ https://alligator.io/vuejs/vue-jwt-pattern ...