#P5816. Spreading Black on an Infinite Grid

    ID: 19044 Type: Default 1000ms 256MiB

Spreading Black on an Infinite Grid

Spreading Black on an Infinite Grid

In an infinite square grid (with vertices at integer coordinates), there are initially n black vertices and all the other vertices are white. Every second, all internal white points turn black simultaneously, and this process continues until no internal white point remains. A white vertex P(x,y) is called an internal white point if and only if both of the following conditions hold:

  • There exist two black vertices on the same horizontal line as P, one strictly to its left and one strictly to its right. In other words, there exist x1 and x2 with x1 < x < x2 such that \(P(x1,y)\) and \(P(x2,y)\) are black.
  • There exist two black vertices on the same vertical line as P, one strictly below and one strictly above. That is, there exist y1 and y2 with y1 < y < y2 such that \(P(x,y1)\) and \(P(x,y2)\) are black.

Your task is to compute the total number of black vertices in the final configuration after the process stops. Note that once a vertex becomes black, it remains black forever.

Note: Any formulas are written in LaTeX format. For example, the horizontal condition is: $$\exists\; x_1,x_2 \text{ such that } x_1 < x < x_2 \text{ and } P(x_1,y),\; P(x_2,y) \text{ are black.}$$

inputFormat

The first line contains an integer n representing the number of initially black vertices. Each of the following n lines contains two space‐separated integers x and y representing the coordinates of a black vertex.

outputFormat

Output a single integer which is the total number of black vertices in the grid after the process stops.

sample

4
0 0
2 0
0 2
2 2
4