Histograms

No attempts yetTime limit1sMemory limit256 MB

Problem

A histogram represents a data distribution. Here a histogram of width WW is given by an even-length sequence of points

(x0,y1),(x1,y1),(x1,y2),(x2,y2),,(xN/21,yN/2),(xN/2,yN/2)(x_0, y_1),(x_1, y_1),(x_1, y_2),(x_2, y_2),\ldots,(x_{N/2-1}, y_{N/2}),(x_{N/2}, y_{N/2})

in the plane. Each adjacent pair shares an xx or a yy coordinate, and horizontal and vertical segments alternate.

The shape must satisfy:

  1. x0=0x_0 = 0
  2. xN/2=Wx_{N/2} = W
  3. xi<xi+1x_i < x_{i+1}
  4. yi>0y_i > 0
  5. yiyi+1y_i \ne y_{i+1}

For histogram HH, let yH(x)y_H(x) be the height on unit interval x,x+1\langle x, x+1 \rangle. Two error measures are used:

diffcount(H,H)=x=0W1diff(yH(x),yH(x)),diff(y1,y2)=0 if y1=y2 else 1\text{diffcount}(H', H) = \sum_{x=0}^{W-1} \text{diff}(y_H(x), y_{H'}(x)),\quad \text{diff}(y_1,y_2)=0\text{ if }y_1=y_2\text{ else }1

abserror(H,H)=x=0W1yH(x)yH(x)\text{abserror}(H', H) = \sum_{x=0}^{W-1} |y_H(x) - y_{H'}(x)|

Given HH, a point set SS, and the measure GG, find a histogram HH' that uses only points from SS and minimizes the chosen error.

Input

Line 1: integers N,M,GN, M, G (2N1000002 \le N \le 100000, NN even, 2M1000002 \le M \le 100000, 1G21 \le G \le 2). G=1G=1 selects diffcount, G=2G=2 selects abserror.

Next NN lines: points (X,Y)(X, Y) defining histogram HH.

Next MM lines: points (X,Y)(X, Y) in set SS. (0X1060 \le X \le 10^6, 1Y1061 \le Y \le 10^6)

Output

Line 1: minimum error DD.

Line 2: even integer LL, the number of points in the optimal histogram.

Next LL lines: coordinates X YX\ Y of those points. The definition must satisfy every rule from the statement.