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

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

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

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. SpringSecurity简单入门

    1.简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spr ...

  2. django框架5

    内容概要 模板语法之过滤器(类似于内置函数) 模板语法之标签(类似于流程控制) 自定义过滤器.标签.inclusion_tag 模板的导入 模板的继承 注释语法补充 前期数据准备(测试环境搭建) al ...

  3. 解决Docker运行命令时提示"Got permission denied while trying to connect to the Docker daemon socket"类情况

    Docker安装命令: 解决Docker运行命令时提示"Got permission denied while trying to connect to the Docker daemon ...

  4. 第6章 字符串(上)——C风格字符串

    6.1 C-strings(C 风格字符串) C风格字符串: 字符数组是元素为字符型的数组,字符串是以空字符'\0' 作为数组最后一个元素的字符数组. 如果指定了数组的大小,而字符串的长度又小于数组大 ...

  5. 一文详解JackSon配置信息

    背景 1.1 问题 Spring Boot 在处理对象的序列化和反序列化时,默认使用框架自带的JackSon配置.使用框架默认的,通常会面临如下问题: Date返回日期格式(建议不使用Date,但老项 ...

  6. WPF开发随笔收录-带递增递减按钮TextBox

    一.前言 今天分享一下如何实现带递增递减按钮的TextBox控件 二.正文 1.之前的博客分享了一篇自定义XamlIcon控件的文章,这次就直接在那个项目的基础上实现今天这个自定义控件 2.首先添加两 ...

  7. Linux文件的通配符

    通配符的作用:匹配文件名 常见的通配符: *:表示任意个字符(不包括隐藏文件) ?:单个任意字符(中文也算一个字符) []:表示匹配一范围或者其中一个 表示匹配范围: [a-z] --- 不但包括了小 ...

  8. 对象映射 - Mapping.Mapster

    前言 在项目中我们会经常遇到对象的映射,比如像Model和Dto之间的映射,或者是对象的深拷贝,这些都是需要我们自己实现的.此时,项目中会出现很多初始化对象的代码,这些代码写起来相当的枯燥乏味,那么有 ...

  9. Oracle数据库控制文件多路复用

    Oracle数据库控制文件多路复用多路复用控制文件,指的是在系统不同的位置上同时存放多个控制文件的副本,此时如果某个路径对应的磁盘发送物理损坏导致该控制文件损坏,就可以通过另一个磁盘上的控制文件进行恢 ...

  10. centos7 ./configure --prefix error checking for C compiler

    解决方法: 输入以下命令 yum -y install gcc gcc-c++ autoconf automake make