bzoj 2733 永无乡 线段树
题目:
支持两种操作:
- 合并两点所在的联通块
- 查询某点所在联通块内权值第k小.
题解
平衡树启发式合并随便搞一搞就好了。
我写了一个线段树合并
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 100010;
struct Node{
Node *ch[2];
int siz,id;
void update(){
siz = ch[0]->siz + ch[1]->siz;
}
}*null,mem[maxn*30],*root[maxn],*it;
inline void init(){
it = mem;null = it++;
null->ch[0] = null->ch[1] = null;
null->siz = 0;
}
inline Node* newNode(){
Node *p = it++;p->ch[0] = p->ch[1] = null;
p->siz = 0;return p;
}
inline void insert(Node* &p,int l,int r,int pos,int id){
if(p == null) p = newNode();
if(l == r){
p->siz ++ ;
p->id = id;
return ;
}
int mid = l+r >> 1;
if(pos <= mid) insert(p->ch[0],l,mid,pos,id);
else insert(p->ch[1],mid+1,r,pos,id);
p->update();return ;
}
inline Node* Union(Node *x,Node *y){
if(x == null) return y;
if(y == null) return x;
x->ch[0] = Union(x->ch[0],y->ch[0]);
x->ch[1] = Union(x->ch[1],y->ch[1]);
x->update();return x;
}
int n;
inline int query(Node *p,int k){
if(k < 1 || k > p->siz) return -1;
int l = 1,r = n;
while(1){
if(l == r) return p->id;
int mid = l+r >> 1;
if(p->ch[0]->siz >= k){
p = p->ch[0];
r = mid;
}else{
k -= p->ch[0]->siz;
p = p->ch[1];
l = mid+1;
}
}
}
int fa[maxn];
inline int find(int x){
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main(){
init();int m;read(n);read(m);
for(int i=1;i<=n;++i) root[i] = null,fa[i] = i;
for(int i=1,x;i<=n;++i){
read(x);
insert(root[i],1,n,x,i);
}
for(int i=1,u,v;i<=m;++i){
read(u);read(v);
int x = find(u);
int y = find(v);
if(x == y) continue;
fa[x] = y;
root[y] = Union(root[x],root[y]);
}
int q;read(q);
char ch;
int x,k,u,v;
while(q--){
while(ch=getchar(),ch<'!');
if(ch == 'Q'){
read(x);read(k);
int fx = find(x);
printf("%d\n",query(root[fx],k));
}else if(ch == 'B'){
read(u);read(v);
int x = find(u);
int y = find(v);
if(x == y) continue;
fa[x] = y;
root[y] = Union(root[x],root[y]);
}
}
return 0;
}
bzoj 2733 永无乡 线段树的更多相关文章
- bzoj 2733 永无乡 - 并查集 - 线段树
永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛. ...
- bzoj 2733: [HNOI2012]永无乡 -- 线段树
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...
- Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...
- bzoj 2733 : [HNOI2012]永无乡 (线段树合并)
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
- bzoj2733: [HNOI2012]永无乡 线段树合并
永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛. ...
- BZOJ2733[HNOI2012]永无乡——线段树合并+并查集+启发式合并
题目描述 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...
- [HNOI2012]永无乡 线段树合并
[HNOI2012]永无乡 LG传送门 线段树合并练手题,写这篇博客只是为了给我的这篇文章找个板子题. 并查集维护连通性,对于不在同一个连通块内的合并操作每次直接合并两颗线段树,复杂度\(O(n \l ...
- 洛谷P3224 [HNOI2012]永无乡(线段树合并+并查集)
题目描述 永无乡包含 nnn 座岛,编号从 111 到 nnn ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 nnn 座岛排名,名次用 111 到 nnn 来表示.某些岛之间由巨大的桥连接, ...
- 【bzoj2733】[HNOI2012]永无乡 线段树合并
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
随机推荐
- wcf利用IDispatchMessageInspector实现接口监控日志记录和并发限流
一般对于提供出来的接口,虽然知道在哪些业务场景下才会被调用,但是不知道什么时候被调用.调用的频率.接口性能,当出现问题的时候也不容易重现请求:为了追踪这些内容就需要把每次接口的调用信息给完整的记录下来 ...
- POJ 1068 Parencodings【水模拟--数括号】
链接: http://poj.org/problem?id=1068 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...
- PoC简介
无线一键通功能,POC(PTT Over Cellular)也称PTT(Push To Talk)功能.PTT:一键通(Push-to-Talk)功能是一种全新的移动技术,可以快速地进行"一 ...
- linux c编程:popen
我们在执行shell命令比如cat /etc/group | grep root的时候,通过管道的机制将cat /etc/group的结果传递给grep root,然后将结果显示出来 linux中提供 ...
- zip 解压脚本
zip 解压脚本 gpk-unzip.py #!/usr/bin/env python # -*- coding: utf-8 -*- # unzip-gbk.py import os import ...
- Jquery的parent和parents(找到某一特定的祖先元素)用法(转发:https://blog.csdn.net/cui_angel/article/details/7903704)
<!-- parent是指取得一个包含着所有匹配元素的唯一父元素的元素集合. parents则是取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素).可以通过一个可选的表达式进行筛选. ...
- zend 和 esftp插件开发大型PHP项目,ZEND最常用快捷键
先说一下如何安装zend的esftp插件,下载插件esftp-1.1.1.zip,下载地址http://sourceforge.net/projects/esftp/ 或者 http://yun.ba ...
- (转载)C#格式规范
前言 之前工作中整理的一篇编码规范. 代码注释 注释约定 只在需要的地方加注释,不要为显而易见的代码加注释使用 /// 生成的xml标签格式的文档注释 方法注释 所有的方法都应该以描述这段代码的功能的 ...
- Linux:进程管理
Linux:进程管理 进程间通信 文件和记录锁定. 为避免两个进程间同时要求访问同一共享资源而引起访问和操作的混乱,在进程对共享资源进行访问前必须对其进行锁定,该进程访问完后再释放.这是UNIX为共享 ...
- HDU - 2701 Lampyridae Teleportae 【模拟】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2701 题意 有一个萤火虫会闪现 一个人 也会闪现 给出 这个人的起始位置 和他能够闪现的距离 然后依次 ...