#P12148. Piece Removal on an Infinite Chessboard

    ID: 14248 Type: Default 1000ms 256MiB

Piece Removal on an Infinite Chessboard

Piece Removal on an Infinite Chessboard

On an infinite chessboard there are \(n\) pieces located at distinct positions \((x_i, y_i)\). You can remove all pieces by performing a sequence of operations. In each operation, you perform the following steps:

  1. Select a cell \((x,y)\).
  2. If there is a piece at \((x,y)\), remove it; otherwise, end the current operation.
  3. Then, in order, check the cells \((x+1, y+1)\), \((x+1, y)\), and \((x+1, y-1)\). When you encounter the first cell among these that contains a piece, update \((x,y)\) to that cell's coordinates and return to step 2. If none of these cells contain a piece, the current operation ends.

Your task is to determine the minimum number of operations required to remove all the pieces.

Note: Each operation follows a deterministic path based on the order of checking the cells. Therefore, if a piece can be reached from another piece through this process, you do not need to start a separate operation for it.

The answer is equal to the number of pieces that cannot be reached from any other piece following the rules; that is, pieces that serve as starting points for their removal chains. More formally, a piece at \((x,y)\) can be removed as part of a chain if there exists a piece that can visit it according to the following conditions:

  • If \((x-1, y-1)\) contains a piece, then that piece will remove the current one (since \((x-1)+1, (y-1)+1 = (x, y)\)).
  • If \((x-1, y)\) contains a piece and \((x, y+1)\) is empty, then the piece at \((x-1, y)\) will remove the current piece.
  • If \((x-1, y+1)\) contains a piece and both \((x, y+1)\) and \((x, y+2)\) are empty, then the piece at \((x-1, y+1)\) will remove the current piece.

You need to count the pieces for which none of the above conditions hold; these pieces are the sources of the removal chains.

inputFormat

The first line contains a single integer \(n\) \((1 \leq n \leq 10^5)\), the number of pieces.

Each of the following \(n\) lines contains two integers \(x_i\) and \(y_i\) \( (|x_i|,|y_i| \leq 10^9)\) representing the coordinates of a piece. All pieces are at distinct positions.

outputFormat

Output a single integer: the minimum number of operations required to remove all pieces.

sample

3
1 1
2 2
2 1
2

</p>