This page is still under construction.

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

Handbags

Time limit11sMemory limit512 MB

Summary
A grid of villages sets prices from s source prices; report the maximum price and how many villages reach it.
Level

Hard8 of 10

Topics
Shortest path, Graph, Math
Solved
No attempts yet

Problem

Handbags are a popular souvenir everywhere in the city of Manhattanila, because the city makes them itself.

Manhattanila has (a+1)(b+1)(a+1)(b+1) villages, locally called barangays, laid out neatly in an (a+1)×(b+1)(a+1) \times (b+1) rectangular lattice. A village is named by two integers (x,y)(x, y) with 0≤x≤a0 \le x \le a and 0≤y≤b0 \le y \le b, its row number and its column number, both counted from 0.

Exactly ss of the villages make handbags. Call these the source villages. Every other village buys its handbags from a neighboring village. Two villages are neighbors when they share a boundary, so a village has up to four neighbors, one in each cardinal direction. A neighbor need not be a source village, so it may buy its own supply from another neighbor, and so on.

A source village makes handbags at a fixed price of its own. A village that buys from a neighbor selling at pp pesos sells at p+1p+1 pesos. Every village pays as little as it can: it buys from the neighbor that sells cheapest, and a source village sells at its own price or at its cheapest neighbor's price plus 1, whichever is smaller. These rules give every village exactly one price.

A tourist comes to Manhattanila for souvenirs (pasalubong). To impress her friends she plans to say that she bought her handbags in the village where they cost the most, while she quietly buys them in the village where they cost the least. Answer her question: what is the highest price of a handbag in the city, and how many villages sell at that price?

Input

The first line contains TT, the number of test cases.

The first line of each test case contains three integers aa, bb and ss, where ss is the number of source villages. Each of the next ss lines contains three integers xx, yy and pp, meaning that village (x,y)(x, y) is a source village and sells its handbags at pp pesos. No pair (x,y)(x, y) appears twice in one test case.

Constraints

  • 1≤T≤500001 \le T \le 50000
  • 0≤a,b≤1060 \le a, b \le 10^6
  • 0≤x≤a0 \le x \le a
  • 0≤y≤b0 \le y \le b
  • 1≤s≤301 \le s \le 30
  • 1≤p≤1091 \le p \le 10^9
  • The sum of ss over all test cases is at most 5000050000.

Output

For each test case, print one line with two integers separated by a space:

  • the highest price of a handbag in the city, and
  • the number of villages that sell at that price.

Examples2

  1. Example 1

    Input
    3
    5 6 2
    0 0 5
    5 6 1
    10 10 4
    0 10 5
    1 6 8
    2 3 2
    5 10 5
    10 20 2
    0 0 5
    10 20 16
    
    Expected output
    8 9
    13 3
    25 21
    
  2. Example 2

    Input
    1
    0 0 1
    0 0 7
    
    Expected output
    7 1