#P9272. [CEOI 2013] 千岛之国 / Adritic
[CEOI 2013] 千岛之国 / Adritic
Background
Translated from CEOI 2013 Day 2.
“Land of a Thousand Islands” was the official slogan of Croatia’s tourism industry in the mid-1990s. Although the slogan is not actually accurate (Croatia has slightly more than islands), “island hopping” (sailing from one island to another) is a popular summer activity. The map of the Adriatic Sea is represented by a grid of unit squares with rows and columns. Rows are numbered from north to south as to , and columns are numbered from west to east as to . There are islands in the sea, numbered from to , and each island lies inside one unit square of the grid. The position of island is given by the coordinates of that grid cell: its row number and column number . No two islands share the same position.
Due to wind and sea currents, you can sail directly from one island to another only when island is roughly to the northwest or southeast of . More precisely, you can “hop” from island to island only if either and both hold, or and both hold. Note that the distance between two islands, or the presence of other islands, does not affect whether it is possible to hop from one to the other. If it is not possible to hop directly from to , you may be able to travel from to using a sequence of hops via other islands. The travel distance from to is defined as the minimum number of hops needed to get from to .

For example, in the figure above, starting from the island located at row , column , we can hop to four other islands, while the travel distance to the remaining two islands is two hops.
Problem Description
A sailing regatta is being planned, and the organizers are considering each island as a possible venue. For each candidate island, they want to know: if every other island sends a sailboat, what is the minimum total number of hops required for all sailboats to reach the candidate island, or equivalently, what is the sum of travel distances from all other islands to the candidate island.
Write a program that, given the positions of islands, computes for each island the sum of travel distances from all other islands to island . The testdata guarantee that for every pair of islands and , it is possible to travel from to using some sequence of hops.
Input Format
The first line contains an integer (), the number of islands. The next lines contain the island positions. Each position is given as a pair of integers between and (inclusive), representing the row number and the column number.
Output Format
Output lines. For each island, in the order given in the input, output the sum of travel distances from all other islands to that island, one line per island.
7
1 7
7 5
4 5
4 8
6 6
6 1
2 3
16
11
12
11
12
16
8
4
1 1
2 3
3 2
4 4
3
4
4
3
Hint
In of the test cases, is at most .
In of the test cases, is at most .
In of the test cases, is at most .
In of the test cases, is at most .
In of the test cases, .
Note: can be .
Translated by ChatGPT 5