1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<vector> using namespace std; #define int long long #define MAXN 800006 #define pb push_back #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define inf 0x3f3f3f3f3f3f3f3f int n , q; namespace SGT { pii T[MAXN << 2]; void pushup( int rt ) { T[rt] = min( T[rt << 1] , T[rt << 1 | 1] ); } void build( int rt , int l , int r ) { T[rt] = mp( inf , inf ); if( l == r ) return; int m = l + r >> 1; build( rt << 1 , l , m ) , build( rt << 1 | 1 , m + 1 , r ); } void mdfy( int rt , int l , int r , int p , pii c ) { if( l == r && p == l ) { T[rt] = min( T[rt] , c ); return; } int m = l + r >> 1; if( p <= m ) mdfy( rt << 1 , l , m , p , c ); else mdfy( rt << 1 | 1 , m + 1 , r , p , c ); pushup( rt ); } pii que( int rt , int l , int r , int L , int R ) { if( L <= l && R >= r ) return T[rt]; int m = l + r >> 1; pii res = mp( inf , inf ); if( L <= m ) res = min( res , que( rt << 1 , l , m , L , R ) ); if( R > m ) res = min( res , que( rt << 1 | 1 , m + 1 , r , L , R ) ); return res; } } #define mx 600000 struct point{ int x , y , id; } P[MAXN] ; bool cmp( point x , point y ) { return x.x == y.x ? x.y > y.y : x.x > y.x; } int A[MAXN] , a[MAXN]; struct edge{ int u , v , w; } E[MAXN] ; int ecn; void doit( ) { sort( P + 1 , P + 1 + n , cmp ); for( int i = 1 ; i <= n ; ++ i ) A[i] = P[i].x - P[i].y; sort( A + 1 , A + 1 + n ); int sz = unique( A + 1 , A + 1 + n ) - A - 1; for( int i = 1 ; i <= n ; ++ i ) a[i] = lower_bound( A + 1 , A + 1 + sz , P[i].x - P[i].y ) - A ; SGT::build( 1 , 1 , mx ); pii res = mp( 0 , 0 ); for( int i = 1 ; i <= n ; ++ i ) { pii fd = SGT::que( 1 , 1 , mx , 1 , a[i] ); if( fd.se != 0x3f3f3f3f3f3f3f3f ) E[++ecn] = ( edge ) { P[i].id , fd.se , - P[i].x - P[i].y + fd.fi }; SGT::mdfy( 1 , 1 , mx , a[i] , mp( P[i].x + P[i].y , P[i].id ) ); } } bool cmpp( edge a , edge b ) { return a.w < b.w; } int fa[MAXN]; int find( int x ) { return x == fa[x] ? x : fa[x] = find( fa[x] ); }
namespace tree { int head[MAXN] , nex[MAXN] , to[MAXN] , wto[MAXN] , cn; void ade( int u , int v , int w ) { nex[++cn] = head[u] , to[cn] = v , wto[cn] = w , head[u] = cn; } #define MAXK 20 int G[MAXN][MAXK][2]; int dep[MAXN]; void init( ) { dep[1] = 0; memset( head , 0 , sizeof head ); memset( G , 0 , sizeof G ); cn = 0; } void dfs( int u , int fa ) { dep[u] = dep[fa] + 1; for( int i = head[u] ; i ; i = nex[i] ) { int v = to[i]; if( v == fa ) continue; G[v][0][0] = u , G[v][0][1] = wto[i]; for(int j = 1 ; j < 20 ; ++ j) if(G[G[v][j - 1][0]][j - 1][0]) G[v][j][0] = G[G[v][j-1][0]][j-1][0],G[v][j][1] = max(G[v][j-1][1],G[G[v][j-1][0]][j-1][1]); else break; dfs( v , u ); } } int que( int u , int v ) { if( dep[u] < dep[v] ) swap( u , v ); int suml = -inf , sumr = -inf; if( dep[u] != dep[v] ) for( int k = MAXK - 1 ; k >= 0 ; -- k ) if( dep[G[u][k][0]] >= dep[v] ) suml = max( suml , G[u][k][1] ) , u = G[u][k][0]; if( u == v ) return max( suml , sumr ); for( int k = MAXK - 1 ; k >= 0 ; -- k ) if( G[u][k][0] != G[v][k][0] ) suml = max( suml , G[u][k][1] ) , sumr = max( sumr , G[v][k][1] ), u = G[u][k][0] , v = G[v][k][0]; suml = max( suml , G[u][0][1] ) , sumr = max( sumr , G[v][0][1] ); return max( suml , sumr ); } }
signed main( ) { freopen("fuck.in","r",stdin); int T;cin >> T; while( T-- ) { cin >> n; ecn = 0; for( int i = 1 , u , v ; i <= n ; ++ i ) scanf("%lld%lld",&P[i].x,&P[i].y) , P[i].id = i; doit( ); for( int i = 1 ; i <= n ; ++ i ) swap( P[i].x , P[i].y ); doit( ); for( int i = 1 ; i <= n ; ++ i ) P[i].x = -P[i].x; doit( ); for( int i = 1 ; i <= n ; ++ i ) swap( P[i].x , P[i].y ); doit( ); sort( E + 1 , E + 1 + ecn , cmpp ); tree::init( ); for( int i = 1 ; i <= n ; ++ i ) fa[i] = i; for( int i = 1 ; i <= ecn ; ++ i ) if( find( E[i].u ) != find( E[i].v ) ) fa[find( E[i].u )] = find( E[i].v ) , tree::ade( E[i].u , E[i].v , E[i].w ) , tree::ade( E[i].v , E[i].u , E[i].w ); tree::dfs( 1 , 1 ); cin >> q; int u , v; while( q-- ) { scanf("%lld%lld",&u,&v); printf("%lld\n",tree::que( u , v )); } } }
|