F - Rooks Editorial /

Time Limit: 1.25 sec / Memory Limit: 1024 MB

配点 : 22002200

問題文

無限に広がるチェス盤上の NN 個の敵ルークの位置 (Xi,Yi)(X_i, Y_i) が与えられます。[訳注: ルークは将棋の飛車と似た動きをする駒です。] どの二つのルークも、お互いを攻められる位置にはありません (すなわち、一行あたり、または一列あたりのルークの数は一個以下です)。

あなたは、ルークのうち一個をキングに置き換え、キングを繰り返し動かしてできるだけ多くのルークを取ろうとしています。[訳注: キングは将棋の王将と似た動きをする駒です。]

ルークに攻められているマスに入ることはできません。 また、斜めに移動することで空きマスに移ることもできません (しかし、斜めに移動することでルークを取ることはできます)。

(つまり、このキングの動きは、斜め四方向に動いて駒を取ることと縦横四方向に動くことができる強化版ポーンのようなものです。)

各ルークについて、そのルークをキングで置き換えた際に取ることのできる最大数のルークを取るために必要な最小手数を求めてください。

制約

  • 2N2000002 \leq N \leq 200\,000
  • 1Xi,Yi1061 \leq X_i, Y_i \leq 10^6
  • XiXjX_i \neq X_j
  • YiYjY_i \neq Y_j
  • 入力中の値はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

NN
X1X_1 Y1Y_1
X2X_2 Y2Y_2
\vdots
XNX_N YNY_N

出力

NN 行出力せよ。 ii 行目は、(Xi,Yi)(X_i, Y_i) に置かれたルークをキングに置き換えた場合に対応する。 この行には、一つの整数、すなわち MiM_i 個のルークを取るために必要な最小手数を出力せよ。 ここで、MiM_i は (何手かけてもよいとして) この場合に取ることのできるルークの最大数である。


入力例 1Copy

Copy
6
1 8
6 10
2 7
4 4
9 3
5 1

出力例 1Copy

Copy
5
0
7
5
0
0

下図を見てください。 ルーク 33 をキングに置き換えた場合、他のルークを最大で二個取ることができます。 図の赤い経路がこの場合の最適な手順の一つ - ルーク 11 を取り、右下方向に進み続けてルーク 44 を取る - です。 ここでの手数は 77 手であり、これが出力例の三つ目の数です。

path

xx 軸正方向: 右、yy 軸正方向: 上

ルーク 2,5,62, 5, 6 のいずれかをキングに置き換えた場合には、他のルークを一個も取ることができません。このとき、最小手数は 00 です。


入力例 2Copy

Copy
5
5 5
100 100
70 20
81 70
800 1

出力例 2Copy

Copy
985
985
1065
1034
0

入力例 3Copy

Copy
10
2 5
4 4
13 12
12 13
14 17
17 19
22 22
16 18
19 27
25 26

出力例 3Copy

Copy
2
2
9
9
3
3
24
5
0
25

Score : 22002200 points

Problem Statement

You are given positions (Xi,Yi)(X_i, Y_i) of NN enemy rooks on an infinite chessboard. No two rooks attack each other (at most one rook per row or column).

You're going to replace one rook with a king and then move the king repeatedly to beat as many rooks as possible.

You can't enter a cell that is being attacked by a rook. Additionally, you can't move diagonally to an empty cell (but you can beat a rook diagonally).

(So this king moves like a superpawn that beats diagonally in 4 directions and moves horizontally/vertically in 4 directions.)

For each rook, consider replacing it with a king, and find the minimum possible number of moves needed to beat the maximum possible number of rooks.

Constraints

  • 2N2000002 \leq N \leq 200\,000
  • 1Xi,Yi1061 \leq X_i, Y_i \leq 10^6
  • XiXjX_i \neq X_j
  • YiYjY_i \neq Y_j
  • All values in input are integers.

Input

Input is given from Standard Input in the following format.

NN
X1X_1 Y1Y_1
X2X_2 Y2Y_2
\vdots
XNX_N YNY_N

Output

Print NN lines. The ii-th line is for scenario of replacing the rook at (Xi,Yi)(X_i, Y_i) with your king. This line should contain one integer: the minimum number of moves to beat MiM_i rooks where MiM_i denotes the maximum possible number of beaten rooks in this scenario (in infinite time).


Sample Input 1Copy

Copy
6
1 8
6 10
2 7
4 4
9 3
5 1

Sample Output 1Copy

Copy
5
0
7
5
0
0

See the drawing below. If we replace rook 3 with a king, we can beat at most two other rooks. The red path is one of optimal sequences of moves: beat rook 1, then keep going down and right until you can beat rook 4. There are 7 steps and that's the third number in the output.

path

x-coordinate increases from left to right, while y increases bottom to top.

Starting from rook 2, 5 or 6, we can't beat any other rook. The optimal number of moves is 0.


Sample Input 2Copy

Copy
5
5 5
100 100
70 20
81 70
800 1

Sample Output 2Copy

Copy
985
985
1065
1034
0

Sample Input 3Copy

Copy
10
2 5
4 4
13 12
12 13
14 17
17 19
22 22
16 18
19 27
25 26

Sample Output 3Copy

Copy
2
2
9
9
3
3
24
5
0
25


2025-03-29 (Sat)
09:21:42 +00:00