This page is still under construction.

Parts of this page are still being built. What you see may change.

The Snail Wants to Rest in the Shade

Time limit1sMemory limit512 MB

Summary
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 xx-axis or the yy-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 (y=0y = 0). 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 NN (4≤N≤200,0004 \leq N \leq 200,000).

Each of the next NN lines contains the coordinates of a vertex of the structure, (xi,yi)(x_i, y_i) (−109≤xi≤109-10^9 \leq x_i \leq 10^9, 0≤yi≤1090 \leq y_i \leq 10^9), in order. The vertices are given in clockwise order, and the segment connecting the ii-th and the (i+1)(i+1)-th points forms one surface (1≤i<N1 \leq i < N).

y1y_1 and yNy_N are always 00, and every other yiy_i is greater than 00. Also, x1<xNx_1 < x_N.

The segments representing the surfaces do not cross each other except at their endpoints. Each segment is parallel to the xx-axis or the yy-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.

Examples2

  1. Example 1

    Input
    12
    -8 0
    -8 3
    -5 3
    -5 2
    -2 2
    -2 7
    6 7
    6 5
    2 5
    2 2
    8 2
    8 0
    
    Expected output
    24
    
  2. Example 2

    Input
    6
    0 0
    0 500000000
    -500000000 500000000
    -500000000 1000000000
    500000000 1000000000
    500000000 0
    
    Expected output
    3000000000