This page is still under construction.

Parts of this page are still being built. What you see may change.

Rectilinear Regions

Time limit0.5sMemory limit512 MB

Summary
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 xx axis (respectively, the yy 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 xx axis and the yy 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 xx direction and the right end runs to infinity in the positive xx direction. A staircase is increasing or decreasing depending on whether its yy coordinate grows or shrinks as you walk along it from left to right. A staircase with nn vertical segments is called an nn step staircase.

Two unbounded staircases LL and UU can bound several closed rectilinear regions, or none at all. Some of those regions are bounded below by LL and above by UU. 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 UU has 3 steps and staircase LL has 4 steps. The two yellow regions are closed rectilinear regions bounded below by LL and above by UU. The values xiL,yiLx_i^L, y_i^L (respectively xiU,yiUx_i^U, y_i^U) are the xx and yy coordinates of the corner points of staircase LL (respectively UU).

A staircase is written with the notation

y0 x1 y1 x2 y2 … xn yn(1)y_0\ x_1\ y_1\ x_2\ y_2\ \dots\ x_n\ y_n \qquad (1)

where x1<x2<⋯<xnx_1 < x_2 < \dots < x_n are the xx coordinates of the vertical segments and y0,y1,…,yny_0, y_1, \dots, y_n are the yy coordinates of the horizontal segments. An increasing staircase has y0<y1<⋯<yny_0 < y_1 < \dots < y_n and a decreasing staircase has y0>y1>⋯>yny_0 > y_1 > \dots > y_n. Here y0y_0 is the height of the left semi-infinite horizontal segment and yny_n is the height of the right one.

All xx coordinates of the corner points of LL and UU that appear in notation (1) are distinct, and all yy coordinates of those corner points are distinct as well. Given the two unbounded staircases LL and UU, compute the number of closed rectilinear regions bounded below by LL and above by UU, together with their total area.

Input

The first line contains two positive integers nn and mm, the number of steps of the unbounded staircases LL and UU. (1≤n≤250001 \le n \le 25000, 1≤m≤250001 \le m \le 25000)

The second line contains 2n+12n+1 integers describing the corner points of staircase LL, and the third line contains 2m+12m+1 integers describing the corner points of staircase UU, both in the order of notation (1). Every coordinate is a non-negative integer at most 5000050000.

Output

Print two integers kk and ww on one line, separated by a space. Here kk is the number of closed rectilinear regions bounded below by staircase LL and above by staircase UU, and ww is their total area. If there is no such region, print 0 for both kk and ww.

Examples3

  1. Example 1

    Input
    4 3
    6 2 9 11 11 15 16 21 19
    3 6 12 10 14 18 17
    
    Expected output
    2 32
    
  2. Example 2

    Input
    4 3
    9 1 7 3 5 5 3 7 1
    0 2 2 4 4 6 6
    
    Expected output
    0 0
    
  3. Example 3

    Input
    1 1
    1 50000 50000
    0 0 49999
    
    Expected output
    1 2499900000