atcoder#ABC254G. [ABC254G] Elevators

[ABC254G] Elevators

Score : 600600 points

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.

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
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.

1 1 1
1 1 2
1 1 1 2
1