标签:
5 20 16 10 10 4 5 2 3 3 4 3 5 4 5 1 5
8 5 5 -1 10
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int maxn = 200010; 6 struct node{ 7 int l,r,f,value,dis; 8 }; 9 node heap[maxn]; 10 int mergeIt(int a,int b){ 11 if(a == 0) return b; 12 if(b == 0) return a; 13 if(heap[a].value < heap[b].value) swap(a,b); 14 heap[a].r = mergeIt(heap[a].r,b); 15 heap[heap[a].r].f = a; 16 if(heap[heap[a].l].dis < heap[heap[a].r].dis) swap(heap[a].l,heap[a].r); 17 if(heap[a].r == 0) heap[a].dis = 0; 18 else heap[a].dis = heap[heap[a].r].dis+1; 19 return a; 20 } 21 int pop(int a){ 22 int l = heap[a].l; 23 int r = heap[a].r; 24 heap[l].f = l; 25 heap[r].f = r; 26 heap[a].l = heap[a].r = heap[a].dis = 0; 27 return mergeIt(l,r); 28 } 29 int findF(int a){ 30 return heap[a].f = heap[a].f == a?a:findF(heap[a].f); 31 } 32 int main(){ 33 int n,m; 34 while(~scanf("%d",&n)){ 35 for(int i = 1; i <= n; ++i){ 36 scanf("%d",&heap[i].value); 37 heap[i].l = heap[i].r = heap[i].dis = 0; 38 heap[i].f = i; 39 } 40 scanf("%d",&m); 41 while(m--){ 42 int fa,fb,a,b; 43 scanf("%d %d",&a,&b); 44 fa = findF(a); 45 fb = findF(b); 46 if(fa == fb) puts("-1"); 47 else{ 48 heap[fa].value >>= 1; 49 int u = pop(fa); 50 u = mergeIt(u,fa); 51 52 heap[fb].value >>= 1; 53 int v = pop(fb); 54 v = mergeIt(v,fb); 55 56 printf("%d\n",heap[mergeIt(u,v)].value); 57 } 58 } 59 } 60 return 0; 61 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4331799.html