Intersection of Two Prisms

Time limit1sMemory limit128 MB

Summary
Compute the exact rational volume of the intersection of two infinite convex prisms whose axes are perpendicular, given their polygonal cross sections.
Level

Hard8 of 10

Topics
Geometry, Math, Simulation
Solved
No attempts yet

Problem

Suppose that P1P_1 is a prism of infinite height whose axis is parallel to the zz-axis, and P2P_2 is also a prism of infinite height whose axis is parallel to the yy-axis. P1P_1 is defined by the polygon C1C_1, which is the cross section of P1P_1 and the xyxy-plane, and P2P_2 is defined by the polygon C2C_2, which is the cross section of P2P_2 and the xzxz-plane.

Figure I.1 shows two cross sections that appear as the first dataset in the sample input, and Figure I.2 shows the relationship between the prisms and their cross sections.

C1C_1: cross section of P1P_1 and the xyxy-plane (left). C2C_2: cross section of P2P_2 and the xzxz-plane (right).

Figure I.1: Cross sections of the prisms.

P1P_1 and C1C_1 (left). P2P_2 and C2C_2 (right).

Figure I.2: Prisms and their cross sections.

Figure I.3: Intersection of the two prisms.

Figure I.3 shows the intersection of the two prisms in Figure I.2, namely P1P_1 and P2P_2.

Write a program that calculates the volume of the intersection of the two prisms.

Input

The input is a sequence of datasets. The number of datasets is less than 200200.

Each dataset is formatted as follows.

m n
x11 y11
x12 y12
...
x1m y1m
x21 z21
x22 z22
...
x2n z2n

mm and nn are integers (3≤m≤1003 \le m \le 100, 3≤n≤1003 \le n \le 100) that represent the numbers of vertices of the polygons C1C_1 and C2C_2, respectively.

x1ix_{1i}, y1iy_{1i}, x2jx_{2j} and z2jz_{2j} are integers between −100-100 and 100100, inclusive. (x1i,y1i)(x_{1i}, y_{1i}) and (x2j,z2j)(x_{2j}, z_{2j}) are the positions of the ii-th and jj-th vertices of C1C_1 and C2C_2, respectively.

The sequences of these vertex positions are given in counterclockwise order, either on the xyxy-plane or the xzxz-plane, as in Figure I.1.

You may assume that all the polygons are convex; that is, all interior angles of each polygon are less than 180 degrees. You may also assume that all the polygons are simple; that is, each polygon's boundary neither crosses nor touches itself.

The end of the input is indicated by a line containing two zeros.

Output

For each dataset, output the exact volume of the intersection of the two prisms P1P_1 and P2P_2 on its own line. Because all vertex coordinates are integers, this volume is always a rational number. Print it as a fraction p/q in lowest terms, where q>0q > 0 and pp and qq share no common factor greater than 11 (an integer volume VV is written as V/1, and a volume of zero is written as 0/1). Do not print any other characters.

Examples1

  1. Example 1

    Input
    4 3
    7 2
    3 3
    0 2
    3 1
    4 2
    0 1
    8 1
    4 4
    30 2
    30 12
    2 12
    2 2
    15 2
    30 8
    13 14
    2 8
    8 5
    13 5
    21 7
    21 9
    18 15
    11 15
    6 10
    6 8
    8 5
    10 12
    5 9
    15 6
    20 10
    18 12
    3 3
    5 5
    10 3
    10 10
    20 8
    10 15
    10 8
    4 4
    -98 99
    -99 -99
    99 -98
    99 97
    -99 99
    -98 -98
    99 -99
    96 99
    0 0
    
    Expected output
    113/24
    1680/1
    9823/20
    0/1
    2994501843/394