CF1681E.Labyrinth Adventures

省选/NOI-

通过率:0%

时间限制:6.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

The labyrinth has nn layers. The first layer is the bottom left corner (cell (1,1)(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 55 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 nn) 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 11 to n−1n-1 you are given positions of these two doors. The doors can be passed in both directions: either from layer ii to layer i+1i+1 or from layer i+1i+1 to layer ii.

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 mm queries of sort: what's the minimum number of moves one has to make to go from cell (x1,y1)(x_1, y_1) to cell (x2,y2)(x_2, y_2).

你发现了一张形状奇特的迷宫地图。该地图是一个 nn 行 nn 列的网格。网格的行从下到上编号为 11 至 nn,列从左到右编号为 11 至 nn。

该迷宫共有 nn 层。第 11 层为左下角(即单元格 (1,1)(1, 1))。第 22 层由所有在网格内、且与第 11 层中任意单元格正交或对角相邻(即共享一条边或一个顶点)的单元格组成。第 33 层由所有在网格内、且与第 22 层中任意单元格正交或对角相邻的单元格组成。依此类推。

例如,一个具有 55 层的迷宫形状如下所示:

各层之间由墙壁隔开,但这些墙壁上设有门。

除第 nn 层外,每一层均恰好有两扇通向下一层次的门:一扇位于该层的上侧墙上,另一扇位于该层的右侧墙上。对于从 11 到 n−1n-1 的每一层,题目均给出这两扇门的位置。门是双向通行的:既可从第 ii 层经门进入第 i+1i+1 层,也可从第 i+1i+1 层经门返回第 ii 层。

若你位于某个单元格中,则仅当相邻(正交方向,即上下左右)单元格之间无墙壁阻挡时,方可移动至该相邻单元格(例如:若两个单元格分属不同层,且其间无门,则不可移动)。

现在你有 mm 个查询,每个查询形如:从单元格 (x1,y1)(x_1, y_1) 移动到单元格 (x2,y2)(x_2, y_2) 所需的最少步数是多少?

输入格式

The first line contains a single integer nn (2≤n≤1052 \le n \le 10^5) — the number of layers in the labyrinth.

The ii-th of the next n−1n-1 lines contains four integers d1,x,d1,y,d2,xd_{1,x}, d_{1,y}, d_{2,x} and d2,yd_{2,y} (1≤d1,x,d1,y,d2,x,d2,y≤n1 \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 ii-th layer. The first cell is adjacent to the top wall of the ii-th layer by a side — that side is where the door is. The second cell is adjacent to the right wall of the ii-th layer by a side — that side is where the door is.

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

The jj-th of the next mm lines contains four integers x1,y1,x2x_1, y_1, x_2 and y2y_2 (1≤x1,y1,x2,y2≤n1 \le x_1, y_1, x_2, y_2 \le n) — the coordinates of the cells in the jj-th query.

第一行包含一个整数 nn(2≤n≤1052 \le n \le 10^5)—— 迷宫的层数。

接下来的 n−1n-1 行中,第 ii 行包含四个整数 d1,x,d1,y,d2,xd_{1,x}, d_{1,y}, d_{2,x} 和 d2,yd_{2,y}(1≤d1,x,d1,y,d2,x,d2,y≤n1 \le d_{1,x}, d_{1,y}, d_{2,x}, d_{2,y} \le n)—— 表示第 ii 层中两扇门的坐标。这两个格子均位于第 ii 层。第一个格子与第 ii 层的上边界相邻(共享一条边)—— 该边即为第一扇门所在位置;第二个格子与第 ii 层的右边界相邻(共享一条边)—— 该边即为第二扇门所在位置。

下一行包含一个整数 mm(1≤m≤2⋅1051 \le m \le 2 \cdot 10^5)—— 查询次数。

接下来的 mm 行中,第 jj 行包含四个整数 x1,y1,x2x_1, y_1, x_2 和 y2y_2(1≤x1,y1,x2,y2≤n1 \le x_1, y_1, x_2, y_2 \le n)—— 表示第 jj 次查询中两个格子的坐标。

输出格式

For each query, print a single integer — the minimum number of moves one has to make to go from cell (x1,y1)(x_1, y_1) to cell (x2,y2)(x_2, y_2).

对于每个查询,输出一个整数——从单元格 (x1,y1)(x_1, y_1) 移动到单元格 (x2,y2)(x_2, y_2) 所需的最少移动次数。

输入输出样例

  • 输入#1

    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

    输出#1

    0
    1
    1
    2
    0
    2
    1
    0
    1
    0
  • 输入#2

    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

    输出#2

    3
    4
    3
    6
    2

说明/提示

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

以下是第二个示例中迷宫的地图。门用红色标记。

输入解题思路,AI测评打分。不知道怎么写?

首页