Under construction

Still being built. Things may change

Adriatic

Time limit2sMemory limit256 MB

Summary
On a 2500 by 2500 grid, islands ordered alike from northwest to southeast connect in one hop, and each island needs the sum of fewest hops from all others.
Level

Hard9 of 10

Topics
Graph, Prefix sum, Combinatorics
Solved
No attempts yet

Problem

"The land of a thousand islands" was an official motto of Croatian tourism in the mid 1990s. The motto is not exact, because Croatia has a little more than 1000 islands, but island hopping, sailing from one island to the next, really is a popular summer activity.

In this task the map of the Adriatic sea is a grid of unit squares with 2500 rows and 2500 columns. Rows are numbered 1 through 2500 from north to south, and columns are numbered 1 through 2500 from west to east. There are NN islands in the sea, numbered 1 through NN, and each island lies inside one unit square. The location of island KK is given by the row number RKR_K and the column number CKC_K of that square. No two islands lie in the same square.

Because of the winds and the sea currents, a boat can sail directly from an island only to the islands that lie northwest or southeast of it. Precisely, it is possible to sail from island AA to island BB in one hop if RA<RBR_A < R_B and CA<CBC_A < C_B, or if RA>RBR_A > R_B and CA>CBC_A > C_B. Neither the distance between the two islands nor the presence of other islands between them changes this. When a direct hop from AA to BB is impossible, a sequence of hops through other islands can still connect them. The sailing distance from AA to BB is the smallest number of hops needed to sail from AA to BB.

For example, in the first example below, the island in row 2, column 3 reaches four islands in a single hop, and the sailing distance to each of the two remaining islands is 2.

A sailing congress is being planned and the organizers consider every island as a possible location. For a candidate island they want to know this: if every other island sends one sailboat, what is the smallest total number of hops the sailboats need in order to all reach the candidate island? That total is the sum of the sailing distances from all other islands to the candidate island. Given the locations of the NN islands, compute for every island KK the sum of the sailing distances from all other islands to island KK.

The input is such that for all islands AA and BB it is possible to sail from AA to BB using some sequence of hops.

Input

The first line contains an integer NN (3≤N≤250 0003 \le N \le 250\,000), the number of islands. Each of the next NN lines contains the location of one island: two integers between 1 and 2500 inclusive, the row number and the column number.

Output

Print NN lines. For each island, in the order the islands are given in the input, print on its own line the sum of the sailing distances from all other islands to that island.

Examples2

  1. Example 1

    Input
    7
    1 7
    7 5
    4 5
    4 8
    6 6
    6 1
    2 3
    
    Expected output
    16
    11
    12
    11
    12
    16
    8
    
  2. Example 2

    Input
    4
    1 1
    2 3
    3 2
    4 4
    
    Expected output
    3
    4
    4
    3