#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 10001000 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 25002500 rows and 25002500 columns. Rows are numbered from north to south as 11 to 25002500, and columns are numbered from west to east as 11 to 25002500. There are NN islands in the sea, numbered from 11 to NN, and each island lies inside one unit square of the grid. The position of island KK is given by the coordinates of that grid cell: its row number RKR_K and column number CKC_K. 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 BB is roughly to the northwest or southeast of AA. More precisely, you can “hop” from island AA to island BB only if either RA<RBR_A < R_B and CA<CBC_A < C_B both hold, or RA>RBR_A > R_B and CA>CBC_A > C_B 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 AA to BB, you may be able to travel from AA to BB using a sequence of hops via other islands. The travel distance from AA to BB is defined as the minimum number of hops needed to get from AA to BB.

For example, in the figure above, starting from the island located at row 22, column 33, 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 NN islands, computes for each island KK the sum of travel distances from all other islands to island KK. The testdata guarantee that for every pair of islands AA and BB, it is possible to travel from AA to BB using some sequence of hops.

Input Format

The first line contains an integer NN (1N2500001\le N\le250000), the number of islands. The next NN lines contain the island positions. Each position is given as a pair of integers between 11 and 25002500 (inclusive), representing the row number and the column number.

Output Format

Output NN 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 25%25\% of the test cases, NN is at most 100100.

In 50%50\% of the test cases, NN is at most 15001500.

In 60%60\% of the test cases, NN is at most 50005000.

In 80%80\% of the test cases, NN is at most 2500025000.

In 100%100\% of the test cases, 1N2500001\le N\le250000.

Note: NN can be 11.

Translated by ChatGPT 5