The Snail Wants to Rest in the Shade
Time limit1sMemory limit512 MB
Given a rectilinear polygon lit by 45-degree sunlight from the upper right, find the total length of shaded surface where a snail can avoid the sun.
- Level
Hard8 of 10
- Topics
- Geometry, Sorting, Simulation
- Solved
- No attempts yet
Problem

A rectilinear polygon-shaped structure stands in a two-dimensional world. Its surfaces are parallel to the -axis or the -axis.
The sun is very far away, so light always enters at 45 degrees from the upper right toward the lower left, regardless of distance or position.
A snail is very small and can be represented as a point. The snail crawls on the surface of the structure (including its ceiling walls) or on the ground (). Some places are shaded because they receive no light.
The snail wants to avoid the sunlight in the shade. For the snail, find the total length of shade where it can avoid the sunlight.
Input
The first line contains the number of vertices of the structure ().
Each of the next lines contains the coordinates of a vertex of the structure, (, ), in order. The vertices are given in clockwise order, and the segment connecting the -th and the -th points forms one surface ().
and are always , and every other is greater than . Also, .
The segments representing the surfaces do not cross each other except at their endpoints. Each segment is parallel to the -axis or the -axis. If two segments meet at an endpoint, they are perpendicular.
Output
On the first line, print the total length of the shade where the snail can avoid the sunlight.