Input
4 2
0 1 2 2
2 4 4 5
Output
0
Explanation:
Here, Bob will first fall on the second prop, and he will travel till endpoint 2. Then he will fall on the first prop and travel till the endpoint 0. hence the final position will be X = 0.
The first line contains a single integer 'T' denoting the number of tests cases to be run. Then the test cases follow.
The first line of each test case contains two space-separated integers - initial X coordinate and N - number of segment props.
The next N lines of each test case describe the prop each by four integers x1, y1, x2, y2 - denoting the coordinates of the leftmost and rightmost point of the prop, respectively.
Props do not intersect or touch each other.
For each test case, print an integer βAβ denoting the final
coordinate
of Bob.
Answers for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken
care of. Just implement the function and return the answer.
1 <= T <= 50
1 <= N <= 100
0 <= X <= 104
0 <= X1, Y1, X2, Y2 <= 10000
x1 < x2
Time Limit: 1 sec.
Let us see the process described in the statement. First, we can say that Bobβs Y-coordinate is at infinity and check if there is some prop behind. If there are none of them - this X-position is final for him. Otherwise, he will fall on some prop below, roll down from one of its ends, and have a similar problem.
Let us check every prop. For every prop, we can check that it is entirely to the left from us or entirely to the right from us; if it is - this means that for sure the given object is not below. If the projection of our point on the X-axis lies on the projection of an object on the X-axis - let's find the Y-coordinate of the segment at the given X-coordinate.
If this coordinate is more significant than the X-coordinate of our point - it means that the segment is actually above us (even if the lowest endpoint has a smaller Y-coordinate than our point).
Otherwise, it is below us - but it is possible that we will not fall on this segment because there is some other segment between us.
Let us pick among all segments below us one with the largest Y-coordinate at a given X-coordinate - Bob will fall on this segment.
Algorithm