Rectilinear Regions
Time limit0.5sMemory limit512 MB
Given two unbounded staircase polylines L and U, count the closed regions they enclose with L below and U above, and sum their areas.
- Level
Hard8 of 10
- Topics
- Geometry, Two pointers, Sorting, Implementation
- Solved
- No attempts yet
Problem
A rectilinear path joining two points in the plane is a path made only of horizontal and vertical segments. A rectilinear path is monotone with respect to the axis (respectively, the axis) if its intersection with every vertical line (respectively, every horizontal line) is either empty or one contiguous piece of that line. A rectilinear path that is monotone with respect to both the axis and the axis is a staircase. A staircase is unbounded if both of its ends are semi-infinite horizontal segments, that is, the left end runs to infinity in the negative direction and the right end runs to infinity in the positive direction. A staircase is increasing or decreasing depending on whether its coordinate grows or shrinks as you walk along it from left to right. A staircase with vertical segments is called an step staircase.
Two unbounded staircases and can bound several closed rectilinear regions, or none at all. Some of those regions are bounded below by and above by . In Figure G.1 the two regions colored yellow are of that kind. This problem asks for the number of such regions and their total area. A part that stretches to infinity on one side is not a closed region, so it is not counted.

Figure G.1. An example where staircase has 3 steps and staircase has 4 steps. The two yellow regions are closed rectilinear regions bounded below by and above by . The values (respectively ) are the and coordinates of the corner points of staircase (respectively ).
A staircase is written with the notation
where are the coordinates of the vertical segments and are the coordinates of the horizontal segments. An increasing staircase has and a decreasing staircase has . Here is the height of the left semi-infinite horizontal segment and is the height of the right one.
All coordinates of the corner points of and that appear in notation (1) are distinct, and all coordinates of those corner points are distinct as well. Given the two unbounded staircases and , compute the number of closed rectilinear regions bounded below by and above by , together with their total area.
Input
The first line contains two positive integers and , the number of steps of the unbounded staircases and . (, )
The second line contains integers describing the corner points of staircase , and the third line contains integers describing the corner points of staircase , both in the order of notation (1). Every coordinate is a non-negative integer at most .
Output
Print two integers and on one line, separated by a space. Here is the number of closed rectilinear regions bounded below by staircase and above by staircase , and is their total area. If there is no such region, print 0 for both and .