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

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

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

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. CF1682C. LIS or Reverse LIS?

    题意:给\(n\)个数,问你能构出严格上升子序列长度和下降子序列长度最小值的最大值. 思路: 如果一个数出现至少两次,ans++. 统计出现一次的个数,因为再最长上升子序列中,只能有一个值能贡献到下降 ...

  2. ES6 - promise(3)

    上一篇熟悉了promise的具体过程: promise的过程: 启动异步任务 => 返回promise对象 =>给promise对象绑定回调函数(甚至可以在异步任务结束后指定多个). 从p ...

  3. 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的"心路(累)历程"

    目录 今天要干嘛? 在哪里干? 从哪里开始干? 快速开干! 解决依赖问题再继续干! 如何干翻重来? 连着 KubeSphere 一起干! 干不过,输了. 重整旗鼓,继续干! 再次重整旗鼓,继续干! 一 ...

  4. Elasticsearch 在地理信息空间索引的探索和演进

    vivo 互联网服务器团队- Shuai Guangying 本文梳理了Elasticsearch对于数值索引实现方案的升级和优化思考,从2015年至今数值索引的方案经历了多个版本的迭代,实现思路从最 ...

  5. JS中通过Input中id获取输入框中的值

    HTML中 <input type="text" id="ONE" oninput="kpi()"> JS中 function ...

  6. NC14247 Xorto

    NC14247 Xorto 题目 题目描述 给定一个长度为 \(n\) 的整数数组,问有多少对互不重叠的非空区间,使得两个区间内的数的异或和为 \(0\) . 输入描述 第一行一个数 \(n\) 表示 ...

  7. mt19937 用法

    老是忘记怎么用,自己写一个用作备忘录吧. 首先需要的头文件: #include <random> 或者是 #include <bits/stdc++.h> //万能头 yyds ...

  8. 大家好,我是UCMP云管家,这是我的自我介绍

    随着云计算的不断普及,构建在计算.存储.网络.数据库等基础资源之上的云平台逐渐大行其道:而随着多种云平台技术路线的发展成熟,多个云厂商的云平台开始出现在企业IT市场.对于企业而言,为满足成本.按需.隐 ...

  9. 在eclipse配置javafx

    JAVA学习中,遇到了这个问题,解决方法记录一下(我用的jdk11) 最新几版的eclipse中没有javafx,需要自己进行手动配置,先下载一下javafx的包,解压,找到lib文件夹(主要用的是这 ...

  10. Error Code: 1054. Unknown column '字段名' in 'field list'

    问题描述: j博主在java开发过程中,通过读取excel中表名和字段名,动态创建insert的SQL语句,在mysql可视化工具中执行此SQL语句时,一直提示"Error Code: 10 ...