有没有大佬可以救救
不明原因WA了
做法是 建n棵平衡树,每次加边合并那两棵树
玄关
#include<bits/stdc++.h>
using namespace std;
const int MX=5e5+10;
struct node{
int l,r;
int key,val;
int siz,fa;
}fhq[MX];
mt19937 rnd(114);
int cnt=0,root[MX]={0};
inline int new_node(int val){
node &i=fhq[++cnt];
i.key=rnd();i.val=val;i.siz=1;
return cnt;
}
inline void update(int now){
fhq[now].siz=fhq[fhq[now].l].siz+fhq[fhq[now].r].siz+1;
if(fhq[now].l) fhq[fhq[now].l].fa=now;
if(fhq[now].r) fhq[fhq[now].r].fa=now;
}
void split(int now,int val,int &x,int &y){
if(!now) x=y=0;
else{
if(fhq[now].val<=val){
x=now;
split(fhq[now].r,val,fhq[now].r,y);
}
else{
y=now;
split(fhq[now].l,val,x,fhq[now].l);
}
update(now);
}
}
int merge(int x,int y){
if(!x||!y) return x+y;
if(fhq[x].key>fhq[y].key){
fhq[x].r=merge(fhq[x].r,y);
update(x);
return x;
}
else{
fhq[y].l=merge(x,fhq[y].l);
update(y);
return y;
}
}
int fath(int now){
while(fhq[now].fa){
now=fhq[now].fa;
}
return now;
}
inline void ins(int k,int val){
int x,y,z;
split(root[k],val,x,y);
root[k]=merge(x,merge(new_node(val),y));
}
inline int getnum(int now,int rank){
if(fhq[now].siz<rank) return -1;
while(now){
if(fhq[fhq[now].l].siz+1==rank) break;
else if(fhq[fhq[now].l].siz>=rank){
now=fhq[now].l;
}
else{
rank-=fhq[fhq[now].l].siz+1;
now=fhq[now].r;
}
}
return now;
}
inline void meg(int x,int y){
int fx=fath(x),fy=fath(y);
if(fx!=fy){
if(fhq[fx].val>fhq[fy].val) swap(fx,fy);
root[fx]=merge(fx,fy);
root[fy]=0;
}
}
signed main(){
int n,m;scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
int xi;scanf("%d",&xi);
ins(i,xi);
}
for(int i=1;i<=m;i++){
int x,y;scanf("%d%d",&x,&y);
meg(x,y);
}
// for(int i=1;i<=n;i++){
// printf("check %d\n",fhq[root[i]].siz);
// }
int q;scanf("%d",&q);
while(q--){
char op;int x,y;scanf("%s%d%d",&op,&x,&y);
if(op=='Q'){
printf("%d\n",getnum(fath(x),y));
}
if(op=='B'){
meg(x,y);
}
// cout<<op<<" "<<x<<" "<<y<<'\n';
}
return 0;
}