鏈塏鍙尨瀛? 姣忓彧閮芥湁涓涓姏閲忓? 寮濮嬬殑鏃跺欎簰涓嶈璇? 瀹冧滑涔嬮棿浼?xì)鍙戠敓M嬈℃枟浜? 姣忔鍙戠敓a, b鐨勬枟浜夋椂, a, b閮戒細(xì)浠庡悇鑷殑鏈嬪弸鍦堥噷鎷夊嚭涓涓渶寮虹殑, 涔嬪悗涓ゅ彧鐚村瓙鎵? 鎵撳畬鍚庤繖涓ゅ彧鐚村瓙鐨勫姏閲忓煎悇鍑忓崐. 騫朵笖鎵撳畬鍚? 涓ゅ彧鐚村瓙鐨勬湅鍙嬪湀鐨勬墍鏈変漢閮戒簰鐩歌璇?涔熷氨鏄笉浼?xì)鍐嶆?.
瀵逛簬姣忎竴涓妭鐐? 瀹氫箟dis 琛ㄧずX鑺傜偣鍒版渶鍙寵竟鐨勭┖鑺傜偣鐨勮窛紱葷殑鏈灝忓?/div>
瀵逛簬姣忎釜鑺傜偣X, 瑕佹眰X鐨勫乏鍎垮瓙鐨刣is >= 鍙沖効瀛愮殑dis, 閭d箞瀹規(guī)槗鍙戠幇, 瀵逛簬N涓妭鐐圭殑宸﹀亸鏍? 鍏跺彸鍎垮瓙鏈澶氬彧鏈塴ogN涓妭鐐?
鍚堝茍鎿嶄綔灝辨槸璁╁鏉傚害钀藉湪鍙沖効瀛愪笂, 浠庤岃揪鍒發(fā)ogN鐨勫悎騫跺鏉傚害.
棣栧厛瀵逛簬涓や釜鍫? 鑻ュ叾涓竴涓負(fù)絀? 榪斿洖鍙︿竴涓?
鍚﹀垯(榪欓噷浠ュぇ鏍瑰爢涓轟緥), a鎸囧悜鍫嗛《杈冨ぇ鐨勫爢, b鎸囧悜鍙︿竴涓? 璁゛鐨勫彸鍎垮瓙鍜宐鍚堝茍, 鍚堝茍鍚庣殑瀛愭爲(wèi)浣滀負(fù)a鐨勫彸鍎垮瓙.
鎺ヤ笅鏉? 媯鏌鐨勪袱涓効瀛愭槸鍚︽弧瓚砫is, 涓嶆弧瓚沖氨浜ゆ崲涓や釜鍎垮瓙.
鏈鍚? 鏇存柊a鐨刣is.
榪欐牱灝卞鏄撳疄鐜板爢鐨勫叾浠栨搷浣?( 姣斿鎻掑叆, 鍒犻櫎欏剁瓑 ).
鍙﹀ 榪橀渶瑕佺敤鍒?騫舵煡闆?
*/
//#pragma warning( disable:4789 )
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <utility>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;
const int MM = 100010;
struct left {
int l,r,dis,val,dad;
} heap[MM];
int N, M;
inline int max ( const int &a, const int &b) {
return a > b ? a : b;
}
inline int find ( int &x ) {
return heap[x].dad == x ? x : heap[x].dad = find ( heap[x].dad );
}
inline void swap(int &a, int &b) {
a ^= b ^= a ^= b;
}
inline int merge ( int x, int y ) {
if ( x == 0 ) return y;
if ( y == 0 ) return x;
if ( heap[y].val > heap[x].val ) swap ( x, y );
heap[x].r = merge ( heap[x].r, y );
heap[heap[x].r].dad = x;
if ( heap[ heap[x].l ].dis < heap[ heap[x].r ].dis )
swap ( heap[x].l, heap[x].r );
if ( heap[x].r == 0 ) heap[x].dis = 0;
else heap[x].dis = heap[ heap[x].r ].dis + 1;
return x;
}
inline int push ( int x, int y ) {
return merge ( x, y );
}
inline int pop ( int &x ) {
int l = heap[x].l;
int r = heap[x].r;
heap[l].dad = l;
heap[r].dad = r;
heap[x].l = heap[x].r = heap[x].dis = 0;
return merge ( l, r );
}
inline bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<'0'||in>'9')) in=getchar();
if(in=='-'){ IsN=true;num=0;}
else num=in-'0';
while(in=getchar(),in>='0'&&in<='9'){
num*=10,num+=in-'0';
}
if(IsN) num=-num;
return true;
}
int main() {
while ( scan_d ( N ) ) {
for ( int i = 1; i <= N; ++ i ) {
scan_d ( heap[i].val );
heap[i].l = heap[i].r = heap[i].dis = 0;
heap[i].dad = i;
}
scan_d ( M );
int a, b, x, y;
while ( M -- ) {
scan_d (a); scan_d (b);
x = find ( a );
y = find ( b );
if ( x == y ) {
puts ( "-1" );
} else {
heap[x].val /= 2;
int xx = push ( pop ( x ), x );
heap[y].val /= 2;
int yy = push ( pop ( y ), y );
printf ( "%d\n", heap[ merge ( xx, yy ) ].val );
}
}
}
return 0;
}