Sweeping Robot

Time limit1sMemory limit128 MB

Summary
Count the distinct grid cells a robot sees sideways from each unit of its grid path inside an orthogonal polygonal museum, with walls blocking the view.
Level

Medium4 of 10

Topics
Simulation, Geometry, Matrix
Solved
No attempts yet

Problem

A robot with a camera on it sweeps some areas of a museum. The museum is a polygon whose walls are all horizontal or vertical, drawn on a mesh of 1×11 \times 1 cells as in the figure, and every vertex of the polygon lies on a mesh vertex.

The robot also moves only along mesh edges, either horizontally or vertically. Its camera sweeps the two directions perpendicular to the direction of travel. While the robot moves horizontally the camera points vertically, so it sees only cells to the north and to the south of that horizontal part of the path. While the robot moves vertically it sees only cells to the east and to the west of that vertical part of the path. Walls stop the view. Cut the path into pieces of length 1, and from each piece the camera sees, in both perpendicular directions, every cell up to the first wall.

The figure shows a polygon together with a path of the robot (the dashed line). The dotted squares are the ones the robot sees.

Given such a polygon and a robot path inside it, compute the total area (total number of squares) the robot sees. A cell seen more than once counts once.

Input

The input has several test cases. Each test case starts with a line holding two integers nn and kk (2≤n,k≤1002 \le n, k \le 100), where nn is the number of walls (or vertices) of the museum and kk is the number of vertices of the robot path.

The next nn lines describe the museum vertices. The iith of these lines holds two space separated non-negative integers xix_i and yiy_i, neither exceeding 500, the xx and yy coordinates of the iith vertex of the museum. There is a wall between vertex ii and vertex i+1i+1, and the (n+1)(n+1)th vertex is the first vertex.

The next kk lines describe the vertices of the robot path, in the order they appear on the path from the starting point to the ending point. Each of these lines holds the xx and yy coordinates of a vertex. The path lies inside the museum. Its vertices, but not its edges, may touch the museum walls. The path may cross itself.

The last line of the input is 0 0, which you should not process.

Output

For each test case, print on its own line the total area (total number of squares) the robot sees.

Examples1

  1. Example 1

    Input
    20 4
    0 2
    1 2
    1 1
    2 1
    2 2
    3 2
    3 0
    4 0
    4 3
    5 3
    5 4
    4 4
    4 5
    3 5
    3 6
    2 6
    2 5
    1 5
    1 3
    0 3
    2 3
    2 4
    3 4
    3 2
    0 0
    
    Expected output
    10