#abc254g. G - Elevators

G - Elevators

Score : 600600 points

问题描述

存在一个由 NN10910^9 层摩天大楼组成的综合建筑群。这些摩天大楼按编号从 11NN 编号,楼层则按编号从 1110910^9 编号。

从任何一座摩天大楼的任意一层楼出发,都可以通过空中连廊在一分钟内到达另一座任意摩天大楼的同一层楼。

另外,还有 MM 部电梯。第 ii 部电梯在摩天大楼 AiA_i 的第 BiB_i 层与第 CiC_i 层之间运行。利用这部电梯,在满足 Bix,yCiB_i \le x,y \le C_i 条件下的每一对整数楼层 xxyy,可以在 xy|x-y| 分钟内从摩天大楼 AiA_i 的第 xx 层到达第 yy 层。

接下来需要回答 QQ 个查询。

  • 确定是否可以从摩天大楼 XiX_i 的第 YiY_i 层到达摩天大楼 ZiZ_i 的第 WiW_i 层,并在可能的情况下找出到达那里所需的最短时间。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a complex composed of NN 10910^9-story skyscrapers. The skyscrapers are numbered 11 to NN, and the floors are numbered 11 to 10910^9.

From any floor of any skyscraper, one can use a skybridge to get to the same floor of any other skyscraper in one minute.

Additionally, there are MM elevators. The ii-th elevator runs between Floor BiB_i and Floor CiC_i of Skyscraper AiA_i. With this elevator, one can get from Floor xx to Floor yy of Skyscraper AiA_i in xy|x-y| minutes, for every pair of integers x,yx,y such that Bix,yCiB_i \le x,y \le C_i.

Answer the following QQ queries.

  • Determine whether it is possible to get from Floor YiY_i of Skyscraper XiX_i to Floor WiW_i of Skyscraper ZiZ_i, and find the shortest time needed to get there if it is possible.

Constraints

  • 1N,M,Q2×1051 \le N,M,Q \le 2 \times 10^5
  • 1AiN1 \le A_i \le N
  • 1Bi<Ci1091 \le B_i < C_i \le 10^9
  • 1Xi,ZiN1 \le X_i,Z_i \le N
  • 1Yi,Wi1091 \le Y_i,W_i \le 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM QQ

A1A_1 B1B_1 C1C_1

A2A_2 B2B_2 C2C_2

\vdots

AMA_M BMB_M CMC_M

query1\mathrm{query}_1

query2\mathrm{query}_2

\vdots

queryQ\mathrm{query}_Q

Each query is in the following format:

XiX_i YiY_i ZiZ_i WiW_i

Output

Print QQ lines. The ii-th line should contain -1 if, for queryi\mathrm{query}_i, the destination is unreachable; otherwise, it should contain the minimum number of minutes needed to get there.

Sample Input 1

3 4 3
1 2 10
2 3 7
3 9 14
3 1 3
1 3 3 14
3 1 2 7
1 100 1 101

Sample Output 1

12
7
-1

For the 11-st query, you can get to the destination in 1212 minutes as follows.

  • Use Elevator 11 to get from Floor 33 to Floor 99 of Skyscraper 11, in 66 minutes.
  • Use the skybridge on Floor 99 to get from Skyscraper 11 to Skyscraper 33, in 11 minute.
  • Use Elevator 33 to get from Floor 99 to Floor 1414 of Skyscraper 33, in 55 minutes.

For the 33-rd query, the destination is unreachable, so -1 should be printed.

Sample Input 2

1 1 1
1 1 2
1 1 1 2

Sample Output 2

1

update @ 2024/3/10 10:50:35