http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2334
模板题
如果要删除一个点,pop了之后还要更新fa
由于fa的修改是O(n)的,所以这个点不能动
只能添加新点解决其他问题
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define rep(i,l,r) for(int i=(l),_=(r);i<=_;i++)
#define per(i,r,l) for(int i=(r),_=(l);i>=_;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define INE(i,u) for(int i=head[u];~i;i=e[i].next)
#define LL long long
inline const int read()
{int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;
for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
const int N=100010;
int n;
struct node
{
int key,l,r,fa,dis;
}T[N];
#define LS T[x].l
#define RS T[x].r
int find(int x){return x==T[x].fa?x:T[x].fa=find(T[x].fa);}
int merge(int x,int y)
{
if(x == 0) return y;
if(y == 0) return x;
if(T[x].key < T[y].key) swap(x,y);
T[x].r=merge(RS,y); T[RS].fa=x;
if(T[LS].dis < T[RS].dis) swap(LS,RS);
if(RS == 0) T[x].dis=0;
else T[x].dis=T[RS].dis+1;
return x;
}
int push(int x,int y)
{
return merge(x,y);
}
int pop(int x)
{
int l=LS,r=RS;
T[l].fa=l; T[r].fa=r;
LS=RS=T[x].dis=0;
return merge(l,r);
}
void input()
{
rep(i,1,n)
{
T[i]=(node){read(),0,0,i,0};
}
}
void solve()
{
int x,y;
rep(i,1,read())
{
x=find(read()); y=find(read());
if(x == y) puts("-1");
else
{
int rx=pop(x);
int ry=pop(y);
T[x].key/=2;
T[y].key/=2;
rx=push(rx,x);
ry=push(ry,y);
printf("%d\n",T[merge(rx,ry)].key);
}
}
}
int main()
{
//freopen("_.in","r",stdin); freopen("_.out","w",stdout);
while(~scanf("%d",&n))
input(),solve();
return 0;
}