RT,蒟蒻发现第一个测试样例中有180+/1000行输出有问题,但是调了两天还是没有发现问题,不知道是哪里出现了问题,求谷内大佬们看看是FHQ写挂了,还是做法问题QWQ
感谢您的帮助,蒟蒻会关注回报的。
原码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<random>
using namespace std;
const int N=1e6+10;
std::mt19937 rng(std::random_device{}());
int n,m,q;
struct fhq{
int fa[N],tot=0,ch[N][2],val[N],siz[N],key[N];
int find(int x){
if(fa[x]==x){
return x;
}
return fa[x]=find(fa[x]);
}
void mergehb(int x,int y){
int fx=find(x),fy=find(y);
fa[fx]=fy;
}
int newnode(int k){
++tot;
val[tot]=k;
siz[tot]=1;
key[tot]=rng();
return tot;
}
void pushup(int pos){
siz[pos]=siz[ch[pos][0]]+siz[ch[pos][1]]+1;
}
void split(int pos,int k,int &x,int &y){
if(!pos){
x=y=0;
return;
}
else if(val[pos]<=k){
x=pos;
split(ch[pos][1],k,ch[pos][1],y);
}
else{
y=pos;
split(ch[pos][0],k,x,ch[pos][0]);
}
pushup(pos);
}
int merge(int x,int y){
if(!x||!y){
return x+y;
}
if(key[x]<key[y]){
ch[x][1]=merge(ch[x][1],y);
pushup(x);
return x;
}
else{
ch[y][0]=merge(x,ch[y][0]);
pushup(y);
return y;
}
}
int kth(int pos,int rank){
if(rank<=siz[ch[pos][0]]){
return kth(ch[pos][0],rank);
}
else if(rank==siz[ch[pos][0]]+1){
return pos;
}
else{
return kth(ch[pos][1],rank-siz[ch[pos][0]]-1);
}
}
void insert(int pos,int &rt){
int x,y;
split(rt,val[pos],x,y);
rt=merge(x,merge(pos,y));
}
void qfshb(int &pos,int &rt){
if(!pos){
return;
}
qfshb(ch[pos][0],rt);
qfshb(ch[pos][1],rt);
ch[pos][0]=ch[pos][1]=0;
siz[pos]=1;
insert(pos,rt);
}
void hb(int x,int y){
int &rt1=fa[x],&rt2=fa[y];
qfshb(rt1,rt2);
}
}tr;
int main(){
// freopen("P3224_1.in","r",stdin);
// freopen("P3224.ans","w",stdout);
scanf("%d%d",&n,&m);
char opt;
int x,y;
for(int i=1;i<=n;i++){
scanf("%d",&x);
tr.newnode(x);
tr.fa[i]=i;
}
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
int fx=tr.find(x),fy=tr.find(y);
if(fx!=fy){
if(tr.siz[fx]>tr.siz[fy]){
swap(x,y);
}
tr.hb(x,y);
tr.mergehb(fx,fy);
}
}
scanf("%d",&q);
for(int i=1;i<=q;i++){
scanf("%c",&opt);
scanf("%c %d %d",&opt,&x,&y);
if(opt=='B'){
int fx=tr.find(x),fy=tr.find(y);
if(fx!=fy){
if(tr.siz[fx]>tr.siz[fy]){
swap(x,y);
}
tr.hb(x,y);
tr.mergehb(fx,fy);
}
}
else{
int fx=tr.find(x);
if(tr.siz[fx]<y){
printf("-1\n");
}
else{
printf("%d\n",tr.kth(fx,y));
}
}
}
}