#P9083. [PA 2018] Ryki
[PA 2018] Ryki
Problem Description
This problem is translated from PA 2018 Round 5 Ryki.
Berlandia is an infinite chessboard made of square cells. Rows are numbered with increasing integers from bottom to top, and columns are numbered with increasing integers from left to right. Let denote the cell in row and column . If two different cells share at least one corner, we call them adjacent. This means the cells are 8-connected.
The distance between two cells and is the Euclidean distance, i.e.:
There are bears living in Berlandia. The -th bear lives in cell . Multiple bears may live in the same cell.
Bears can live alone, but sometimes they move closer to each other. When a bear roars, all bears in other cells immediately move to the adjacent cell that is closest to the roaring bear. It can be proven that there is exactly one such cell (no ties occur). Bears in the same cell as the roaring bear do not move.
For example, consider two bears: one in cell and the other in cell . If the bear in roars, the other bear will move to cell , and their final distance becomes .
The bears will roar one by one in the order: first bear, second bear, ..., last bear. Except for one bear called Limak: he is too cold to roar, and he also cannot leave his cell. Poor Limak.
However, you do not know which bear is Limak. For each from to , assuming the -th bear is Limak, find the final positions of all bears. For each possibility, it is enough to output the sum of the products of the row and column coordinates of all bears. That is, suppose that after roars, the -th bear is at , output:
Input Format
The first line contains a positive integer , the number of bears.
The next lines each contain two integers . The -th of these lines gives the initial position of the -th bear.
Output Format
Output lines. In the -th line, output one integer: assuming Limak is the -th bear, the sum of the products of row and column coordinates of all bears after the process ends.
4
3 5
2 1
1 4
2 1
27
24
25
35
Hint
Explanation for Sample 1
The figure below shows the case , i.e. the roaring order is . The red circles indicate the roaring bears. The final sum of products is .

Constraints
This problem uses bundled testdata.
It is guaranteed that in each subtask, at least one of the following three cases occurs:
- For all ,
- The time limit is seconds
Note: Since the detailed time limits for each test point were not published, the time limit for all test points of this problem is seconds.
Translated by ChatGPT 5