This page is still under construction.

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

Grid Panel

Time limit1sMemory limit128 MB

Summary
Given a panel with holes, find the smallest rectilinear convex region that covers every cell next to a hole plus one full row or column.
Level

Hard8 of 10

Topics
Geometry, Brute force
Solved
No attempts yet

Problem

A factory produces grid panels. Sometimes a freshly made panel is faulty: it has holes located at grid points of the panel. Workers gather every faulty panel, cut out the parts that contain holes, and replace them with flawless panel pieces. Each cut must follow grid lines and remove one connected region that contains every grid cell adjacent to a hole. The connected region has to satisfy all of the following conditions:

  • (i) It contains every grid cell adjacent to each hole.
  • (ii) It contains every grid cell of one row or one column of the panel, chosen as the base cutting strip.
  • (iii) It is a rectilinear convex polygon.
  • (iv) Among all rectilinear polygons that satisfy (i), (ii), and (iii), its area is minimum.

A polygon is a rectilinear polygon if its boundary consists only of horizontal and vertical segments. A rectilinear polygon is a rectilinear convex polygon if its intersection with every horizontal line and every vertical line is either empty or a single segment.

For example, consider the 8×78 \times 7 panel with 66 holes in Figure 1(a). If the fourth row from the bottom is chosen as the base cutting strip, the removed connected region has 2929 grid cells (Figure 1(b)). If the fourth column from the left is chosen instead, the removed connected region has 2727 grid cells (Figure 1(c)), which is the smallest possible rectilinear convex polygon.

Figure 1

Given the size of a panel and the positions of its holes, compute the smallest rectilinear convex polygon that satisfies the conditions above. Because each grid cell has area 11, the area of the rectilinear convex polygon in Figure 1(c) is 2727.

Input

Read from standard input. The first line contains the number of test cases TT. Each test case is given as follows.

The first line of a test case contains two integers ww and hh (2≤w,h≤500002 \le w, h \le 50000), the width and the height of the panel. The next line contains an integer nn (1≤n≤10001 \le n \le 1000), the number of holes. Each of the following nn lines contains two integers xx and yy (0≤x≤w0 \le x \le w, 0≤y≤h0 \le y \le h), the coordinates of a hole. The lower-left corner of the panel is the origin of the coordinate system. Integers on the same line are separated by a single space.

Output

Write to standard output. For each test case, print exactly one line containing one integer: the area of the rectilinear convex polygon that minimally covers all holes on the panel.

Examples4

  1. Example 1

    Input
    1
    8 7
    6
    2 2
    3 1
    8 3
    5 5
    4 6
    3 4
    
    Expected output
    27
    
  2. Example 2

    Input
    3
    4 4
    1
    2 2
    8 7
    6
    2 2
    3 1
    8 3
    5 5
    4 6
    3 4
    12 10
    15
    2 7
    3 8
    4 6
    4 7
    5 5
    5 7
    6 4
    6 5
    7 3
    7 5
    8 2
    8 3
    9 4
    9 5
    10 3
    
    Expected output
    6
    27
    44
    
  3. Example 3

    Input
    1
    4 4
    1
    2 2
    
    Expected output
    6
    
  4. Example 4

    Input
    1
    5 5
    1
    0 0
    
    Expected output
    5