Intelligence in Perpendicularia
Time limit3sMemory limit512 MB
Given an orthogonal simple polygon, find the total length of wall that an observer looking along a principal axis direction cannot see.
- Level
Medium7 of 10
- Topics
- Geometry, Implementation, Sorting, Array
- Solved
- No attempts yet
Problem
Perpendicularia has only two directions, vertical and horizontal. The government is going to build a new intelligence service facility. Several plans are on the table, and for each of them the government wants the total secured perimeter.
The total secured perimeter is the total length of the facility walls that an outside observer looking along an axis direction cannot see. A point of a wall is visible when the ray that leaves that point perpendicular to the wall, pointing away from the facility, never meets the facility again. Every other point of a wall is invisible.
The figure below shows one of the plans, with its secured perimeter drawn in bold.

Write a program that calculates the total secured perimeter of the given plan.
Input
The plan of the facility is given as a polygon.
The first line contains one integer , the number of vertices of the polygon (). Each of the next lines contains two integers and , the coordinates of the -th vertex (). The vertices are listed in order along the boundary.
All vertices are distinct, and no vertex lies on another edge. Every edge is either vertical () or horizontal (), and two edges that are not neighbors never meet.
Output
Print the total secured perimeter as a single integer.