Leonardo, like many other Italian scientists and artists of his age, was extremely interested in city planning and urban design. He aimed to model an ideal city: comfortable, spacious and rational in its usage of resources, far away from the narrow, claustrophobic cities of the Middle Ages.
The ideal city
The city is made of blocks placed on an infinite grid of cells. Each cell is identified by a pair of coordinates (row, column). Given a cell , the adjacent cells are: , , , and . Each block, when placed onto the grid, covers exactly one of the cells. A block can be placed onto the cell if and only if . We will use the coordinates of the cells to also refer to the blocks on top of them. Two blocks are adjacent if they are placed in adjacent cells. In an ideal city, all of its blocks are connected in such a way that there are no "holes" inside its border, that is, the cells must satisfy both conditions below.
- For any two empty cells, there exists at least one sequence of adjacent empty cells connecting them.
- For any two non-empty cells, there exists at least one sequence of adjacent non-empty cells connecting them.
Example 1
None of the configurations of blocks below represent an ideal city: the first two on the left do not satisfy the first condition, the third one does not satisfy the second condition, and the fourth one does not satisfy either of the conditions.
Distance
When traversing the city, a hop indicates going from one block to an adjacent one. Empty cells cannot be traversed. Let be the coordinates of the blocks placed on the grid. For any two distinct blocks at coordinates and , their distance is the smallest number of hops that are required to go from one of these blocks to the other one.
Example 2
The configuration below represents an ideal city made of blocks at coordinates , , , , , , , , , , and . For example, , , , and .
Statement
Your task is to, given an ideal city, write a program to compute the sum of all pairwise distances between blocks and for which . Formally, your program should compute the value of the following sum:
Specifically, you have to implement a routine DistanceSum(N, X, Y)
that, given and two arrays and that describe the city, calculates the formula above.
Both and are of size ; block is at coordinates for , and .
Since the result may be too big to be represented using bits, you should report it modulo (one billion).
In Example 2, there are pairs of blocks. The sum of all the pairwise distances is .
Subtask 1 [11 points]
You may assume that .
Subtask 2 [21 points]
You may assume that .
Subtask 3 [23 points]
You may assume that .
Additionally, the following two conditions hold: given any two non-empty cells and such that , every cell between them is non-empty too; given any two non-empty cells and such that , every cell between them is non-empty too.
Subtask 4 [45 points]
You may assume that .
Implementation Details
The program you submit must implement the subprogram described above using the following signature.
int DistanceSum(int N, int X[], int Y[]);
This subprogram must behave as described above. Of course you are free to implement other subprograms for its internal use. Your submissions must not interact in any way with standard input/output, nor with any other file.
Sample Grader
The sample grader will expect input in the following format:
- line :
- lines :
Attachment Package
The sample grader along with the example test case is available here.
Comments