题目:

支持两种操作:

  1. 合并两点所在的联通块
  2. 查询某点所在联通块内权值第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 永无乡 线段树的更多相关文章

  1. bzoj 2733 永无乡 - 并查集 - 线段树

    永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛. ...

  2. bzoj 2733: [HNOI2012]永无乡 -- 线段树

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...

  3. Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...

  4. bzoj 2733 : [HNOI2012]永无乡 (线段树合并)

    Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...

  5. bzoj2733: [HNOI2012]永无乡 线段树合并

    永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛. ...

  6. BZOJ2733[HNOI2012]永无乡——线段树合并+并查集+启发式合并

    题目描述 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...

  7. [HNOI2012]永无乡 线段树合并

    [HNOI2012]永无乡 LG传送门 线段树合并练手题,写这篇博客只是为了给我的这篇文章找个板子题. 并查集维护连通性,对于不在同一个连通块内的合并操作每次直接合并两颗线段树,复杂度\(O(n \l ...

  8. 洛谷P3224 [HNOI2012]永无乡(线段树合并+并查集)

    题目描述 永无乡包含 nnn 座岛,编号从 111 到 nnn ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 nnn 座岛排名,名次用 111 到 nnn 来表示.某些岛之间由巨大的桥连接, ...

  9. 【bzoj2733】[HNOI2012]永无乡 线段树合并

    Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...

随机推荐

  1. 解决不同浏览器创建不同 XMLHTTP 对象的问题

    function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XML ...

  2. 【BZOJ2337】[HNOI2011]XOR和路径 期望DP+高斯消元

    [BZOJ2337][HNOI2011]XOR和路径 Description 题解:异或的期望不好搞?我们考虑按位拆分一下. 我们设f[i]表示到达i后,还要走过的路径在当前位上的异或值得期望是多少( ...

  3. Android jni 编程(参数的传递,成员,方法的)相互访问

    package com.test.androidjni; import android.app.Activity; import android.os.Bundle; import android.u ...

  4. 配置过滤器filter对跨站脚本攻击XSS实现拦截

    1.web.xml中配置filter [html] view plain copy   <filter> <filter-name></filter-name> & ...

  5. PAT 1051. 复数乘法 (15)

    复数可以写成(A + Bi)的常规形式,其中A是实部,B是虚部,i是虚数单位,满足i2 = -1:也可以写成极坐标下的指数形式(R*e(Pi)),其中R是复数模,P是辐角,i是虚数单位,其等价于三角形 ...

  6. pip-grep

    Pip-pop pip-grep主要是用于方便查看Requirements.txt中那些模块是安装了的.也就是通过输入的然后模块名称然后在Requirements.txt中进行查询.里面比较难的就是d ...

  7. activiti--6-------------------------------------连线(一般数据库表的查询顺序)

    一.流程图 二.这次把流程图和Java类放在一个包下 三.代码 package com.xingshang.f_sequenceFlow; import java.io.InputStream; im ...

  8. python基础18 ---多态与绑定方法

    一.抽象类 1.抽象类的定义:从一堆类中抽象出相同的内容,重新组成一个新的类,这样的类属于抽象类. 2.香蕉类是一类水果,苹果类是一类水果,葡萄类是一类水果,但是他们都属于水果,从他们这些类中可以抽象 ...

  9. iOS UIImage UIImageView 展示图片 不变形 处理

    展示图片 时候 固定 了 imageView  的大小  图片 也裁剪了 尽量保持比例 可是 还是失真 变形了 这张图 ui 要求展示的UIimageView 大小 是固定 的  ,传过来的 图片 是 ...

  10. LintCode:链表操作(合并与反转)

    描述: (1)翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null ********************** ...