题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1512
水题,就是在考察左偏树,不多说,直接上代码就是了。
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=1e5+5;
struct Node{
int l,r,f,v,h;
}node[MAXN];
int merge(int a,int b){
if(a==0) return b;
if(b==0) return a;
if(node[a].v<node[b].v) swap(a,b);
node[a].r=merge(b,node[a].r);
node[node[a].r].f=a;
if(node[node[a].l].h<node[node[a].r].h) swap(node[a].l,node[a].r);
if(node[a].r==0) node[a].h=0;
else node[a].h=node[node[a].r].h+1;
return a;
}
int pop(int u){
int l=node[u].l;
int r=node[u].r;
node[l].f=l;
node[r].f=r;
node[u].h=node[u].l=node[u].r=0;
return merge(l,r);
}
int find(int u){ return node[u].f==u?u:find(node[u].f); }
int main(){
int n;
while(~scanf("%d",&n)){
int stg;
for(int i=1;i<=n;++i){
scanf("%d",&stg);
node[i].v=stg;
node[i].f=i;
node[i].l=node[i].r=node[i].h=0;
}
int q;
scanf("%d",&q);
while(q--){
int a,b;
scanf("%d%d",&a,&b);
a=find(a);
b=find(b);
if(a==b){
puts("-1");continue;
}
node[a].v/=2;
node[b].v/=2;
int a1=pop(a);
int b1=pop(b);
a=merge(a,a1);
b=merge(b,b1);
printf("%d\n",node[merge(a,b)].v);
}
}
}