Earth and The Meteorites
Practice
3.8 (191 votes)
Approved
Easy
Ready
Sorting
Problem
50% Success 4527 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Once upon a time, the Earth was a flat rectangular landmass. And there was no life. It was then that the sky lit up with meteorites falling from out of space. Wherever they fell on the planet, a river was born, which flowed in all 4 directions (North, East, West, South), till the waters reached the edge of the Earth and simply fell off into space.

Now, these rivers criss-crossed and divided the one huge landmass (Pangaea) into many smaller landmasses. Now the lifeless (there was no life, remember?), want to know the number of landmasses on the planet after all the meteorites have fallen. They also want to know the area of the smallest and largest landmass. Can you help the lifeless in this question?

Input:

First line contains T which is the number of test cases.

First line of every test case contains 3 integers N, M, Q where N and M are coordinates of the bottom right corner of the planet and Q is the number of meteorites.
The next Q lines contains the coordinates X, Y where each of the meteorites fell.

Output:

For each test case, output a line containing 3 integers indicating the number of regions, the minimum area and the maximum area.

Constraints:

  • 1 ≤ T ≤ 10
  • 2 ≤ N, M ≤ 105
  • 0 ≤ Q ≤ 105
  • 1 ≤ X ≤ N
  • 1 ≤ Y ≤ M
  • 0 ≤ sum of Q over all test cases ≤ 105

Scoring:

  • 2 ≤ N, M ≤ 10, 0 ≤ Q ≤ 10 : (30 pts)
  • 2 ≤ N, M ≤ 1000, 0 ≤ Q ≤ 1000 : (30 pts)
  • Original Constraints : (40 pts)

Note:

  • The Earth can be assumed to be a rectangle with top left point as (1,1) and bottom right point (N,M).
  • More than one meteorite may have landed at some point.
  • The rivers may be assumed to be flowing in very thin straight lines parallel to the edges of the planet.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
22 votes
Tags:
ApprovedData StructuresEasyOpenSorting
Points:20
31 votes
Tags:
AlgorithmsEasyQuick Sort
Points:20
272 votes
Tags:
Binary SearchEasyOpenSorting