Intersection of Two Prisms
Time limit1sMemory limit128 MB
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 is a prism of infinite height whose axis is parallel to the -axis, and is also a prism of infinite height whose axis is parallel to the -axis. is defined by the polygon , which is the cross section of and the -plane, and is defined by the polygon , which is the cross section of and the -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.

: cross section of and the -plane (left). : cross section of and the -plane (right).
Figure I.1: Cross sections of the prisms.

and (left). and (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 and .
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 .
Each dataset is formatted as follows.
m n
x11 y11
x12 y12
...
x1m y1m
x21 z21
x22 z22
...
x2n z2n
and are integers (, ) that represent the numbers of vertices of the polygons and , respectively.
, , and are integers between and , inclusive. and are the positions of the -th and -th vertices of and , respectively.
The sequences of these vertex positions are given in counterclockwise order, either on the -plane or the -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 and 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 and and share no common factor greater than (an integer volume is written as V/1, and a volume of zero is written as 0/1). Do not print any other characters.