PICAD - Crime at Piccadily Circus

Sherlock Holmes is carrying out an investigation into the crime at Piccadily Circus. Holmes is trying to determine the maximal and minimal number of people staying simultaneously at the crime scene at a moment when the crime could have been commited. Scotland Yard has already carried out a thorough investigation already, interrogated everyone seen at the crime scene and determined what time they appeared at the crime scene and what time they left. Doctor Watson offered his help to process the data gathered by Scotland Yard and find the numbers interesting Sherlock Holmes, but he has some difficulties. Help him!

Task

Write a program which

  • reads from standard input the time interval during which the crime was commited and the data gathered by Scotland Yard,
  • finds the minimal and the maximal number of people present simultaneously in the time interval when the crime could have been commited, (these numbers can be zero, though it would seem strange that noone was present at the crime scene when the crime was commited, but that's the type of crime Holmes and Watson have to deal with)
  • writes the outcome to standard output.

Input

Ten test cases (given one under another, you have to process all!). The first line of each test case consists of two integer numbers p and k, 0<=p<=k<=100000000. These denote the first and the last moment when the crime could have been commited. The second line of each test case contains one integer n, 3<=n<=5000. This is the number of people interrogated by Scotland Yard. The next n lines consist of two integers - line i+2 contains numbers ai and bi separated by a single space, 0<=ai<=bi<=1000000000. These are the moments at which the i-th person apperared at and left the crime scene respectively. It means that the i-th person was at the crime scene for the whole time from moment ai until moment bi (inclusive).

Output

For every test case your program should write to the standard output only one line with two integers separated by a single space: the minimal and maximal number of people staying simultaneously at the crime scene, in the interval between moment p and k, (inclusive).

Example

Only one test case.

Input:
5 10
4
1 8
5 8
7 10
8 9

Output:
1 4


Added by:Adam Dzedzej
Date:2004-06-10
Time limit:13s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:Internet Contest Pogromcy Algorytmow (Algorithm Tamers)
Round IV, 2003

hide comments
2017-10-30 19:37:33
Will there always be 10 test cases?
2017-09-17 03:57:45
What a nightmare to get right. As of now, 3600 submissions out of 5600 total are WAs. I was struggling even after looking at somebody else's correct code :(

Understand the timestamps! Eg.:
[(5,6), (6,7), (8,9)] -> 2 ppl at 6:00 but 0 at 7:30
2016-04-17 17:31:33 minhthai
so apparently, you have to do it 10 times in a row. So..many..WAs :((
2015-06-16 22:05:40 Abhilash
comments helped
2015-03-05 14:36:06 Govind Lahoti
Nice problem
2014-03-20 04:41:53 André Balan [UFABC]


Last edit: 2014-03-20 17:42:07
2014-02-02 06:25:58 Ankit Chaudhary
tricky tc :
5 20
3
5 20
5 10
11 20
1 2
// at time b/w 10 and 11 let say 10.5 there is only one person. Time can take float value.


5 10
2
5 7
8 10
0 1
// no one is present at 7.5, so min=0

Question is not explained properly which cause me many WAs.
2013-01-13 10:45:13 Shubham Depp Bansal
i am not getting the meaning of input 0.how can a man gets in at 0 time and leavs at the end of 0..i mean how??
2010-12-27 14:30:59 The Bartender
Be careful:
0 <= p <= k <= 10^8
0 <= ai <= bi <= 10^9

2010-04-01 19:41:36 :D
In a case when one interval stars right after another, you should assume that there is some time in-between.

For example for this imput:
1 2
2
1 1
2 2

The answer is:
0 1

It could seem that "moments" are defined as blocks of time of length 1, but they actually just standard points on time axis.

Last edit: 2010-04-01 21:07:53
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.