Baltic OI '10 P1 - BEARs

View as PDF

Submit solution


Points: 15
Time limit: 1.0s
Memory limit: 64M

Problem type
Baltic Olympiad in Informatics: 2010 Day 1, Problem 1

The Infinite city is divided into unitary square blocks by an infinite number of south-north and west-east two-way streets. One of the south-north streets is labeled 0, and the street numbers increase to the east and decrease to west. Similarly, one of the west-east streets is labeled 0, and the numbers increase to the north and decrease to the south.

Every intersection is labeled by an ordered pair of numbers of the streets that intersect (the first being the number of south-north street). Some street sections are more important and are called main streets.

One day sheriff Wolf (the fiercest caretaker of the Infinite city) is patrolling the streets and at intersection (A, B) he notices a car with a few members of the well-known BEAR gang. Wolf has heard of the BEARs' plans to break into the city's Honey Warehouse, which is located near the intersection (0, 0), and decides to stop them.

However, they haven't committed any crime so far and Wolf can't arrest them. But he has the authority to stop his car at any intersection and block exactly one of the four unit segments that meet at this intersection. However he can't block a unit segment that belongs to a main street.

So Wolf decides to pursue the BEARs and just before they reach an intersection, he may overtake their car and block one of the four unit segments at the intersection. The BEARs will be able to drive into the intersection, but they won't be able to exit the intersection to a segment blocked by the sheriff's car.

The sheriff wants to keep the BEARs as far away from the Honey Warehouse as possible. Find the maximum distance D, such that any intersection (x, y) the BEARs may reach satisfies the condition \max(|x|, |y|) \ge D.

Constraints

|A|, |B| \le 10^6

0 \le N \le 500

Input Specification

The first line of input contains two integers A and B, the starting point of the BEARs. The second line contains one integer N, the number of main streets. Each of the following N lines contains four integers: X_1, Y_1, X_2, Y_2 (|X_i|, |Y_i| \le 10^6), meaning that the street section between intersections (X_1, Y_1) and (X_2, Y_2) is a main street. Either X_1 = X_2 or Y_1 = Y_2 holds.

Output Specification

Output the maximum value of D.

Sample Input

3 3
3
1 0 3 0
0 0 0 3
3 0 3 1

Sample Output

1

Explanation

The following figure illustrates how the BEARs can get to within distance 1 of the warehouse:


Thicker black segments represent main streets. The sheriff cannot block unit segments belonging to main streets.

Even though the BEARs may continue trying forever, the sheriff can prevent them from ever getting closer to the warehouse.


Comments

There are no comments at the moment.