This page is still under construction.

Parts of this page are still being built. What you see may change.

Flood Fill

Time limit2sMemory limit128 MB

Summary
Given M points and a threshold D, group points whose taxicab distance is at most D into connected components, then report the number of components and the largest component size.
Level

Hard8 of 10

Topics
Union-find, Sorting, Divide and conquer, Geometry
Solved
No attempts yet

Problem

There are MM points on the plane. The taxicab distance between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is defined as ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|.

Two points are said to be directly connected if their taxicab distance is at most DD. A group of points that can reach one another by following a chain of direct connections forms a single island. In other words, an island is a connected component of the "directly connected" relation.

For the given points, find the number of islands and the size of the largest island (the number of points it contains).

When D=1D = 1, only points on the same cell or one orthogonal step apart are connected, which is the classic Flood Fill problem. This problem generalises that threshold to an arbitrary DD.

Input

The first line contains the number of points MM and the distance threshold DD. (1≤M≤1000001 \le M \le 100000, 1≤D≤1091 \le D \le 10^9)

Each of the next MM lines contains the coordinates XiX_i and YiY_i of a point. (1≤Xi,Yi≤1091 \le X_i, Y_i \le 10^9)

The same coordinates may appear more than once; in that case the two points have taxicab distance 00 and therefore always belong to the same island.

Output

Print the number of islands and the size of the largest island on one line, separated by a space.

Hint

Because the coordinate range is very large, comparing every pair of points directly can be slow. If you change coordinates to u=x+yu = x + y and v=x−yv = x - y, the taxicab distance becomes the Chebyshev distance max⁡(∣u1−u2∣, ∣v1−v2∣)\max(|u_1 - u_2|,\ |v_1 - v_2|). Then two points are directly connected exactly when ∣u1−u2∣≤D|u_1 - u_2| \le D and ∣v1−v2∣≤D|v_1 - v_2| \le D — that is, when they fall inside a common axis-aligned square of side DD in the transformed coordinates.

Examples4

  1. Example 1

    Input
    4 2
    1 1
    3 3
    2 2
    10 10
    
    Expected output
    2 3
    
  2. Example 2

    Input
    2 4
    1 1
    3 3
    
    Expected output
    1 2
    
  3. Example 3

    Input
    2 3
    1 1
    3 3
    
    Expected output
    2 1
    
  4. Example 4

    Input
    5 2
    1 1
    2 2
    3 3
    100 100
    101 100
    
    Expected output
    2 3