#P7027. Perpendicular Security

    ID: 20234 Type: Default 1000ms 256MiB

Perpendicular Security

Perpendicular Security

The government of Perpendicularia needs to evaluate several facility plans for a top‐secret service center. In Perpendicularia everything is built along the two perpendicular directions: vertical and horizontal. In a given plan the facility is represented as a simple orthogonal polygon (its edges are parallel to the coordinate axes). An outside observer may view the facility only perpendicularly (from north, south, east, or west). The visible perimeter (for a given direction) consists of those wall segments that are the first encountered along the viewing direction. In other words, if one projects all wall segments along one of the four cardinal directions, only the parts that are extremal (first hit) will be visible.

The secured perimeter is defined as the total length of facility walls that remain invisible to any perpendicularly–looking observer. Equivalently, if P is the total perimeter of the facility and V is the total visible length when combining the views from all four sides, then the secured perimeter is $$ S = P - V. $$ All formulas in this statement (when needed) are expressed in \( \LaTeX \) format.

You are given the facility plan as an orthogonal polygon. The vertices of the polygon are given in order (either clockwise or counterclockwise) and the edges alternate between horizontal and vertical. Your task is to compute the secured perimeter.

Note: To determine whether a wall is visible from a given direction the following process is used. First, determine the exterior side (the side opposite to the interior) for each wall segment. For a vertical wall, if it is traversed upward then its exterior is to the right if the polygon is counterclockwise and to the left if the polygon is clockwise; if traversed downward then the exterior is to the left (for counterclockwise) or to the right (for clockwise). For a horizontal wall, if it is traversed to the right then its exterior is to the south in a counterclockwise polygon and to the north in a clockwise polygon; if traversed to the left then its exterior is to the north (for counterclockwise) or to the south (for clockwise).

For each set of wall segments with the same orientation and exterior side (vertical walls facing west or east, horizontal walls facing north or south), imagine sweeping a line (horizontal for vertical walls and vertical for horizontal walls) across the extent of these segments. In each sweep band, the only segment visible is that whose coordinate (\(x\) for vertical walls, \(y\) for horizontal walls) is extremal (smallest for walls seen from the west or south and largest for walls seen from the east or north). The sum of the lengths of all visible portions over the four directions is \(V\).

inputFormat

The first line contains a single integer \(n\) (\(4 \le n \le 10^5\)), the number of vertices of the polygon. Each of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(|x_i|,|y_i| \le 10^9\)), the coordinates of the vertices given in order (either clockwise or counterclockwise). It is guaranteed that the polygon is simple and its edges are either horizontal or vertical.

outputFormat

Output a single integer, the total secured perimeter of the facility. (Note that the answer is \(S = P - V\) as explained above.)

sample

4
0 0
4 0
4 3
0 3
0