Matrix
Practice
4.5 (14 votes)
Algorithms
Breadth first search
Graphs
Medium
Problem
83% Success 7898 Attempts 30 Points 2s Time Limit 512MB Memory 1024 KB Max Code

All team is in the Matrix and need your help to escape!

Matrix is an infinite discrete world of square cells \((x, y)\). There are some infinite height buildings and you have all records about them. The record \((x, y)\) means that building occupies all cells \((x', y'): x' = x, y' \leq y\). In other words, building is a one cell width strip which goes infinitely down. All buildings have pairwise distinct x coordinates.

There are q team members. For each team member you know his current cell and finish cell in which there is his exit from Matrix. Note that each team member should use his own exit and can't use exit of some other team member.

In one second each team member can go from cell \((x, y)\) to any of cells \((x + 1, y), (x - 1, y), (x, y - 1), (x, y + 1)\) if corresponding cell is not occupied by building. Once a person reaches cell with his exit, he can exit from the Matrix instantly.

For each team member you have to find minimum time which he needs to exit from the Matrix.

Input format

The first line of input contains two space separated integers n and q (\(1 \leq n, q \leq 3 \cdot 10^5\)) - the number of buildings and the number of team members.

Next n lines contain records about buildings. The i-th of them contains two space separated integers \(x_i\) and \(y_i\) (\(-10^9 \leq x_i, y_i \leq 10^9\)) - the parameters of the i-th building.

Next q lines contain information about team members. The i-th of them contains four space separated integers \(sx_i\), \(sy_i\), \(fx_i\) and \(fy_i\) (\(-10^9 \leq sx_i, sy_i, fx_i, fy_i \leq 10^9\)) - the current cell of the i-th team member and the cell where his exit is located. It is guaranteed that neither \((sx_i, sy_i)\) nor \((fx_i, fy_i)\) belong to any building.

Output format

Print q lines. On the i-th of them print the single integer - the required time for escape for the i-th team member.

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:30
21 votes
Tags:
AlgorithmsBreadth First SearchGraphs
Points:30
7 votes
Tags:
AlgorithmsBinary SearchBreadth First SearchGraphsImplementationMedium
Points:30
34 votes
Tags:
AlgorithmsBreadth First SearchGrammar-VerifiedGraphsMediumQueue