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 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
随机推荐
- 弹窗:popwindow 4部分
弹窗:popwindow 四部分 ①windows.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...
- java ScriptEngine 使用
Java SE 6最引人注目的新功能之一就是内嵌了脚本支持.在默认情况下,Java SE 6只支持JavaScript,但这并不以为着Java SE 6只能支持JavaScript.在Java SE ...
- URAL 1181 Cutting a Painted Polygon【递归+分治】
题目: http://acm.timus.ru/problem.aspx?space=1&num=1181 http://acm.hust.edu.cn/vjudge/contest/view ...
- Xshell调节字体大小和样式
有时候没有看着字体太小的,好难受, 调节字体大小: ALT+P快捷键打开
- php读写csv、xml文件: SimpleExcel
实例结构: 1. csv2xml.demo.php <?php use SimpleExcel\SimpleExcel; // 这句不能少! require_once ('../lib/Simp ...
- PHP计算多少秒/分/时/天/周/月/年之前 : timeago
function timeago( $ptime ) { $etime = time() - $ptime; if ($etime < 59) return '刚刚'; $interval = ...
- "ORA-01012: not logged on"以及"Connected to an idle instance."解决思路
今天测试用的ORACLE服务器出现卡顿情况,于是准备重启一下,在运行shutdown指令关闭数据库的时候意外断开连接,后面想再次进入ORACLE服务器启动时便遇见如下报错: 使用sqlplus /no ...
- LeetCode:颜色分类【75】
LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...
- afinal 文件上传、下载、图片加载实例
// Afinal框架讲解 public class AfinalActivity extends FinalActivity { @ViewInject(id=R.id.bt_afinal_load ...
- 用cocos2d-html5做的消除类游戏《英雄爱消除》(2)——Block设计实现
Block可以说是这个游戏的核心类,它除了包含自身的一些属性和方法外还添加了对触摸事件的响应. 我们先来看下源码吧 /** * Power by html5中文网(html5china.com) * ...