有一个30000*N(i) 的列队,2种操作

1. M i,j    i行移动到j行的末尾

2.C i,j   询问i行和j行的距离(如果在同一列)

 

#include <bits/stdc++.h>
using namespace std ;
 const int N=3e4;
 int fa[N+2],L[N],n,sz[N]; 
 
 int find(int x){
 	if(x==fa[x]) return x;
 	int t=find(fa[x]);
 	L[x]+=L[fa[x]];
 	
 	return fa[x]=t;
 }
 
 signed main(){
 	int x,y,tes;char op;
 	cin>>tes;
 	for(int i=1;i<=N;i++) fa[i]=i,sz[i]=1;
 	while(tes--){
 		cin>>op>>x>>y; int fx=find(x),fy=find(y);
 		
 		if(op=='M') 
 			L[fx]+=sz[fy],fa[fx]=fa[fy],sz[fy]+=sz[fx],sz[fx]=0;
 		else{
 			if(fx==fy) cout<<abs(L[x]-L[y])-1<<endl;
 			else cout<<-1<<endl;
 		}
 	}
 }
 
 
 

 

posted on 2023-03-14 17:59  towboat  阅读(16)  评论(0)    收藏  举报