Golf
Time limit5sMemory limit1024 MB
Find the fewest axis-parallel hits needed to move a golf ball from (S, T) to (U, V) without passing through any rectangular obstacle interior.
- Level
Medium7 of 10
- Topics
- BFS, Graph, Segment tree
- Solved
- No attempts yet
Problem
JOI-kun is a boy practicing golf on a special golf course.
The course is a plane with -coordinates. It contains obstacles. The -th obstacle () occupies the rectangular region whose -coordinates are between and (inclusive) and whose -coordinates are between and (inclusive). Two obstacles, including their boundaries, do not intersect.
The start point is and the end point is . The two points are different, and neither is contained in any obstacle or on any obstacle boundary. At the beginning, the golf ball is placed at the start point.
JOI-kun hits the ball and moves it any distance in one of the four directions parallel to the coordinate axes. The track of the ball must not touch the interior of any obstacle. The ball may pass through obstacle boundaries, and it may stop on a boundary. From a stopped position, the ball can be hit in any direction that has no obstacle immediately blocking it.
Find the minimum number of hits needed to move the ball from the start point to the end point.
Input
The first line contains four integers , , , , separated by spaces. The second line contains . Each of the next lines contains four integers , , , , separated by spaces.
Output
Print the minimum number of hits on one line.
Constraints
- for every
- for every
- Obstacles do not intersect each other, including their boundaries.
- The start and end points are not contained in any obstacle or on any obstacle boundary.