C - ミ・テレフェリコ (Mi Teleférico) 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MiB

Score: 100 points

Problem Statement

La Paz, the capital city of Bolivia, is famous as a tourist spot and for an aerial cable car called Mi Teleférico. You are now visiting La Paz for sightseeing, and you want to visit as many sightseeing places as possible. In this task, we consider the following simplified situation.

There are N aerial cable car stations in La Paz, numbered from 1 to N in ascending order of altitude. There are M one-way lines, numbered from 1 to M. There are P aerial cable car companies, numbered from 1 to P. Each line is managed by a single company. Line i (1 \leq i \leq M) is operated from station A_i to station B_i, and is managed by the company C_i. Here, the line always runs from the lower altitude station to the higher altitude station. In other words, A_i < B_i holds.

The Bureau of transportation of La Paz issued unlimited ride passes for convenience. Each ride pass contains 2 integers l, r, which satisfy 1 \leq l \leq r \leq P. The pass enables the possessor to ride lines, which are managed by any one of company l, l + 1, \dots, r. In other words, for an integer i which satisfies 1 \leq i \leq M, the pass enables the possessor to ride line i when l \leq C_i \leq r holds. It is possible to use a single pass for several lines. Let a ride pass (l, r) denote this ride pass.

Now, Q tourists, numbered from 1 to Q, visit La Paz. Tourist j (1 \leq j \leq Q) has a ride pass (L_j, R_j) and X_j boliviano cash.

Each tourist's goal is to ensure that no station cannot be travelled from station 1, using only lines that can be ridden with the ride pass he or she has. Tourist j (1 \leq j \leq Q) can exchange his or her ride pass described in the following process to achieve their goal. Here, each tourist can exchange at most once.

  1. He or she chooses 2 integers l', r', which satisfy 1 \leq l' \leq r' \leq P.
  2. He or she exchanges a ride pass (L_j, R_j) for a ride pass (l', r'). It costs |L_j - l'| + |R_j - r'| boliviano as a fee.

Your purpose is to determine, for each tourist, whether or not he or she can achieve his or her goal within the cash he or she has.

Write a program which, given information about stations, lines, and tourists, determines whether or not he or she can achieve his or her goal within the cash he or she has for each tourist.


Input

Read the following data from the standard input.

N M P
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
Q
L_1 R_1 X_1
L_2 R_2 X_2
\vdots
L_Q R_Q X_Q

Output

Write Q lines to the standard output. On the j-th line (1 \leq j \leq Q), output Yes if tourist j can achieve his or her goal, and No otherwise.


Constraints

  • 2 \leq N \leq 300\,000.
  • 1 \leq M \leq 300\,000.
  • 1 \leq P \leq 10^9.
  • 1 \leq A_i < B_i \leq N (1 \leq i \leq M).
  • 1 \leq C_i \leq P (1 \leq i \leq M).
  • 1 \leq Q \leq 400\,000.
  • 1 \leq L_j \leq R_j \leq P (1 \leq j \leq Q).
  • 0 \leq X_j \leq 10^9 (1 \leq j \leq Q).
  • Given values are all integers.

Subtasks

  1. (7 points) N \leq 50, M \leq 50, Q \leq 50, X_j = 0 (1 \leq j \leq Q).
  2. (8 points) P \leq 10.
  3. (11 points) P \leq 100.
  4. (23 points) P \leq 300\,000, X_j = 0 (1 \leq j \leq Q).
  5. (9 points) P \leq 300\,000.
  6. (22 points) N \leq 8\,000, M \leq 8\,000.
  7. (20 points) No additional constraints.

Sample Input 1

4 6 10
1 2 3
2 4 7
1 2 6
2 3 5
3 4 2
3 4 8
4
3 7 0
5 6 0
3 4 0
1 9 0

Sample Output 1

Yes
No
No
Yes

First, tourist 1 has a ride pass (3, 7) and 0 boliviano cash initially. He or she can achieve his or her goal without exchange. Because, this pass enables tourist 1 to ride 4 lines, lines 1, 2, 3, 4, and he or she can move to each stations from station 1 by using these 4 lines as follows.

  • It is possible to move as station 1 \rightarrow 2 by using line 3.
  • It is possible to move as station 1 \rightarrow 2 \rightarrow 3 by using lines 1, 4 in this order.
  • It is possible to move as station 1 \rightarrow 2 \rightarrow 4 by using lines 3, 2 in this order.

Therefore, output Yes on the 1-st line.

Next, tourist 2 has a ride pass (5, 6) and 0 boliviano cash initially. However, He or she can't achieve his or her goal. Because, this pass enables tourist 2 to ride 2 lines, lines 3, 4, and he or she can't move station 4 from station 1 by using these 2 lines. Moreover, since tourist 2 has only 0 boliviano as cash, he or she can't exchange the pass with another pass.

Hence, output No on the 2-nd line.

Also, tourist 3 can't achieve his or her goal, and tourist 4 can achieve his or her goal. Thus, output No on the 3-rd line, and output Yes on the 4-th line.

This sample input satisfies the constraints of all the Subtasks.


Sample Input 2

4 6 10
1 2 3
2 4 7
1 2 6
2 3 5
3 4 2
3 4 8
3
5 6 10
3 4 1
7 8 3

Sample Output 2

Yes
No
Yes

The information of stations and lines is the same as in the sample Input 1.

First, tourist 1 has a ride pass (5, 6) and 10 boliviano cash initially. He or she can achieve his or her goal with exchange as below.

  1. He or she chooses 2 integers l' = 1, r' = 5, which satisfy 1 \leq l' \leq r' \leq P.
  2. He or she exchange a ride pass (5, 6) for a ride pass (1, 5). It costs |5 - 1| + |6 - 5| = 5 boliviano as a fee.

Therefore, output Yes on the 1-st line.

Next, tourist 2 has a ride pass (3, 4) and 1 boliviano cash initially. He or she can't achieve his or her goal with any exchange.

Consequently, output No on the 2-nd line.

Since Tourist 3 can achieve his or her goal, output Yes on the 3-rd line.

This sample input satisfies the constraints of Subtasks 2,3,5,6,7.


Sample Input 3

3 1 1000000000
1 2 6
1
1 1000000000 1000000000

Sample Output 3

No

It is not possible to move from station 1 to station 3 with these lines, so tourists can't achieve their goal regardless of their ride passes.

This sample input satisfies the constraints of Subtasks 6,7.


Sample Input 4

5 9 2000
2 3 1814
2 3 457
1 2 1226
3 4 1354
1 5 1050
1 2 1725
2 3 1383
1 5 1626
1 4 1795
5
850 1872 128
82 428 1217
487 924 573
1639 1926 202
202 420 25

Sample Output 4

Yes
Yes
Yes
Yes
No

This sample input satisfies the constraints of Subtasks 5,6,7.

配点: 100

問題文

ボリビアの首都であるラパスは観光地であるとともに,ミ・テレフェリコ(Mi Teleférico)というロープウェイ路線網でも有名である.あなたはラパスに観光に来ており,できるだけ多くの場所を観光したいと思っている.ここで,現実を単純化した次のような状況設定を考えたい.

ラパスには N 個のロープウェイ駅があり,標高が低い順に 1 から N までの番号が付けられている.また,M 個の一方通行の路線があり,1 から M までの番号が付けられている.さらに,P 個のロープウェイ会社があり,1 から P までの番号が付けられている.各路線は 1 つの会社によって管理されている.路線 i (1 \leqq i \leqq M) は駅 A_i から駅 B_i に向かって運行しており,会社 C_i によって管理されている.ここで,路線は必ず標高の低い駅から標高の高い駅に向かって運行している.すなわち,A_i < B_i が成立している.

利便性のために,ラパスの交通局はフリーパスを発行した.それぞれのフリーパスには 1 \leqq l \leqq r \leqq P を満たす 2 つの整数 l, r が書かれており,会社 l, l + 1, \dots, r によって管理されている路線に乗ることができる.すなわち,1 \leqq i \leqq M を満たす整数 i について l \leqq C_i \leqq r を満たすならば路線 i に乗ることができる.ここで,1 つのフリーパスを複数の路線で使うことも可能である.このフリーパスをフリーパス (l, r) とする.

さて,ラパスに 1 から Q までの番号が付けられた Q 人の観光客が訪れた.観光客 j (1 \leqq j \leqq Q) はフリーパス (L_j, R_j) と,現金 X_j ボリビアーノを持っている.

観光客の目標は,持っているフリーパスを使って乗ることができる路線のみを用いて,駅 1 から移動できない駅がないようにすることである.そのために,観光客 j (1 \leqq j \leqq Q) は以下の手順で表される交換を行うことができる.ただし,各観光客について,交換は高々 1 回しか行うことができない.

  1. 1 \leqq l' \leqq r' \leqq P を満たす 2 つの整数 l', r' を決める.
  2. フリーパス (L_j, R_j) とフリーパス (l', r') を交換する.手数料として |L_j - l'| + |R_j - r'| ボリビアーノを要する.

あなたの目的は,それぞれの観光客について,持っている現金の範囲内で目標を達成することができるかを判定することである.

路線と観光客の情報が与えられたとき,それぞれの観光客について,持っている現金の範囲内で目標を達成することができるかを判定するプログラムを作成せよ.


入力

入力は以下の形式で標準入力から与えられる.

N M P
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
Q
L_1 R_1 X_1
L_2 R_2 X_2
\vdots
L_Q R_Q X_Q

出力

標準出力に Q 行で出力せよ.j 行目 (1 \leqq j \leqq Q) には,観光客 j が目標を達成することができる場合は Yes を,そうでない場合は No を出力せよ.


制約

  • 2 \leqq N \leqq 300\,000
  • 1 \leqq M \leqq 300\,000
  • 1 \leqq P \leqq 10^9
  • 1 \leqq A_i < B_i \leqq N (1 \leqq i \leqq M).
  • 1 \leqq C_i \leqq P (1 \leqq i \leqq M).
  • 1 \leqq Q \leqq 400\,000
  • 1 \leqq L_j \leqq R_j \leqq P (1 \leqq j \leqq Q).
  • 0 \leqq X_j \leqq 10^9 (1 \leqq j \leqq Q).
  • 入力される値はすべて整数である.

小課題

  1. (7 点) N \leqq 50M \leqq 50Q \leqq 50X_j = 0 (1 \leqq j \leqq Q).
  2. (8 点) P \leqq 10
  3. (11 点) P \leqq 100
  4. (23 点) P \leqq 300\,000X_j = 0 (1 \leqq j \leqq Q).
  5. (9 点) P \leqq 300\,000
  6. (22 点) N \leqq 8\,000M \leqq 8\,000
  7. (20 点) 追加の制約はない.

入力例 1

4 6 10
1 2 3
2 4 7
1 2 6
2 3 5
3 4 2
3 4 8
4
3 7 0
5 6 0
3 4 0
1 9 0

出力例 1

Yes
No
No
Yes

まず,観光客 1 については,最初フリーパス (3, 7) と現金 0 ボリビアーノを持っている.この観光客は,フリーパスの交換を行わないことで目標を達成できる.なぜなら,フリーパス (3, 7) により乗ることができる路線は 1, 2, 3, 44 つであり,この 4 つの路線を用いて,以下のように駅 1 から各駅に移動することができるからである.

  • 路線 3 を用いることで,駅 1 \to 2 と移動することができる.
  • 路線 1, 4 をこの順に用いることで,駅 1 \to 2 \to 3 と移動することができる.
  • 路線 3, 2 をこの順に用いることで,駅 1 \to 2 \to 4 と移動することができる.

したがって,1 行目に Yes を出力する.

次に,観光客 2 については,最初フリーパス (5, 6) と現金 0 ボリビアーノを持っているが,この観光客は目標を達成することができない.なぜなら,フリーパス (5, 6) により乗ることができる路線は路線 3, 42 つのみであり,この 2 つの路線を用いて,駅 1 から駅 4 へ移動することができないからである.また,現金を 0 ボリビアーノしか持っていないため,異なるフリーパスと交換することができないからである.

したがって,2 行目に No を出力する.

さらに,観光客 3 については目標を達成することができず,観光客 4 については目標を達成することができるため,3 行目に No を出力し,4 行目に Yes を出力する.

この入力例はすべての小課題の制約を満たす.


入力例 2

4 6 10
1 2 3
2 4 7
1 2 6
2 3 5
3 4 2
3 4 8
3
5 6 10
3 4 1
7 8 3

出力例 2

Yes
No
Yes

路線の情報は入力例 1 と同じである.

まず,観光客 1 については最初フリーパス (5, 6) と現金 10 ボリビアーノを持っている.この観光客は,フリーパスの交換を次のように行うことで目標を達成できる.

  1. 1 \leqq l' \leqq r' \leqq P を満たす 2 つの整数として l' = 1, r' = 5 と決める.
  2. フリーパス (5, 6) とフリーパス (1, 5) を交換する.手数料として |5-1|+|6-5|=5 ボリビアーノを要する.

したがって,1 行目に Yes を出力する.

次に,観光客 2 については最初フリーパス (3, 4) と現金 1 ボリビアーノを持っている.この観光客はどのようなフリーパスの交換を行っても,目標を達成することができない.

したがって,2 行目に No を出力する.

さらに,観光客 3 については目標を達成することができるため,3 行目に Yes を出力する.

この入力例は小課題 2,3,5,6,7 の制約を満たす.


入力例 3

3 1 1000000000
1 2 6
1
1 1000000000 1000000000

出力例 3

No

この路線において,駅 1 から駅 3 へ移動することができない.したがって,フリーパスによらず,観光客は目標を達成することはできない.

この入力例は小課題 6,7 の制約を満たす.


入力例 4

5 9 2000
2 3 1814
2 3 457
1 2 1226
3 4 1354
1 5 1050
1 2 1725
2 3 1383
1 5 1626
1 4 1795
5
850 1872 128
82 428 1217
487 924 573
1639 1926 202
202 420 25

出力例 4

Yes
Yes
Yes
Yes
No

この入力例は小課題 5,6,7 の制約を満たす.