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

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

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

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. 深入浅出Nginx实战与架构

    本文主要内容如下(让读者朋友们深入浅出地理解Nginx,有代码有示例有图): 1.Nginx是什么? 2.Nginx具有哪些功能? 3.Nginx的应用场景有哪些? 4.Nginx的衍生生态有哪些? ...

  2. 基于Web的CAD一张图协同在线制图更新轻量级解决方案[示例已开源]

    背景 之前相关的博文中介绍了如果在Web网页端展示CAD图形(唯杰地图云端图纸管理平台 https://vjmap.com/app/cloud),有不少朋友问,能不能实现一个协同的功能,实现不同部门不 ...

  3. 优秀开源平台,前后端分离快速开发平台,一站式多端开发(PC+APP)

    JNPF平台架构介绍 JNPF快速开发平台采用前后端分离技术.采用B/S架构开发,形成一站式开发多端(APP+PC)使用. PC端版本介绍 第一个当然是当下热门的.net core了,运行环境为Vis ...

  4. Go微服务框架go-kratos实战05:分布式链路追踪 OpenTelemetry 使用

    一.分布式链路追踪发展简介 1.1 分布式链路追踪介绍 关于分布式链路追踪的介绍,可以查看我前面的文章 微服务架构学习与思考(09):分布式链路追踪系统-dapper论文学习(https://www. ...

  5. CabloyJS 基于 EggJS 实现的模块编译与发布

    背景 现在,EggJS被许多开发团队所采用.有的团队基于商业知识产权的考量,往往会提一个问题:是否可以把EggJS当中的代码编译打包,然后再把代码丑化? 模块编译的机制 EggJS为何不能便利的实现编 ...

  6. DevOps落地实践点滴和踩坑记录-(1)

    记录初衷 本人一直在从事企业内DevOps落地实践的工作,走了不少弯路,也努力在想办法解决面临的问题,期间也经历过不少人和事情,最近突然有想法把经历过的,不管好的不好的都记录下来,分享给和我一样的一线 ...

  7. 一个ES设置操作引发的“血案”

    背景说明 ES版本 7.1.4 在ES生产环境中增加字段,一直提示Setting index.mapper.dynamic was removed after version 6.0.0错误.但是我只 ...

  8. Spring框架系列(2) - Spring简单例子引入Spring要点

    上文中我们简单介绍了Spring和Spring Framework的组件,那么这些Spring Framework组件是如何配合工作的呢?本文主要承接上文,向你展示Spring Framework组件 ...

  9. Kali2019渗透环境配置

    一.系统安装 二.基础配置 # 配置源 vim /etc/apt/sources.list # kali官方源 deb http://http.kali.org/ kali-rolling main ...

  10. FastASR——PaddleSpeech的C++实现

    FastASR 基于PaddleSpeech所使用的conformer模型,使用C++的高效实现模型推理,在树莓派4B等ARM平台运行也可流畅运行. 项目简介 本项目仅实现了PaddleSpeech ...