题目:

支持两种操作:

  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;
}

最新文章

  1. [Unity游戏开发]向量在游戏开发中的应用(一)
  2. Spring系列之依赖注入的方式
  3. sql 删除表格delete drop truncate 区别(转)
  4. Hadoop.2.x_简单的测试文件读取与上传
  5. LR监控Windows资源
  6. Hibernate中的PO
  7. mysql存储过程和函数使用实例
  8. MVC中使用AuthorizeAttribute做身份验证操作
  9. HDOJ_1010 Tempter of the Bone
  10. java 信号量Semaphore
  11. Cocos Creator 资源加载流程剖析【一】——cc.loader与加载管线
  12. QT学习之如何在QToolBar中添加带图标的QToolButton并设置图标大小
  13. Mybatis的updateByExampleSelective方法
  14. C++ openmp并行程序在多核linux上如何最大化使用cpu
  15. #define用法之一
  16. jquery预加载的几种方式
  17. git cherry-pick 报错 fatal: bad object
  18. Oracle RAC时间同步(NTP/CTSS)
  19. dedecms 5.7sp2在用type标签时出现调用无效问题
  20. wpf-典型的mvvm模式通用中小型管理系统框架0

热门文章

  1. poj1135
  2. 为什么Git 比 SVN 好
  3. cocos2d-x3.6 生成带类图的离线文档
  4. python基础12 ---函数模块2
  5. 阿里云 rails nginx 配置https访问
  6. gulp 打包报错:ReferenceError: internalBinding is not defined
  7. maven导入项目时,缺少部分source folder
  8. Data Structure Stack: Infix to Postfix
  9. Linuxshell资料汇总
  10. java-从这里开始认识