Labyrinth Adventures
Contest: Educational Codeforces Round 129 (Rated for Div. 2)
Difficulty rating: 2600
Tags: data structures, dp, matrices, shortest paths

You found a map of a weirdly shaped labyrinth. The map is a grid, consisting of $$$n$$$ rows and $$$n$$$ columns. The rows of the grid are numbered from $$$1$$$ to $$$n$$$ from bottom to top. The columns of the grid are numbered from $$$1$$$ to $$$n$$$ from left to right.

The labyrinth has $$$n$$$ layers. The first layer is the bottom left corner (cell $$$(1, 1)$$$). The second layer consists of all cells that are in the grid and adjacent to the first layer by a side or a corner. The third layer consists of all cells that are in the grid and adjacent to the second layer by a side or a corner. And so on.

The labyrinth with $$$5$$$ layers, for example, is shaped as follows:

The layers are separated from one another with walls. However, there are doors in these walls.

Each layer (except for layer $$$n$$$) has exactly two doors to the next layer. One door is placed on the top wall of the layer and another door is placed on the right wall of the layer. For each layer from $$$1$$$ to $$$n-1$$$ you are given positions of these two doors. The doors can be passed in both directions: either from layer $$$i$$$ to layer $$$i+1$$$ or from layer $$$i+1$$$ to layer $$$i$$$.

If you are standing in some cell, you can move to an adjacent by a side cell if a wall doesn't block your move (e.g. you can't move to a cell in another layer if there is no door between the cells).

Now you have $$$m$$$ queries of sort: what's the minimum number of moves one has to make to go from cell $$$(x_1, y_1)$$$ to cell $$$(x_2, y_2)$$$.

Input format:
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of layers in the labyrinth.

The $$$i$$$-th of the next $$$n-1$$$ lines contains four integers $$$d_{1,x}, d_{1,y}, d_{2,x}$$$ and $$$d_{2,y}$$$ ($$$1 \le d_{1,x}, d_{1,y}, d_{2,x}, d_{2,y} \le n$$$) — the coordinates of the doors. Both cells are on the $$$i$$$-th layer. The first cell is adjacent to the top wall of the $$$i$$$-th layer by a side — that side is where the door is. The second cell is adjacent to the right wall of the $$$i$$$-th layer by a side — that side is where the door is.

The next line contains a single integer $$$m$$$ ($$$1 \le m \le 2 \cdot 10^5$$$) — the number of queries.

The $$$j$$$-th of the next $$$m$$$ lines contains four integers $$$x_1, y_1, x_2$$$ and $$$y_2$$$ ($$$1 \le x_1, y_1, x_2, y_2 \le n$$$) — the coordinates of the cells in the $$$j$$$-th query.

Output format:
For each query, print a single integer — the minimum number of moves one has to make to go from cell $$$(x_1, y_1)$$$ to cell $$$(x_2, y_2)$$$.

Constraints:
Time limit: 6.0 s
Memory limit: 512.0 MB

Note:
Here is the map of the labyrinth from the second example. The doors are marked red.

Example input:
2
1 1 1 1
10
1 1 1 1
1 1 1 2
1 1 2 1
1 1 2 2
1 2 1 2
1 2 2 1
1 2 2 2
2 1 2 1
2 1 2 2
2 2 2 2

Example output:
0
1
1
2
0
2
1
0
1
0

Example input:
4
1 1 1 1
2 1 2 2
3 2 1 3
5
2 4 4 3
4 4 3 3
1 2 3 3
2 2 4 4
1 4 2 3

Example output:
3
4
3
6
2

Editorial:
WLOG, assume all queries ask to move from a lower layer to a higher layer. The first thing to notice in the problem is that it is always optimal to never go down a layer.
You have an optimal path that is going down some layers, and then returning to the same layer. So it leaves a layer in some its cell and returns to it in some other cell (or the same one). The best distance it can achieve is the Manhattan distance between these two cells. However, we can also achieve the Manhattan distance by just going along this layer, and the answer will be at least as optimal.
If the query asks about the cells of the same layer, just answer with the Manhattan distance. Otherwise, we can describe the path as follows: go from the first cell to some door on its layer, enter the door and go to another door on the next layer, so on until the layer of the second cell, where you go from a door to the second cell.
Thus, we could potentially write $$$dp_{i,j}$$$ — the shortest distance from the start to the $$$j$$$-th door of the $$$i$$$-th layer. Initialize both doors of the first layer, take the best answer from the both doors of the last layer. That would be $$$O(n)$$$ per query, which is too slow.
Let's optimize it with some precalculations. In particular, we want to know the shortest distance between one door of some layer and one door of another layer.
We can use the technique similar to binary lifting. Calculate the distance between a pair of doors on layers which are $$$2^x$$$ apart for all $$$x$$$ up to $$$\log n$$$. Let $$$dp_{i,x,d1,d2}$$$ be the distance from door $$$d1$$$ of layer $$$i$$$ to door $$$d2$$$ of layer $$$i+2^x$$$. $$$dp_{i,0,d1,d2}$$$ can be initialized straightforwardly. Then, to calculate $$$dp_{i,x,d1,d2}$$$, we can use the values for $$$x-1$$$: $$$dp_{i,x-1,d1,t}$$$ and $$$dp_{i+2^{x-1},x-1,t,d2}$$$ for some intermediate door $$$t$$$ on layer $$$i+2^{x-1}$$$.
To obtain the answer, use $$$O(\log n)$$$ jumps to reach the layer one before the last one. Then iterate over the last door.
Alternatively, you could pack this dynamic programming into a segment tree, use divide and conquer on queries or do square root decomposition.
Overall complexity: $$$O((n + m) \log n)$$$.