洞庭青草,近中秋,更无一点风色。玉鉴琼田三万顷,着我扁舟一叶。素月分辉,明河共影,表里俱澄澈。悠然心会,妙处难与君说。

应念岭表经年,孤光自照,肝胆皆冰雪。短发萧骚襟袖冷,稳泛沧溟空阔。尽挹西江,细斟北斗,万象为宾客。扣舷独啸,不知今夕何夕。

权值线段树精巧飘飘有凌云之气,觉动态开点犹有尘心,巨大的空间负荷自轩辕而下,并查集依然不过辅助,岛屿连结之时,是树吗?不如说链的妥契。在遥远的远方,格陵兰岛传说有一片永恒洁白之地,有人探寻过吗?也许有,却因感叹至白之美而潸然以致不堪踏足半分。说来,古来成大事者,要么白得坦荡,有么黑得彻骨,受欺负的永远是老实人。这样看来,踏上那片洁白的也只能是老实人了吧。

codes
# include "bits/stdc++.h"
using namespace std;
constexpr int N = 1e5 + 3;
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
struct node {
int l, r, tot, id;
// tot : due to the orderliness of the nodes of the weighted segment tree, the required ranking of sub tree nodes can be reflected by tot
};
vector<node> t(n * 2 * log2(n)); // it is said on the Internet that the spatial complexity of the dynamic open point weight line segment tree should be O(nlogn * 2)
# define ls t[rt].l
# define rs t[rt].r
# define lson t[rt].l, l, mid
# define rson t[rt].r, mid + 1, r
auto push_up = [&](int rt) -> void {
t[rt].tot = t[ls].tot + t[rs].tot;
};
int tree_index = 0;
auto update = [&](auto update, int &rt, int l, int r, int x, int id) {
if(!rt) rt = ++tree_index;
if(l == r) {
t[rt].id = id;
++t[rt].tot;
return;
}
int mid = l + r >> 1;
if(x <= mid)
update(update, lson, x, id);
else
update(update, rson, x, id);
push_up(rt);
};
vector<int> fa(n + 1);
vector<int> rt(n + 1);
for(int i = 1; i <= n; ++i) {
int x;
cin >> x;
update(update, rt[i], 1, n, x, i);
fa[i] = i;
}
auto Find = [&](auto Find, int u) -> int {
return u == fa[u] ? u : fa[u] = Find(Find, fa[u]);
};
auto merge = [&](auto merge, int x, int y, int l, int r) -> int {
if(!x || !y) return x | y;
// if(l == r) {)
// why (l == r) is impossible ? well, in this problem, the ranking of the islands has been determined, that is to say, it is guaranteed that the two islands will not rank the same
int mid = l + r >> 1;
t[x].l = merge(merge, t[x].l, t[y].l, l, mid);
t[x].r = merge(merge, t[x].r, t[y].r, mid + 1, r);
push_up(x);
return x;
};
while(m--) {
int x, y;
cin >> x >> y;
x = Find(Find, x), y = Find(Find, y);
fa[y] = x;
rt[x] = merge(merge, rt[x], rt[y], 1, n);
}
int q;
cin >> q;
while(q--) {
char ch[9];
cin >> ch;
// char ch;
// for(ch = '!'; ch!= 'Q' && ch != 'B'; ch = getchar());
// cout << ch << endl;
// HINT : there is a strange bug, in the case I use codes above and below, the second line of queries in the simple seems to be ignored. I don't know why
// char ch = getchar();
// while(ch != 'Q' && ch != 'B') ch = getchar();
// cout << ch << endl;
int x, y;
if(ch[0] == 'B') { // represents the construction of a new bridge between island x and island y
cin >> x >> y;
x = Find(Find, x), y = Find(Find, y);
if(x == y) continue;
fa[y] = x;
rt[x] = merge(merge, rt[x], rt[y], 1, n); // the change of root won't infuluence his childs, and of course, the information will be inherited
}
else { // it means to ask which of the islands connected to the island x is the island with the smallest importance ranking y
cin >> x >> y;
x = Find(Find, x);
auto query = [&](auto query, int rt, int l, int r, int k) -> int {
if(!rt || t[rt].tot < k) return 0;
if(l == r) return t[rt].id;
int mid = l + r >> 1;
if(k <= t[ls].tot)
return query(query, lson, k);
else
return query(query, rson, k - t[ls].tot); // if the left weight is less than the target, he will sacrifice to stop the seeker from moving to the right
};
int ans = query(query, rt[x], 1, n, y);
ans == 0 ? cout << "-1\n" : cout << ans << "\n";
}
}
return 0;
}
/*
5 1
4 3 2 5 1
1 2
7
Q 3 2
Q 2 1
B 2 3
B 1 5
Q 2 1
Q 2 4
Q 2 3
*/

luoguP3224 [HNOI2012]永无乡【线段树,并查集】的更多相关文章

  1. BZOJ 2733 [HNOI2012]永无乡 ——线段树 并查集

    用并查集维护联通块. 用线段树的合并来合并联通块. 自己YY了一个写法. #include <map> #include <cmath> #include <queue& ...

  2. [HNOI2012]永无乡 线段树合并

    [HNOI2012]永无乡 LG传送门 线段树合并练手题,写这篇博客只是为了给我的这篇文章找个板子题. 并查集维护连通性,对于不在同一个连通块内的合并操作每次直接合并两颗线段树,复杂度\(O(n \l ...

  3. bzoj 2733: [HNOI2012]永无乡 -- 线段树

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...

  4. Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...

  5. bzoj 2733 : [HNOI2012]永无乡 (线段树合并)

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

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

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

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

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

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

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

  9. 【bzoj2733】[HNOI2012]永无乡 线段树合并

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

  10. 2733: [HNOI2012]永无乡 线段树合并

    题目: https://www.lydsy.com/JudgeOnline/problem.php?id=2733 题解: 建n棵动态开点的权值线段树,然后边用并查集维护连通性,边合并线段树维护第k重 ...

随机推荐

  1. Redis 应用只 消息队列 简单实现(生产者 消费者模式)

    运行效果:

  2. Numpy的一些操作

    1.什么是Numpy 简单来说: Numpy(Numerical Python)是一个开源的Python科学计算库,用于快速处理任意维度的数组. Numpy支持常见的数组和矩阵操作.对于同样的数值计算 ...

  3. js 动画补间 Tween

    1 /* RunningList (触发过程中可以安全的删除自己) 2 如果触发过程中删除(回调函数中删除正在遍历的数组), 不仅 len 没有变(遍历前定义的len没有变, 真实的len随之减少), ...

  4. 逻辑运算符——JavaSE基础

    逻辑运算符 运算符 说明 逻辑与 &( 与) 两个操作数为true,结果才是true,否则是false 逻辑或 |(或) 两个操作数有一个是true,结果就是true 短路与 &&am ...

  5. Lifted ElGamal 门限加密算法

    本文详细学习Lifted ElGamal 门限加密算法 门限加密体制 (1)门限加密是可以抗合谋的 (2)表现在私钥分为\(n\)份,至少需要\(t\)份才能解密成功,叫做(t-n)门限.类似于&qu ...

  6. 第1章 C++绪论

    写于2022年5月13日: 开通博客用于学习记录分享及交流. C++复习笔记内容参考教材[双语版C++程序设计(第2版)][(爱尔兰)Paul Kelly(P. 凯利),苏小红]. 本书的网站:htt ...

  7. 聊聊消息中间件(1),AMQP那些事儿

    开篇 说到消息队列,相信大家并不陌生.大家在日常的工作中其实都有用过.相信大部分的研发在使用消息队列的过程中也仅仅是停留在用上面,里面的知识点掌握得并不是很系统,有部分强大的功能可能由于本身公司的业务 ...

  8. electron-vue 项目启动动态获取配置文件中的后端服务地址

    前言 最近的项目迭代中新增一个需求,需要在electron-vue 项目打包之后,启动exe 可执行程序的时候,动态获取配置文件中的 baseUrl 作为服务端的地址.electron 可以使用 no ...

  9. 解开XAML的邪恶面纱

    什么是XAML,首先我们看下它的外观 <Window x:Class="Blend_WPF.WindowStyle"        xmlns="http://sc ...

  10. Python音频处理基础知识,这不是轻轻松松~~~

    大家好鸭,我是小熊猫 咱今天来讲一讲音频处理的基础知识上才艺~~~ 1.声音的基础 2.python读取.wav音频 欢迎加入白嫖Q群:660193417### import wave import ...