problems / 190

Suki's Treasure Bounding Box

easy matrix simulation 1400 ms · 256 MB

Suki's Treasure Bounding Box

Suki has buried treasures at various locations in her n×m garden grid. She wants to fence off the smallest axis-aligned rectangular region that contains all the treasures. Given the coordinates of all treasures, find the bounding box—the rectangle with the minimum and maximum row and column indices.

Input

The first line contains three integers n, m, and k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ n·m): the height and width of the garden grid, and the number of treasures buried.

The next k lines each contain two integers r and c (0 ≤ r < n, 0 ≤ c < m): the row and column coordinates of each treasure.

All treasure locations are distinct.

Output

Print four space-separated integers: min_row, max_row, min_col, max_col, representing the bounds of the bounding box that contains all treasures (inclusive).

Notes

The bounding box is the smallest axis-aligned rectangle with sides parallel to the grid edges that contains every treasure location.

Example 1

Input

5 5 3
0 0
4 4
2 2

Output

0 4 0 4

Example 2

Input

10 10 1
5 7

Output

5 5 7 7

Hints · reveal one at a time if you're stuck

Hint 1

The box depends only on the smallest and largest row and column among the treasures. Find those four values in a single pass, starting from the first treasure rather than from 0.

report an issue Prefer your own editor? sooks prep 190 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.