This page is still under construction.

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

Toy Animals

Time limit2sMemory limit128 MB

Summary
Count pairs of points on a 1D, 2D, or 3D integer grid whose Manhattan distance is at most D.
Level

Hard8 of 10

Topics
Divide and conquer, Sorting, Geometry, Bit manipulation
Solved
No attempts yet

Problem

Sanggeun and Seonyoung are playing with toy animals. First they pick one of the three game boards below. Each board is made up of many cells; board 11 is one-dimensional (a line), board 22 is two-dimensional (a grid), and board 33 is three-dimensional (a solid grid).

game boards

Each cell is identified by integer coordinates, and adjacent cells are joined by a line segment.

  • Board 11: a cell is written as xx, and cell xx is adjacent to cells x−1x-1 and x+1x+1.
  • Board 22: a cell is written as (x,y)(x, y), and cell (x,y)(x, y) is adjacent to (x±1, y)(x\pm1,\ y) and (x, y±1)(x,\ y\pm1).
  • Board 33: a cell is written as (x,y,z)(x, y, z), and cell (x,y,z)(x, y, z) is adjacent to (x±1, y, z)(x\pm1,\ y,\ z), (x, y±1, z)(x,\ y\pm1,\ z), and (x, y, z±1)(x,\ y,\ z\pm1).

Sanggeun places NN toy animals on the cells. Several animals may share the same cell.

The distance between two cells is the least number of moves needed to travel from one to the other. Because each move goes to an adjacent cell, the distance equals the sum of the absolute differences of the coordinates.

  • Board 11: ∣x1−x2∣|x_1 - x_2|
  • Board 22: ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|
  • Board 33: ∣x1−x2∣+∣y1−y2∣+∣z1−z2∣|x_1 - x_2| + |y_1 - y_2| + |z_1 - z_2|

Two toy animals can hear each other when the distance between their cells is at most DD. Given the board type, the position of every toy animal, and DD, write a program that counts the number of pairs of toy animals that can hear each other.

Input

The first line contains four integers BB, NN, DD, and MM separated by spaces.

  • BB is the board type. (1≤B≤31 \le B \le 3)
  • NN is the number of toy animals. (1≤N≤100 0001 \le N \le 100\,000)
  • DD is the greatest distance at which two toy animals can hear each other. (1≤D≤100 000 0001 \le D \le 100\,000\,000)
  • MM is the largest value a coordinate can take. If B=1B = 1 then M≤75 000 000M \le 75\,000\,000; if B=2B = 2 then M≤75 000M \le 75\,000; if B=3B = 3 then M≤75M \le 75.

Each of the next NN lines gives the coordinates of one toy animal. On board BB a line contains BB integers separated by spaces: xx when B=1B = 1, x yx\ y when B=2B = 2, and x y zx\ y\ z when B=3B = 3. Every coordinate is a natural number between 11 and MM inclusive. Several animals may occupy the same cell.

Output

Print on the first line the number of pairs of toy animals that can hear each other.

Examples7

  1. Example 1

    Input
    2 5 4 10
    5 2
    7 2
    8 4
    6 5
    4 4
    
    Expected output
    8
    
  2. Example 2

    Input
    1 4 3 100
    1
    2
    5
    9
    
    Expected output
    2
    
  3. Example 3

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

    Input
    1 6 100000000 75000000
    1
    100
    50000000
    2
    75000000
    999
    
    Expected output
    15
    
  5. Example 5

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

    Input
    2 5 100000000 75000
    1 1
    75000 75000
    1 75000
    40000 40000
    2 3
    
    Expected output
    10
    
  7. Example 7

    Input
    3 4 2 5
    1 1 1
    2 1 1
    1 2 2
    3 3 3
    
    Expected output
    2