Under construction

Still being built. Things may change

Closest Pair

Time limit1.5sMemory limit512 MB

Summary
Both point sets lie on horizontal lines; find the Manhattan distance of the closest P-Q pair and count how many distinct pairs achieve it.
Level

Medium6 of 10

Topics
Sorting, Two pointers, Array
Solved
No attempts yet

Problem

You are given two finite sets of points in the plane, PP and QQ. A closest pair of PP and QQ is a pair (p,q)(p, q) with p∈Pp \in P and q∈Qq \in Q whose distance is the smallest among all pairs (p′,q′)(p', q') with p′∈Pp' \in P and q′∈Qq' \in Q.

In this problem the distance between two points aa and bb is defined as follows.

d(a,b)=∣xa−xb∣+∣ya−yb∣d(a, b) = |x_a - x_b| + |y_a - y_b|

Here xax_a and yay_a are the xx and yy coordinates of point aa, and xbx_b and yby_b are the xx and yy coordinates of point bb. So a pair (p,q)(p, q) with p∈Pp \in P and q∈Qq \in Q is a closest pair of PP and QQ if and only if

d(p,q)=min⁡{d(p′,q′):p′∈P, q′∈Q}d(p, q) = \min \{ d(p', q') : p' \in P,\ q' \in Q \}

Given PP and QQ, write a program that computes the distance of a closest pair and the number of distinct closest pairs. Two pairs count as distinct when the point taken from PP differs or the point taken from QQ differs.

The input points satisfy these two properties.

  1. There are integers c1c_1 and c2c_2 such that every point of PP lies on the line y=c1y = c_1 and every point of QQ lies on the line y=c2y = c_2.
  2. No two points of PP have the same coordinates, and no two points of QQ have the same coordinates.

Input

Your program reads from standard input. The input consists of four lines.

The first line contains two integers nn and mm (1≤n≤5×1051 \le n \le 5 \times 10^5, 1≤m≤5×1051 \le m \le 5 \times 10^5), where nn is the number of points in PP and mm is the number of points in QQ.

The second line contains two integers c1c_1 and c2c_2 (−108≤c1,c2≤108-10^8 \le c_1, c_2 \le 10^8) in this order, separated by a single space.

The third line contains nn distinct integers between −108-10^8 and 10810^8, separated by single spaces. They are the xx coordinates of the points in PP, whose yy coordinates all equal c1c_1.

The fourth line contains mm distinct integers between −108-10^8 and 10810^8, separated by single spaces. They are the xx coordinates of the points in QQ, whose yy coordinates all equal c2c_2.

Output

Your program writes to standard output. Print exactly one line holding two integers separated by a single space: the distance of a closest pair of PP and QQ, then the number of closest pairs of PP and QQ.

Examples4

  1. Example 1

    Input
    3 4
    1 -3
    3 0 6
    -2 5 4 2
    
    Expected output
    5 3
    
  2. Example 2

    Input
    5 5
    1 2
    -4 -10 -2 0 -1
    3 18 0 1 5
    
    Expected output
    1 1
    
  3. Example 3

    Input
    1 1
    0 0
    0
    0
    
    Expected output
    0 1
    
  4. Example 4

    Input
    1 1
    -100000000 100000000
    -100000000
    100000000
    
    Expected output
    400000000 1