用并查集维护一下当前联通块的根节点
#include<bits/stdc++.h>
const int N=5000005;
using namespace std;
template<class T>
inline void read(T &x)
{
x=0;
static char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
}
inline void write(int x)
{
if(x<0)
{
x=-x;
putchar('-');
}
if(x>9) write(x/10);
putchar(x%10+'0');
}
int n,m,father[N],val[N],rt[N],cnt,size[N],lson[N],rson[N],id[N];
inline int getfather(int x)
{
if(father[x]==x) return x;
return father[x]=getfather(father[x]);
}
inline void update(int &now,int l,int r,int pos)
{
if(!now) now=++cnt;
if(l==r) {size[now]++;return;}
int mid=(l+r)>>1;
if(pos<=mid) update(lson[now],l,mid,pos);
else update(rson[now],mid+1,r,pos);
s