#P4671. [BalticOI 2011] Polygon (Day2)

    ID: 5411 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>2011Special JudgeBalticOI(波罗的海)

[BalticOI 2011] Polygon (Day2)

题目描述

A simple polygon with NN vertices is drawn on an infinite rectangular grid. For such a polygon, only neighboring edges touch at their common vertex; no other of its edges intersect or touch. All vertices of the polygon lie on grid points, i.e., vertices have integer coordinates.

Your task is to find the total length of grid line segments which lie strictly inside the given polygon (these line segments are highlighted in the drawings below).

输入格式

The first line of input contains a single integer NN, the number of vertices of the polygon. Each of the following NN lines contains two integers xx and yy, the coordinates of one vertex. The vertices are given either in clockwise or counterclockwise order. All vertices are distinct, but more than two consecutive vertices may lie on a line.

输出格式

The only line of output must contain a decimal number: the total length of grid line segments which lie strictly inside the given polygon.

3
5 1
2 4
1 1
10.0
5
0 0
-2 2
-2 -1
2 -2
2 0
12.5

提示

Sample Explanation 1

The length of horizontal lines is 43+83=4\frac{4}{3} + \frac{8}{3} = 4. The length of vertical lines is 3+2+1=63 + 2 + 1 = 6. The total length is 4+6=104 + 6 = 10.

Sample Explanation 2

The length of horizontal lines is 1+2+4=71+2+4 = 7. The length of vertical lines is 94+32+74=5.5\frac{9}{4}+\frac{3}{2}+\frac{7}{4} = 5.5. The total length is 7+5.5=12.57 + 5.5 = 12.5.

数据范围

对于 50%50\% 的数据,多边形所有的边均在网格线上。

对于所有数据,$3 \le N \le 10^5,-5 \times 10^8 \le x,y \le 5 \times 10^8$。