luoguP3224 [HNOI2012]永无乡
https://www.luogu.org/problemnew/show/P3224
考虑对每个岛维护一颗平衡树,用并查集维护连通性,启发式合并即可
这东西其实是一个大暴力,每次把节点少的平衡树合并到节点多的平衡树里
这样可以保证每个点合并一次树的大小*2,每个点最多被插入 log(n) 次,复杂度正确
我使用了简单好写的 leafy tree 作为平衡树,不喜勿喷
#include <bits/stdc++.h>
#define update(u) if(u -> left -> size) u -> size = u -> left -> size + u -> right -> size, u -> value = u -> right -> value
#define new_Node(a, b, c, d) (&(*st[cnt++] = Node(a, b, c, d)))
#define merge(a, b) new_Node(a -> size + b -> size, b -> value, a, b)
#define ratio 4
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
template <typename _T>
inline void read(_T &f) {
f = 0; _T fu = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') fu = -1; c = getchar();}
while(c >= '0' && c <= '9') {f = (f << 3) + (f << 1) + (c & 15); c = getchar();}
f *= fu;
}
const int N = 300000 + 10;
struct Node {
int size, value;
Node *left, *right;
Node (int a, int b, Node *c, Node *d) : size(a), value(b), left(c), right(d) {}
Node () {}
}*root[N], *st[N], t[N], *null;
int w[N], v[N], f[N], pre[N], len;
int n, m, q, cnt = 0;
void maintain(Node *u) {
if(u -> left -> size > u -> right -> size * ratio) u -> right = merge(u -> left -> right, u -> right), st[--cnt] = u -> left, u -> left = u -> left -> left;
if(u -> right -> size > u -> left -> size * ratio) u -> left = merge(u -> left, u -> right -> left), st[--cnt] = u -> right, u -> right = u -> right -> right;
}
void ins(Node *u, int x) {
if(u -> size == 1) u -> left = new_Node(1, min(u -> value, x), null, null), u -> right = new_Node(1, max(u -> value, x), null, null);
else ins(x > u -> left -> value ? u -> right : u -> left, x);
update(u); maintain(u);
}
int find(Node *u, int x) {
if(u -> size == 1) return u -> value;
return x > u -> left -> size ? find(u -> right, x - u -> left -> size) : find(u -> left, x);
}
void dfs(Node *u) {
if(u == null) return;
st[--cnt] = u;
dfs(u -> left);
if(u -> size == 1) w[++len] = u -> value;
dfs(u -> right);
}
int Merge(int a, int b) {
if(root[a] -> size < root[b] -> size) swap(a, b);
len = 0; dfs(root[b]);
for(register int i = 1; i < len; i++) {
ins(root[a], w[i]);
}
return a;
}
int find(int x) {
return f[x] == x ? x : f[x] = find(f[x]);
}
int main() {
read(n); read(m);
null = new Node(0, 0, 0, 0);
for(register int i = 0; i <= 300000; i++) st[i] = &t[i];
for(register int i = 1; i <= n; i++) root[i] = new Node(1, INT_MAX, null, null), read(v[i]), ins(root[i], v[i]), f[i] = i, pre[v[i]] = i;
for(register int i = 1; i <= m; i++) {
int a, b;
read(a); read(b);
int x = find(a), y = find(b);
if(x == y) continue;
int fa = Merge(x, y);
f[x] = f[y] = fa;
}
read(q);
for(register int i = 1; i <= q; i++) {
char c = getchar();
while(c != 'Q' && c != 'B') c = getchar();
if(c == 'Q') {
int a, b;
read(a); read(b);
int x = find(a);
if(root[x] -> size - 1 < b) {
printf("-1\n");
continue;
}
printf("%d\n", pre[find(root[x], b)]);
} else {
int a, b;
read(a); read(b);
int x = find(a), y = find(b);
if(x == y) continue;
int fa = Merge(x, y);
f[x] = f[y] = fa;
}
}
return 0;
}
luoguP3224 [HNOI2012]永无乡的更多相关文章
- luoguP3224 [HNOI2012]永无乡【线段树,并查集】
洞庭青草,近中秋,更无一点风色.玉鉴琼田三万顷,着我扁舟一叶.素月分辉,明河共影,表里俱澄澈.悠然心会,妙处难与君说. 应念岭表经年,孤光自照,肝胆皆冰雪.短发萧骚襟袖冷,稳泛沧溟空阔.尽挹西江,细斟 ...
- BZOJ 2733: [HNOI2012]永无乡 启发式合并treap
2733: [HNOI2012]永无乡 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- bzoj 2733: [HNOI2012]永无乡 离线+主席树
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1167 Solved: 607[Submit][Status ...
- BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)
不难...treap + 启发式合并 + 并查集 搞搞就行了 --------------------------------------------------------------------- ...
- BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]
2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...
- B20J_2733_[HNOI2012]永无乡_权值线段树合并
B20J_2733_[HNOI2012]永无乡_权值线段树合并 Description:n座岛,编号从1到n,每座岛都有自己的独一无二的重要度,按照重要度可以将这n座岛排名,名次用1到 n来表示.某些 ...
- 线段树合并+并查集 || BZOJ 2733: [HNOI2012]永无乡 || Luogu P3224 [HNOI2012]永无乡
题面:P3224 [HNOI2012]永无乡 题解: 随便写写 代码: #include<cstdio> #include<cstring> #include<iostr ...
- bzoj2733: [HNOI2012]永无乡 启发式合并
地址:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 题目: 2733: [HNOI2012]永无乡 Time Limit: 10 Sec ...
- [HNOI2012]永无乡 线段树合并
[HNOI2012]永无乡 LG传送门 线段树合并练手题,写这篇博客只是为了给我的这篇文章找个板子题. 并查集维护连通性,对于不在同一个连通块内的合并操作每次直接合并两颗线段树,复杂度\(O(n \l ...
随机推荐
- 小白之js原生轮播图
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- leetcode378
public class Solution { public int KthSmallest(int[,] matrix, int k) { ); ); var list = new List< ...
- Stars(树状数组单点更新)
Astronomers often examine star maps where stars are represented by points on a plane and each star h ...
- cacti启动有图无数据
cactiEZ服务器重启后,获取不到图形的解决办法 cd /var/www/html/cli/ php -q rebuild_poller_cache.php -d myisamchk --safe- ...
- SQL Server 触发器触发器
内容摘抄自http://www.cnblogs.com/hoojo/archive/2011/07/20/2111316.html,只供自己笔记使用 触发器是一种特殊类型的存储过程,它不同于之前的我们 ...
- hibernate 一对一(级联关系)
hibernate 核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hiber ...
- SQL Server 2008 R2 Express 不能启动
今天,新安装了Sql Server 2008 R2 Express,准备部署相应系统,在完成了数据库还原,系统部署以后,从浏览器里输入系统网址,出现登录页面,登录时报错,无法连上数据库.在查找原因的过 ...
- hrabs的数据库session的修改
using System;using System.Data;using System.Collections;using System.Collections.Generic;using Syste ...
- FreeMarker 的空值处理 , 简单理解 , 不用TMD就会忘记
NO.1 而对于FreeMarker来说,null值和不存在的变量是完全一样的 NO.2 ! 指定缺失变量的默认值 返回String NO.3 ?? 判断变量是否存在 返回boolean NO.4 $ ...
- c#并发编程经典实例文摘
第1章 并发编程概述 1.1 并发编程简介 并发: 多线程(包括并行处理) 异步编程(异步操作)程序启动一个操作,而该操作将会在一段时间后完成 响应时编程(异步事件)可以没有一个实际的开始,可以在任何 ...