A - Adjacent Squares

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

H 行、横 W 列のマス目があり、このうち上から i 個目、左から j 個目のマスを (i,j) と呼びます。
このとき、マス (R,C) に辺で隣接するマスの個数を求めてください。

ただし、ある 2 つのマス (a,b),(c,d) が辺で隣接するとは、 |a-c|+|b-d|=1 (|x|x の絶対値とする) であることを言います。

制約

  • 入力は全て整数
  • 1 \le R \le H \le 10
  • 1 \le C \le W \le 10

入力

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

H W
R C

出力

答えを整数として出力せよ。


入力例 1

3 4
2 2

出力例 1

4

入出力例 1,2,3 に対する説明は、出力例 3 の下にまとめて示します。


入力例 2

3 4
1 3

出力例 2

3

入力例 3

3 4
3 4

出力例 3

2

H=3,W=4 のとき、マス目は以下のようになります。

  • 入力例 1 について、マス (2,2) に隣接するマスは 4 つです。
  • 入力例 2 について、マス (1,3) に隣接するマスは 3 つです。
  • 入力例 3 について、マス (3,4) に隣接するマスは 2 つです。


入力例 4

1 10
1 5

出力例 4

2

入力例 5

8 1
8 1

出力例 5

1

入力例 6

1 1
1 1

出力例 6

0

Score : 100 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.
Find the number of squares that share a side with Square (R, C).

Here, two squares (a,b) and (c,d) are said to share a side if and only if |a-c|+|b-d|=1 (where |x| denotes the absolute value of x).

Constraints

  • All values in input are integers.
  • 1 \le R \le H \le 10
  • 1 \le C \le W \le 10

Input

Input is given from Standard Input in the following format:

H W
R C

Output

Print the answer as an integer.


Sample Input 1

3 4
2 2

Sample Output 1

4

We will describe Sample Inputs/Outputs 1,2, and 3 at once below Sample Output 3.


Sample Input 2

3 4
1 3

Sample Output 2

3

Sample Input 3

3 4
3 4

Sample Output 3

2

When H=3 and W=4, the grid looks as follows.

  • For Sample Input 1, there are 4 squares adjacent to Square (2,2).
  • For Sample Input 2, there are 3 squares adjacent to Square (1,3).
  • For Sample Input 3, there are 2 squares adjacent to Square (3,4).


Sample Input 4

1 10
1 5

Sample Output 4

2

Sample Input 5

8 1
8 1

Sample Output 5

1

Sample Input 6

1 1
1 1

Sample Output 6

0
B - Enlarged Checker Board

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

A 行、横 B 列のマスからなるタイルを縦 N 行、横 N 列に並べてできた、縦 (A\times N) 行、横 (B\times N) 列のマス目 X があります。
1\leq i,j \leq N について、上から i 行目、左から j 列目のタイルをタイル (i,j) とします。

X の各マスは以下のように塗られています。

  • 各タイルは白いタイルまたは黒いタイルである。
  • 白いタイルのすべてのマスは白で塗られ、黒いタイルのすべてのマスは黒で塗られている。
  • タイル (1,1) は白いタイルである。
  • 辺で隣接する 2 つのタイルは異なる色のタイルである。ただし、タイル (a,b) とタイル (c,d) が辺で隣接するとは、|a-c|+|b-d|=1 ( |x|x の絶対値とする)であることを言う。

マス目 X を出力の形式に従って出力してください。

制約

  • 1 \leq N,A,B \leq 10
  • 入力は全て整数

入力

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

N A B

出力

次の条件をみたす (A\times N) 個の文字列 S_1,\ldots,S_{A\times N} を改行区切りで出力せよ。

  • S_1,\ldots,S_{A\times N} はそれぞれ長さ (B\times N). または # からなる文字列である。
  • i,j (1 \leq i \leq A\times N,1 \leq j \leq B\times N) に対し、マス目 X の上から i 行目かつ左から j 列目のマスが白で塗られているならば S_ij 文字目は .であり、黒く塗られているならば # である。

入力例 1

4 3 2

出力例 1

..##..##
..##..##
..##..##
##..##..
##..##..
##..##..
..##..##
..##..##
..##..##
##..##..
##..##..
##..##..

入力例 2

5 1 5

出力例 2

.....#####.....#####.....
#####.....#####.....#####
.....#####.....#####.....
#####.....#####.....#####
.....#####.....#####.....

入力例 3

4 4 1

出力例 3

.#.#
.#.#
.#.#
.#.#
#.#.
#.#.
#.#.
#.#.
.#.#
.#.#
.#.#
.#.#
#.#.
#.#.
#.#.
#.#.

入力例 4

1 4 4

出力例 4

....
....
....
....

Score : 200 points

Problem Statement

Tiles are aligned in N horizontal rows and N vertical columns. Each tile has a grid with A horizontal rows and B vertical columns. On the whole, the tiles form a grid X with (A\times N) horizontal rows and (B\times N) vertical columns.
For 1\leq i,j \leq N, Tile (i,j) denotes the tile at the i-th row from the top and the j-th column from the left.

Each square of X is painted as follows.

  • Each tile is either a white tile or a black tile.
  • Every square in a white tile is painted white; every square in a black tile is painted black.
  • Tile (1,1) is a white tile.
  • Two tiles sharing a side have different colors. Here, Tile (a,b) and Tile (c,d) are said to be sharing a side if and only if |a-c|+|b-d|=1 (where |x| denotes the absolute value of x).

Print the grid X in the format specified in the Output section.

Constraints

  • 1 \leq N,A,B \leq 10
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print (A\times N) strings S_1,\ldots,S_{A\times N} that satisfy the following condition, with newlines in between.

  • Each of S_1,\ldots,S_{A\times N} is a string of length (B\times N) consisting of . and #.
  • For each i and j (1 \leq i \leq A\times N,1 \leq j \leq B\times N), the j-th character of S_i is . if the square at the i-th row from the top and j-th column from the left in grid X is painted white; the character is # if the square is painted black.

Sample Input 1

4 3 2

Sample Output 1

..##..##
..##..##
..##..##
##..##..
##..##..
##..##..
..##..##
..##..##
..##..##
##..##..
##..##..
##..##..

Sample Input 2

5 1 5

Sample Output 2

.....#####.....#####.....
#####.....#####.....#####
.....#####.....#####.....
#####.....#####.....#####
.....#####.....#####.....

Sample Input 3

4 4 1

Sample Output 3

.#.#
.#.#
.#.#
.#.#
#.#.
#.#.
#.#.
#.#.
.#.#
.#.#
.#.#
.#.#
#.#.
#.#.
#.#.
#.#.

Sample Input 4

1 4 4

Sample Output 4

....
....
....
....
C - Adjacent Swaps

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 個のボールが左右一列に並んでいます。初め、左から i \, (1 \leq i \leq N) 番目のボールには整数 i が書かれています。

高橋君は Q 回の操作を行いました。 i \, (1 \leq i \leq Q) 回目に行われた操作は次のようなものです。

  • 整数 x_i が書かれているボールをその右隣のボールと入れ替える。ただし、整数 x_i が書かれているボールが元々右端にあった場合、代わりに左隣のボールと入れ替える。

操作後において左から i \, (1 \leq i \leq N) 番目のボールに書かれている整数を a_i とします。 a_1,\ldots,a_N を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq x_i \leq N
  • 入力は全て整数

入力

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

N Q
x_1
\vdots
x_Q

出力

a_1,\ldots,a_N を空白区切りで出力せよ。


入力例 1

5 5
1
2
3
4
5

出力例 1

1 2 3 5 4

操作は以下のように行われます。

  • 1 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 2,1,3,4,5 となる。
  • 2 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
  • 3 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,4,3,5 となる。
  • 4 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
  • 5 と書かれたボールは右端にあるので左隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,5,4 となる。

入力例 2

7 7
7
7
7
7
7
7
7

出力例 2

1 2 3 4 5 7 6

入力例 3

10 6
1
5
2
9
6
6

出力例 3

1 2 3 4 5 7 6 8 10 9

Score : 300 points

Problem Statement

N balls are lined up in a row from left to right. Initially, the i-th (1 \leq i \leq N) ball from the left has an integer i written on it.

Takahashi has performed Q operations. The i-th (1 \leq i \leq Q) operation was as follows.

  • Swap the ball with the integer x_i written on it with the next ball to the right. If the ball with the integer x_i written on it was originally the rightmost ball, swap it with the next ball to the left instead.

Let a_i be the integer written on the i-th (1 \leq i \leq N) ball after the operations. Find a_1,\ldots,a_N.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq x_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
x_1
\vdots
x_Q

Output

Print a_1,\ldots,a_N, with spaces in between.


Sample Input 1

5 5
1
2
3
4
5

Sample Output 1

1 2 3 5 4

The operations are performed as follows.

  • Swap the ball with 1 written on it with the next ball to the right. Now, the balls have integers 2,1,3,4,5 written on them, from left to right.
  • Swap the ball with 2 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
  • Swap the ball with 3 written on it with the next ball to the right. Now, the balls have integers 1,2,4,3,5 written on them, from left to right.
  • Swap the ball with 4 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
  • Swap the ball with 5 written on it with the next ball to the left, since it is the rightmost ball. Now, the balls have integers 1,2,3,5,4 written on them, from left to right.

Sample Input 2

7 7
7
7
7
7
7
7
7

Sample Output 2

1 2 3 4 5 7 6

Sample Input 3

10 6
1
5
2
9
6
6

Sample Output 3

1 2 3 4 5 7 6 8 10 9
D - 250-like Number

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

以下の条件を満たす整数 k を「 250 に似た数」と呼びます。

  • k が素数 p<q を使って k=p \times q^3 と表される。

N 以下の「 250 に似た数」は全部でいくつありますか?

制約

  • N1 以上 10^{18} 以下の整数

入力

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

N

出力

答えを整数として出力せよ。


入力例 1

250

出力例 1

2
  • 54 = 2 \times 3^3 なので、「 250 に似た数」です。
  • 250 = 2 \times 5^3 なので、「 250 に似た数」です。

250 以下の「 250 に似た数」は、以上の 2 つです。


入力例 2

1

出力例 2

0

入力例 3

123456789012345

出力例 3

226863

Score : 400 points

Problem Statement

Let us regard an integer k as "similar to 250" if the following condition is satisfied:

  • k is represented as k=p \times q^3 with primes p<q.

How many integers less than or equal to N are "similar to 250"?

Constraints

  • N is an integer between 1 and 10^{18} (inclusive)

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer as an integer.


Sample Input 1

250

Sample Output 1

2
  • 54 = 2 \times 3^3 is "similar to 250".
  • 250 = 2 \times 5^3 is "similar to 250".

The two integers above are all the integers "similar to 250".


Sample Input 2

1

Sample Output 2

0

Sample Input 3

123456789012345

Sample Output 3

226863
E - Prefix Equality

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N の整数列 A = (a_1,\ldots,a_N)B = (b_1,\ldots,b_N) が与えられます。

i=1,...,Q に対し、次の形式のクエリに答えてください。

  • A の先頭 x_i(a_1,\ldots,a_{x_i}) に含まれる値の集合と B の先頭 y_i(b_1,\ldots,b_{y_i}) に含まれる値の集合が等しいならば Yes と、そうでなければ No と出力せよ。

制約

  • 1 \leq N,Q \leq 2 \times 10^5
  • 1 \leq a_i,b_i \leq 10^9
  • 1 \leq x_i,y_i \leq N
  • 入力は全て整数

入力

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

N
a_1 \ldots a_N
b_1 \ldots b_N
Q
x_1 y_1
\vdots
x_Q y_Q

出力

Q 行出力せよ。 i 行目には、i 番目のクエリに対する出力をせよ。


入力例 1

5
1 2 3 4 5
1 2 2 4 3
7
1 1
2 2
2 3
3 3
4 4
4 5
5 5

出力例 1

Yes
Yes
Yes
No
No
Yes
No

集合は各値が含まれるかどうかのみに注目した概念であることに気を付けてください。
3 番目のクエリにおいて、A の先頭 2 項には 121 個ずつ、B の先頭 3 項には 11 個と 22 個含まれます。しかし、それぞれに含まれる値の集合はどちらも \{ 1,2 \} となり、一致します。
また、6 番目のクエリにおいては各値が現れる順番が異なりますが、やはり集合としては一致します。

Score : 500 points

Problem Statement

You are given integer sequences A = (a_1,\ldots,a_N) and B = (b_1,\ldots,b_N), each of length N.

For i=1,...,Q, answer the query in the following format.

  • If the set of values contained in the first x_i terms of A, (a_1,\ldots,a_{x_i}), and the set of values contained in the first y_i terms of B, (b_1,\ldots,b_{y_i}), are equal, then print Yes; otherwise, print No.

Constraints

  • 1 \leq N,Q \leq 2 \times 10^5
  • 1 \leq a_i,b_i \leq 10^9
  • 1 \leq x_i,y_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 \ldots a_N
b_1 \ldots b_N
Q
x_1 y_1
\vdots
x_Q y_Q

Output

Print Q lines. The i-th line should contain the response to the i-th query.


Sample Input 1

5
1 2 3 4 5
1 2 2 4 3
7
1 1
2 2
2 3
3 3
4 4
4 5
5 5

Sample Output 1

Yes
Yes
Yes
No
No
Yes
No

Note that sets are a concept where it matters only whether each value is contained or not.
For the 3-rd query, the first 2 terms of A contain one 1 and one 2, while the first 3 terms of B contain one 1 and two 2's. However, the sets of values contained in the segments are both \{ 1,2 \}, which are equal.
Also, for the 6-th query, the values appear in different orders, but they are still equal as sets.

F - One Fourth

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

ABC250 は、 ABC1000 の開催を目指す高橋くんにとってちょうど 1/4 となる記念すべき回です。
そこで、高橋くんはピザを 1 枚買ってきて、そのピザのうちなるべく 1/4 に近い分量を食べて祝うことにしました。

高橋くんが買ってきたピザは凸 N 角形 (N \ge 4) の平らな形をしており、このピザを xy 平面上に置いた際、 i 番目の頂点の座標は (X_i,Y_i) でした。

高橋くんは、このピザを以下のように切って食べることにしました。

  • まず、高橋くんはピザの頂点のうち隣接しない 2 頂点を選び、それらを通る直線に沿ってナイフを入れ、ピザを 2 つに切り分ける。
  • その後、 2 つのピースのうち好きなものをどちらか 1 つ選んで食べる。

高橋くんが買ってきたピザの面積の 1/4a 、高橋くんが食べるピースの面積を b とした時、 8 \times |a-b| としてありえる最小値を求めてください。なお、この値は常に整数となることが示せます。

制約

  • 入力は全て整数
  • 4 \le N \le 10^5
  • |X_i|, |Y_i| \le 4 \times 10^8
  • 入力される頂点は反時計回りに凸 N 角形をなす。

入力

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

N
X_1 Y_1
X_2 Y_2
\dots
X_N Y_N

出力

答えを整数として出力せよ。


入力例 1

5
3 0
2 3
-1 3
-3 1
-1 -1

出力例 1

1

3 番目の頂点と 5 番目の頂点を通る直線に沿ってピザを切り分け、 4 番目の頂点を含む側のピースを食べたとします。
このとき、a=\frac{33}{2} \times \frac{1}{4} = \frac{33}{8}b=48 \times |a-b|=1 であり、これがありえる最小値です。


入力例 2

4
400000000 400000000
-400000000 400000000
-400000000 -400000000
400000000 -400000000

出力例 2

1280000000000000000

入力例 3

6
-816 222
-801 -757
-165 -411
733 131
835 711
-374 979

出力例 3

157889

Score : 500 points

Problem Statement

ABC 250 is a commemorable quarter milestone for Takahashi, who aims to hold ABC 1000, so he is going to celebrate this contest by eating as close to 1/4 of a pizza he bought as possible.

The pizza that Takahashi bought has a planar shape of convex N-gon. When the pizza is placed on an xy-plane, the i-th vertex has coordinates (X_i, Y_i).

Takahashi has decided to cut and eat the pizza as follows.

  • First, Takahashi chooses two non-adjacent vertices from the vertices of the pizza and makes a cut with a knife along the line passing through those two points, dividing the pizza into two pieces.
  • Then, he chooses one of the pieces at his choice and eats it.

Let a be the quarter (=1/4) of the area of the pizza that Takahashi bought, and b be the area of the piece of pizza that Takahashi eats. Find the minimum possible value of 8 \times |a-b|. We can prove that this value is always an integer.

Constraints

  • All values in input are integers.
  • 4 \le N \le 10^5
  • |X_i|, |Y_i| \le 4 \times 10^8
  • The given points are the vertices of a convex N-gon in the counterclockwise order.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
\dots
X_N Y_N

Output

Print the answer as an integer.


Sample Input 1

5
3 0
2 3
-1 3
-3 1
-1 -1

Sample Output 1

1

Suppose that he makes a cut along the line passing through the 3-rd and the 5-th vertex and eats the piece containing the 4-th vertex.
Then, a=\frac{33}{2} \times \frac{1}{4} = \frac{33}{8}, b=4, and 8 \times |a-b|=1, which is minimum possible.


Sample Input 2

4
400000000 400000000
-400000000 400000000
-400000000 -400000000
400000000 -400000000

Sample Output 2

1280000000000000000

Sample Input 3

6
-816 222
-801 -757
-165 -411
733 131
835 711
-374 979

Sample Output 3

157889
G - Stonks

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

あなたは N 日にわたって、 X 社の株の取引を行います。

未来予知の能力者であるあなたは、取引のうち i 日目の X 社の株価が 1 株あたり P_i 円であることを知っています。

あなたは、毎日以下の行動をどれか 1 つだけ行うことが出来ます。

  • X 社の株を 1 株、 P_i 円で買う。
    • このとき、持ち株が 1 株増え、所持金が P_i 円減少する。
  • X 社の株を 1 株、 P_i 円で売る。この行動は株を 1 株以上保有している時行える。
    • このとき、持ち株が 1 株減り、所持金が P_i 円増加する。
  • 何もしない。

あなたの取引開始時の所持金は 10^{100} 円なので、現金に困ることはありません。

N 日目の行動を終えた時点で、所持金の増加額としてありうる最大値を求めてください。
なお、 N 日目の行動を終えた時点でまだ X 社の株をいくつか保有していても、それは所持金の計算上 0 円であるものとします。

制約

  • 入力は全て整数
  • 1 \le N \le 2 \times 10^5
  • 1 \le P_i \le 10^9

入力

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

N
P_1 P_2 \dots P_N

出力

答えを整数として出力せよ。


入力例 1

8
2 5 4 3 7 1 8 6

出力例 1

16

以下のように行動することで所持金を 16 円増加させることができ、これが最大です。

  • 1 日目、株を 1 株買う。 持ち株は 1 株、所持金の増加額は -2 円になる。
  • 2 日目、株を 1 株売る。 持ち株は 0 株、所持金の増加額は 3 円になる。
  • 3 日目、株を 1 株買う。 持ち株は 1 株、所持金の増加額は -1 円になる。
  • 4 日目、株を 1 株買う。 持ち株は 2 株、所持金の増加額は -4 円になる。
  • 5 日目、株を 1 株売る。 持ち株は 1 株、所持金の増加額は 3 円になる。
  • 6 日目、株を 1 株買う。 持ち株は 2 株、所持金の増加額は 2 円になる。
  • 7 日目、株を 1 株売る。 持ち株は 1 株、所持金の増加額は 10 円になる。
  • 8 日目、株を 1 株売る。 持ち株は 0 株、所持金の増加額は 16 円になる。

入力例 2

5
10000 1000 100 10 1

出力例 2

0

入力例 3

15
300 1 4000 1 50000 900000000 20 600000 50000 300 50000 80000000 900000000 7000000 900000000

出力例 3

2787595378

Score : 600 points

Problem Statement

You are going to trade stocks of Company X for the next N days.

As a precognitive, you know that the stock price on the i-th day of trading will be P_i yen (the currency in Japan) per unit.

Every day, you can choose to do exactly one of the following.

  • Buy one unit of stock for P_i yen.
    • You will obtain one unit of stock and your money will decrease by P_i yen.
  • Sell one unit of stock for P_i yen.
    • You will lose one unit of stock and your money will increase by P_i yen.
  • Do nothing.

You initially have 10^{100} yen, so you will never be short of money.

Find the maximum possible amount of money you will have gained when the N-th day has ended.
Even if you still possess some amount of stocks of Company X when the N-th day has ended, it is considered that they are worth 0 yen.

Constraints

  • All values in input are integers.
  • 1 \le N \le 2 \times 10^5
  • 1 \le P_i \le 10^9

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \dots P_N

Output

Print the answer as an integer.


Sample Input 1

8
2 5 4 3 7 1 8 6

Sample Output 1

16

By acting as follows, your money will increase by 16 yen, which is the maximum possible.

  • On the 1-th day, you buy 1 unit of stock. You now have 1 unit of stock, and your money has increased by -2 yen so far.
  • On the 2-nd day, you sell 1 unit of stock. You now have 0 units of stocks, and your money has increased by 3 yen so far.
  • On the 3-rd day, you buy 1 unit of stock. You now have 1 unit of stock, and your money has increased by -1 yen so far.
  • On the 4-th day, you buy 1 unit of stock. You now have 2 units of stocks, and your money has increased by -4 yen so far.
  • On the 5-th day, you sell 1 unit of stock. You now have 1 unit of stock, and your money has increased by 3 yen so far.
  • On the 6-th day, you buy 1 unit of stock. You now have 2 units of stocks, and your money has increased by 2 yen so far.
  • On the 7-th day, you sell 1 unit of stock. You now have 1 unit of stock, and your money has increased by 10 yen so far.
  • On the 8-th day, you sell 1 unit of stock. You now have 0 units of stocks, and your money has increased by 16 yen so far.

Sample Input 2

5
10000 1000 100 10 1

Sample Output 2

0

Sample Input 3

15
300 1 4000 1 50000 900000000 20 600000 50000 300 50000 80000000 900000000 7000000 900000000

Sample Output 3

2787595378
Ex - Trespassing Takahashi

Time Limit: 7 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 から N までの番号がついた N 個の地点と M 本の道があります。 i \, (1 \leq i \leq M) 番目の道は地点 a_i と地点 b_i を双方向に結んでいて、通過に c_i 分かかります。すべての地点同士は道を何本か通って行き来出来ます。また、地点 1,\ldots, K には家があります。

i=1,\ldots,Q に対し、次の問題を解いてください。

地点 x_i の家にいる高橋君が地点 y_i の家に移動しようとしている。
高橋君は最後に睡眠を取ってから道の移動にかかった時間が t_i 分を超えると移動が出来なくなる。
睡眠を取れる場所は家がある地点のみであるが、回数に制限は無い。
高橋君が地点 x_i から地点 y_i まで移動出来るならば Yes と、出来ないならば No と出力せよ。

制約

  • 2 \leq K \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq \min (2 \times 10^5, \frac{N(N-1)}{2})
  • 1 \leq a_i \lt b_i \leq N
  • i \neq j ならば (a_i,b_i) \neq (a_j,b_j)
  • 1 \leq c_i \leq 10^9
  • すべての地点同士は道を何本か通って行き来出来る
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq x_i \lt y_i \leq K
  • 1 \leq t_1 \leq \ldots \leq t_Q \leq 10^{15}
  • 入力は全て整数

入力

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

N M K
a_1 b_1 c_1
\vdots
a_M b_M c_M
Q
x_1 y_1 t_1
\vdots
x_Q y_Q t_Q

出力

Q 行出力せよ。 i 行目には、i 番目の問題に対する出力をせよ。


入力例 1

6 6 3
1 4 1
4 6 4
2 5 2
3 5 3
5 6 5
1 2 15
3
2 3 4
2 3 5
1 3 12

出力例 1

No
Yes
Yes

3 番目の問題において、地点 1 から地点 3 に直接向かうと 13 分以上かかります。しかし、 12 分かけて地点 2 に移動し、そこにある家で睡眠を取ってから地点 3 に移動することが出来ます。よって、答えは Yes となります。

Score : 600 points

Problem Statement

There are N points numbered 1 through N, and M roads. The i-th (1 \leq i \leq M) road connects Point a_i and Point b_i bidirectionally and requires c_i minutes to pass through. One can travel from any point to any other point using some number of roads. There is a house on Points 1,\ldots, K.

For i=1,\ldots,Q, solve the following problem.

Takahashi is currently at the house at Point x_i and wants to travel to the house at Point y_i.
Once t_i minutes have passed since his last sleep, he cannot continue moving anymore.
He can get sleep only at a point with a house, but he may do so any number of times.
If he can travel from Point x_i to Point y_i, print Yes; otherwise, print No.

Constraints

  • 2 \leq K \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq \min (2 \times 10^5, \frac{N(N-1)}{2})
  • 1 \leq a_i \lt b_i \leq N
  • If i \neq j, then (a_i,b_i) \neq (a_j,b_j).
  • 1 \leq c_i \leq 10^9
  • One can travel from any point to any other point using some number of roads.
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq x_i \lt y_i \leq K
  • 1 \leq t_1 \leq \ldots \leq t_Q \leq 10^{15}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
a_1 b_1 c_1
\vdots
a_M b_M c_M
Q
x_1 y_1 t_1
\vdots
x_Q y_Q t_Q

Output

Print Q lines. The i-th line should contain the answer for the i-th problem.


Sample Input 1

6 6 3
1 4 1
4 6 4
2 5 2
3 5 3
5 6 5
1 2 15
3
2 3 4
2 3 5
1 3 12

Sample Output 1

No
Yes
Yes

In the 3-rd problem, it takes no less than 13 minutes from Point 1 to reach Point 3 directly. However, he can first travel to Point 2 in 12 minutes, get sleep in the house there, and then travel to Point 3. Thus, the answer is Yes.