eolymp
bolt
Try our new interface for solving problems

Pairs

\includegraphics{https://static.e-olymp.com/content/5a/5a2cb74e5c3153ac30080e63a4bdc2908bee0917.jpg} Mirko and Slavko are playing with toy animals. First, they choose one of three boards given in the figure below. Each board consists of cells (shown as circles in the figure) arranged into a one, two or three dimensional grid. The distance between two cells is the smallest number of moves that an animal would need in order to reach one cell from the other. In one move, the animal may step into one of the adjacent cells (connected by line segments in the figure). Two animals can hear each other if the distance between their cells is at most \textbf{D}. Slavko's task is to calculate how many pairs of animals there are such that one animal can hear the other. Write a program that, given the board type, the locations of all animals, and the number \textbf{D}, finds the desired number of pairs. \InputFile The first line of input contains four integers in this order: · The board type \textbf{B} (\textbf{1 ≤ B ≤ 3}); · The number of animals \textbf{N} (\textbf{1 ≤ N ≤ 100 000}); · The largest distance \textbf{D} at which two animals can hear each other (\textbf{1 ≤ D ≤ 100 000 000}); · The size of the board M (the largest coordinate allowed to appear in the input): · When \textbf{B=1}, \textbf{M} will be at most \textbf{75 000 000}. · When \textbf{B=2}, \textbf{M} will be at most \textbf{75 000}. · When \textbf{B=3}, \textbf{M} will be at most \textbf{75}. Each of the following \textbf{N} lines contains B integers separated by single spaces, the coordinates of one toy animal. Each coordinate will be between \textbf{1} and \textbf{M} (inclusive). More than one animal may occupy the same cell. \OutputFile Output should consist of a single integer, the number of pairs of animals that can hear each other.
Time limit 5 seconds
Memory limit 64 MiB
Input example #1
1 6 5 100
25
50
50
10
20
23
Output example #1
4
Source IOI-2007, Day 2