```
#include<bits/stdc++.h>
#define N 100005
using namespace std;
int tot=0,fa[N],root[N],n,m,q;
int w[N];
void init(){
for(int i=1;i<=n;i++) fa[i]=i;
}
int get(int x){
return x==fa[x]?x:fa[x]=get(fa[x]);
}
struct SGtree{
int lc,rc;
int sum,id;
}t[3200010];
int newnode(){
t[++tot].lc=t[tot].rc=t[tot].sum=0;
return tot;
}
void push_up(int p){
t[p].sum=t[t[p].lc].sum+t[t[p].rc].sum;
}
void modify(int p,int l,int r,int x,int idx){
if(l==x&&r==x){
t[p].sum++;
t[p].id=idx;
return ;
}
int mid=(l+r)>>1;
if(x<=mid){
if(!t[p].lc) t[p].lc=newnode();
modify(t[p].lc,l,mid,x,idx);
}
else{
if(!t[p].rc) t[p].rc=newnode();
modify(t[p].rc,mid+1,r,x,idx);
}
push_up(p);
}
int query(int p,int l,int r,int k){
if(t[p].sum<k||!p) return -1;
if(l==r) return t[p].id;
int mid=(l+r)>>1;
if(k<=t[t[p].lc].sum) return query(t[p].lc,l,mid,k);
else return query(t[p].rc,mid+1,r,k-t[t[p].lc].sum);
}
int merge(int p,int q,int l,int r){
if(!p) return q;
if(!q) return p;
int mid=(l+r)>>1;
if(l==r){
t[p].sum+=t[q].sum;
return p;
}
t[p].lc=merge(t[p].lc,t[q].lc,l,mid);
t[p].rc=merge(t[p].rc,t[q].rc,mid+1,r);
push_up(p);
return p;
}
int main(){
//freopen("1.txt","w",stdout);
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=n;i++){
scanf("%d",&w[i]);
root[i]=newnode();
modify(root[i],1,n,w[i],i);
}
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
x=get(x),y=get(y);
//cout<<x<<' '<<y<<endl;
fa[y]=x;
root[x]=merge(root[x],root[y],1,n);
//cout<<"qwq"<<' '<<x<<' '<<t[root[x]].sum<<endl;
}
// cout<<get(1)<<endl;
scanf("%d",&q);
while(q--){
char op;
int x,y;
cin>>op;
cin>>x>>y;
if(op=='B'){
x=get(x),y=get(y);
if(x!=y){
fa[y]=x;
root[x]=merge(root[x],root[y],1,n);
}
}
if(op=='Q'){
x=get(x);
printf("%d\n",query(root[x],1,n,y));
}
}
return 0;
}
/*
5 5
1 2 3 4 5
2 3
1 4
2 5
1 3
2 4
10
B 4 5
Q 4 5
*/
```
by chen_qian @ 2021-02-18 16:07:43
哪里有问题?
by chen_qian @ 2021-02-18 16:20:11
@[Sham_Devour](/user/365542) 求教
by chen_qian @ 2021-02-18 16:20:47
但是我现在把输出改成
```
if(op=='Q'){
x=get(x);
int ans=query(root[x],1,n,y);
cout<<ans<<endl;
}
```
AC了?
by chen_qian @ 2021-02-18 16:21:52
@[chen_qian](/user/128870) 你和我一个情况
这个应该是编译器的环境问题啥的
by Prean @ 2021-02-18 16:28:28
@[Prean](/user/160839)
谢谢
by chen_qian @ 2021-02-18 16:37:19