A - Four Digits

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

0 以上 9999 以下の整数 N が与えられます。

N の先頭に必要なだけ 0 をつけ、4 桁の文字列にしたものを出力してください。

制約

  • 0 \leq N \leq 9999
  • N は整数

入力

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

N

出力

答えを出力せよ。


入力例 1

321

出力例 1

0321

321 は 3 桁なので、先頭に 1 つ 0 をつけると 4 桁になります。


入力例 2

7777

出力例 2

7777

入力例 3

1

出力例 3

0001

Score : 100 points

Problem Statement

You are given an integer N between 0 and 9999 (inclusive).

Print it as a four-digit string after appending to it the necessary number of leading zeros.

Constraints

  • 0 \leq N \leq 9999
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

321

Sample Output 1

0321

321 has three digits, so we need to add one leading zero to it to make it have four digits.


Sample Input 2

7777

Sample Output 2

7777

Sample Input 3

1

Sample Output 3

0001
B - Sigma Cubes

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

正整数 N が与えられます。

i=1,2,\ldots,N について (-1)^i \times i^3 を計算したときの、それら N 個の値の総和を求めてください。

すなわち、 \displaystyle \sum_{i=1}^N (-1)^i \times i^3 を求めてください。

制約

  • N は 1 以上 10 以下の整数

入力

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

N

出力

\displaystyle \sum_{i=1}^N (-1)^i \times i^3 の値を出力せよ。


入力例 1

3

出力例 1

-20
  • i=1 のとき: (-1)^i\times i^3=-1 です。
  • i=2 のとき: (-1)^i\times i^3=8 です。
  • i=3 のとき: (-1)^i\times i^3=-27 です。

したがって、 -1 + 8 - 27 = -20 を出力してください。


入力例 2

9

出力例 2

-425

入力例 3

10

出力例 3

575

Score : 100 points

Problem Statement

You are given a positive integer N.

For i=1,2,\ldots,N, calculate (-1)^i \times i^3, and find the sum of these N values.

That is, find \displaystyle \sum_{i=1}^N (-1)^i \times i^3.

Constraints

  • N is an integer between 1 and 10, inclusive.

Input

The input is given from Standard Input in the following format:

N

Output

Output the value of \displaystyle \sum_{i=1}^N (-1)^i \times i^3.


Sample Input 1

3

Sample Output 1

-20
  • When i=1: (-1)^i\times i^3=-1.
  • When i=2: (-1)^i\times i^3=8.
  • When i=3: (-1)^i\times i^3=-27.

Therefore, output -1 + 8 - 27 = -20.


Sample Input 2

9

Sample Output 2

-425

Sample Input 3

10

Sample Output 3

575
C - log2(N)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

正整数 N が与えられるので、 2^k \le N となる最大の整数 k を求めてください。

制約

  • N は 1 \le N \le 10^{18} を満たす整数である

入力

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

N

出力

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


入力例 1

6

出力例 1

2
  • k=2 は 2^2=4 \le 6 を満たします。
  • k \ge 3 である全ての整数 k について 2^k > 6 となります。

以上より、答えは k=2 となります。


入力例 2

1

出力例 2

0

2^0=1 であることに注意してください。


入力例 3

1000000000000000000

出力例 3

59

入力が 32 bit 整数に収まらない場合があります。

Score : 200 points

Problem Statement

Given a positive integer N, find the maximum integer k such that 2^k \le N.

Constraints

  • N is an integer satisfying 1 \le N \le 10^{18}.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer as an integer.


Sample Input 1

6

Sample Output 1

2
  • k=2 satisfies 2^2=4 \le 6.
  • For every integer k such that k \ge 3, 2^k > 6 holds.

Therefore, the answer is k=2.


Sample Input 2

1

Sample Output 2

0

Note that 2^0=1.


Sample Input 3

1000000000000000000

Sample Output 3

59

The input value may not fit into a 32-bit integer.

D - P(X or Y)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250 点

問題文

1,2,3,4,5,6 の 6 種類の目が出るサイコロを 2 つ振ったときに、次の 2 つの条件の少なくとも一方を満たす確率を求めてください。

  • 2 つの出目の合計が X 以上である。
  • 2 つの出目の差の絶対値が Y 以上である。

ここで、どちらのサイコロについても 6 種類のどの目が出るかは同様に確からしく、それぞれのサイコロの出目は独立であるとします。

制約

  • 2\leq X\leq13
  • 0\leq Y\leq6
  • 入力はすべて整数

入力

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

X Y

出力

2 つのサイコロの出目が 2 つの条件の少なくとも一方を満たす確率を出力せよ。 出力された値と真の値との絶対誤差が 10 ^ {-9} 以下のとき、正答と判定される。


入力例 1

9 3

出力例 1

0.555555555555555555555555555555

2 つのサイコロの出目がそれぞれ x と y であることを (x,y) で表すことにすると、それぞれの条件を満たすのは次の場合です。

  • 2 つのサイコロの出目の和が 9 以上になるのは、(3,6),(4,5),(4,6),(5,4),(5,5),(5,6),(6,3),(6,4),(6,5),(6,6) のとき
  • 2 つのサイコロの出目の差が 3 以上になるのは、(1,4),(1,5),(1,6),(2,5),(2,6),(3,6),(4,1),(5,1),(5,2),(6,1),(6,2),(6,3) のとき

これらの条件の少なくとも一方を満たすのは (1,4),(1,5),(1,6),(2,5),(2,6),(3,6),(4,1),(4,5),(4,6),(5,1),(5,2),(5,4),(5,5),(5,6),(6,1),(6,2),(6,3),(6,4),(6,5),(6,6) の 20 通りのいずれかのときです。

よって、求める答えは \dfrac{20}{36}=\dfrac59=0.5555555555\ldots です。

絶対誤差が 10 ^ {-9} 以下のとき正答と判定されるので、0.5555555565 や 0.55555555456789 などと出力しても正解となります。


入力例 2

13 6

出力例 2

0

2 つのサイコロの出目の和が 13 以上になることも、差が 6 以上になることもありません。

よって、求める答えは 0 です。


入力例 3

10 3

出力例 3

0.5

Score : 250 points

Problem Statement

Two dice, each with six faces 1,2,3,4,5,6, are rolled. Find the probability that at least one of the following two conditions holds:

  • The sum of the two outcomes is at least X.
  • The absolute difference of the two outcomes is at least Y.

Each face of each die is equally likely, and the two dice are independent.

Constraints

  • 2 \le X \le 13
  • 0 \le Y \le 6
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

X Y

Output

Output the probability that the two outcomes satisfy at least one of the two conditions. Your answer is accepted if its absolute error from the true value is at most 10^{-9}.


Sample Input 1

9 3

Sample Output 1

0.555555555555555555555555555555

Let (x,y) denote the event that the dice show x and y.

  • The sum is at least 9 for (3,6),(4,5),(4,6),(5,4),(5,5),(5,6),(6,3),(6,4),(6,5),(6,6).
  • The difference is at least 3 for (1,4),(1,5),(1,6),(2,5),(2,6),(3,6),(4,1),(5,1),(5,2),(6,1),(6,2),(6,3).

At least one of these conditions holds for the following 20 pairs: (1,4),(1,5),(1,6),(2,5),(2,6),(3,6),(4,1),(4,5),(4,6),(5,1),(5,2),(5,4),(5,5),(5,6),(6,1),(6,2),(6,3),(6,4),(6,5),(6,6).

Thus, the answer is \dfrac{20}{36}=\dfrac59=0.5555555555\ldots.

Because an absolute error of at most 10^{-9} is allowed, outputs such as 0.5555555565 or 0.55555555456789 are accepted.


Sample Input 2

13 6

Sample Output 2

0

Neither the sum of two dice can be 13 or greater, nor can their difference be 6 or greater.

Thus, the answer is 0.


Sample Input 3

10 3

Sample Output 3

0.5
E - Minimum Glutton

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250 点

問題文

N 個の料理があり、i 個目の料理の甘さは A_i、しょっぱさは B_i です。

高橋君はこれらの N 個の料理を好きな順番で並べ、その順に食べようとします。

高橋君は並べた順番の通りに料理を食べていきますが、食べた料理の甘さの合計が X より大きくなるかしょっぱさの合計が Y より大きくなるとその時点で食べるのをやめます。

高橋君が食べることになる料理の個数としてあり得る最小値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq X, Y \leq 2 \times 10^{14}
  • 1 \leq A_i, B_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N X Y
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

答えを出力せよ。


入力例 1

4 7 18
2 3 5 1
8 8 1 4

出力例 1

2

i 個目の料理のことを料理 i と書きます。

高橋君が 4 個の料理を料理 2, 3, 1, 4 の順に並べ替えたとき、料理 2, 3 を食べた時点での食べた料理の甘さの合計が 8 となり 7 より大きくなります。したがってこの場合は高橋君が食べることになる料理の個数は 2 個です。

高橋君が食べる料理の個数が 1 個以下になることはないため、2 を出力します。


入力例 2

5 200000000000000 200000000000000
1 1 1 1 1
2 2 2 2 2

出力例 2

5

入力例 3

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

出力例 3

6

Score : 250 points

Problem Statement

There are N dishes, and the i-th dish has a sweetness of A_i and a saltiness of B_i.

Takahashi plans to arrange these N dishes in any order he likes and eat them in that order.

He will eat the dishes in the arranged order, but he will stop eating as soon as the total sweetness of the dishes he has eaten exceeds X or the total saltiness exceeds Y.

Find the minimum possible number of dishes that he will end up eating.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq X, Y \leq 2 \times 10^{14}
  • 1 \leq A_i, B_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N X Y
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

Print the answer.


Sample Input 1

4 7 18
2 3 5 1
8 8 1 4

Sample Output 1

2

The i-th dish will be denoted as dish i.

If he arranges the four dishes in the order 2, 3, 1, 4, as soon as he eats dishes 2 and 3, their total sweetness is 8, which is greater than 7. Therefore, in this case, he will end up eating two dishes.

The number of dishes he will eat cannot be 1 or less, so print 2.


Sample Input 2

5 200000000000000 200000000000000
1 1 1 1 1
2 2 2 2 2

Sample Output 2

5

Sample Input 3

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

Sample Output 3

6
F - Robot Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

子供と大人があわせて N 人います。i 番目の人の体重は W_i です。
それぞれの人が子供か大人かは、0 と 1 からなる長さ N の文字列 S によって表され、
S の i 文字目が 0 であるとき i 番目の人が子供であることを、1 であるとき i 番目の人が大人であることをさします。

ロボットである高橋君に対して実数 X を設定すると、 高橋君はそれぞれの人に対して、体重が X 未満なら子供、X 以上なら大人と判定します。
実数 X に対してf(X) を、高橋君に X を設定したときに N 人のうち子供か大人かを正しく判定できる人数で定めます。

X が実数全体を動くとき、f(X) の最大値を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • S は 0 と 1 からなる長さ N の文字列
  • 1\leq W_i\leq 10^9
  • N,W_i は整数

入力

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

N
S
W_1 W_2 \ldots W_N

出力

f(X) の最大値を整数で一行に出力せよ。


入力例 1

5
10101
60 45 30 40 80

出力例 1

4

X=50 と設定すると、高橋君は 2,3,4 番目の人を子供、 1,5 番目の人を大人と判定します。
実際には 2,4 番目の人が子供、 1,3,5 番目の人が大人であるので、このとき、1,2,4,5 番目の合計 4 人に対して正しく判定できています。 よって、f(50)=4 です。

5 人全員に対して正しく判定できるような X は存在しないのでこのときが最大です。よって、4 を出力します。


入力例 2

3
000
1 2 3

出力例 2

3

例えば、X=10 とすると最大値 f(10)=3 を達成します。
全員が大人、または全員が子供である可能性もあることに注意してください。


入力例 3

5
10101
60 50 50 50 60

出力例 3

4

例えば、X=55 とすると最大値 f(55)=4 を達成します。
同じ体重の人が複数人存在する可能性もあることに注意してください。

Score : 300 points

Problem Statement

There are N people, each of whom is either a child or an adult. The i-th person has a weight of W_i.
Whether each person is a child or an adult is specified by a string S of length N consisting of 0 and 1.
If the i-th character of S is 0, then the i-th person is a child; if it is 1, then the i-th person is an adult.

When Takahashi the robot is given a real number X, Takahashi judges a person with a weight less than X to be a child and a person with a weight more than or equal to X to be an adult.
For a real value X, let f(X) be the number of people whom Takahashi correctly judges whether they are children or adults.

Find the maximum value of f(X) for all real values of X.

Constraints

  • 1\leq N\leq 2\times 10^5
  • S is a string of length N consisting of 0 and 1.
  • 1\leq W_i\leq 10^9
  • N and W_i are integers.

Input

Input is given from Standard Input in the following format:

N
S
W_1 W_2 \ldots W_N

Output

Print the maximum value of f(X) as an integer in a single line.


Sample Input 1

5
10101
60 45 30 40 80

Sample Output 1

4

When Takahashi is given X=50, it judges the 2-nd, 3-rd, and 4-th people to be children and the 1-st and 5-th to be adults.
In reality, the 2-nd and 4-th are children, and the 1-st, 3-rd, and 5-th are adults, so the 1-st, 2-nd, 4-th, and 5-th people are correctly judged. Thus, f(50)=4.

This is the maximum since there is no X that judges correctly for all 5 people. Thus, 4 should be printed.


Sample Input 2

3
000
1 2 3

Sample Output 2

3

For example, X=10 achieves the maximum value f(10)=3.
Note that the people may be all children or all adults.


Sample Input 3

5
10101
60 50 50 50 60

Sample Output 3

4

For example, X=55 achieves the maximum value f(55)=4.
Note that there may be multiple people with the same weight.

G - Destroyer Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

N 行 10^9 列の格子状の区画に区切られた街に N 枚の壁があり、1 から N までの番号が割り振られています。
壁 i は上から i 行目、左から L_i 列目から R_i 列目の範囲にあります。(入出力例 1 の図も参考にしてください。)

高橋君は N 枚の壁をすべて破壊することにしました。
超人的な腕力を持つ高橋君は、1 回のパンチで連続する D 列にまとめてダメージを与えることができます。

  • より厳密に言い換えると、1 以上 10^9 - D + 1 以下の 整数 x を選んで、x 列目から x + D - 1 列目に (一部でも) 存在するすべての破壊されていない壁にパンチによるダメージを与えることができます。

壁は一部分でもダメージを受けると、パンチの衝撃波により全体が木っ端みじんに破壊されてしまいます。
(入出力例 1 の説明も参考にしてください。)

高橋君がすべての壁を破壊するには、少なくとも何回のパンチを放つ必要がありますか?

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq D \leq 10^9
  • 1 \leq L_i \leq R_i \leq 10^9
  • 入力はすべて整数である。

入力

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

N D
L_1 R_1
L_2 R_2
\vdots
L_N R_N

出力

すべての壁を破壊するのに必要なパンチの最少回数を出力せよ。


入力例 1

3 3
1 2
4 7
5 9

出力例 1

2

入力例 1 に対応する壁の配置を図示したものが下図です。

image

たとえば次のようにパンチを放つことで、 2 回のパンチですべての壁を破壊することができます。(以下の説明では、a 列目から b 列目までの範囲を \lbrack a, b \rbrack と表記します。)

  • まず、 \lbrack 2, 4 \rbrack にパンチを放つ。 \lbrack 2, 4 \rbrack に存在する壁である壁 1 と壁 2 はダメージを受け、破壊される。
  • 次に \lbrack 5, 7\rbrack にパンチを放つ。\lbrack 5, 7\rbrack に存在する壁 3 はダメージを受け、破壊される。

また、次の方法でもすべての壁を 2 回のパンチで破壊することができます。

  • まず、\lbrack 7, 9 \rbrack にパンチを放ち、壁 2 と壁 3 を破壊する。
  • 次に、\lbrack 1, 3 \rbrack にパンチを放ち、壁 1 を破壊する。

入力例 2

3 3
1 2
4 7
4 9

出力例 2

1

入出力例 1 と比べると、壁 3 の範囲が \lbrack 5, 9 \rbrack から \lbrack 4, 9 \rbrack に変わりました。
この場合は \lbrack 2, 4 \rbrack にパンチを放つことで 1 回ですべての壁を破壊できます。


入力例 3

5 2
1 100
1 1000000000
101 1000
9982 44353
1000000000 1000000000

出力例 3

3

Score : 400 points

Problem Statement

In a town divided into a grid with N rows and 10^9 columns, there are N walls, numbered 1 to N.
Wall i ranges from the L_i-th column to the R_i-th column from the left in the i-th row from the top. (See also the figure at Sample Input and Output 1.)

Takahashi has decided to destroy all N walls.
With his superhuman strength, his one punch can damage consecutive D columns at once.

  • More formally, he can choose an integer x between 1 and 10^9 - D + 1 (inclusive) to damage all walls that exist (or partly exist) in the x-th through (x + D - 1)-th columns and are not yet destroyed.

When a part of a wall is damaged, that whole wall will be destroyed by the impact of the punch.
(See also the figure at Sample Input and Output 1.)

At least how many times does Takahashi need to punch to destroy all walls?

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq D \leq 10^9
  • 1 \leq L_i \leq R_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the minimum number of punches needed to destroy all walls.


Sample Input 1

3 3
1 2
4 7
5 9

Sample Output 1

2

The figure below describes the arrangements of walls in Sample Input 1.

image

He can destroy all walls with two punches, such as the following. (Below, \lbrack a, b \rbrack denotes the range from the a-th through b-th columns.)

  • First, punch \lbrack 2, 4 \rbrack. The walls existing in \lbrack 2, 4 \rbrack ― Walls 1 and 2 ― are damaged and destroyed.
  • Second, punch \lbrack 5, 7 \rbrack. The wall existing in \lbrack 5, 7 \rbrack ― Wall 3 ― is damaged and destroyed.

It is also possible to destroy all walls with two punches in this way:

  • First, punch \lbrack 7, 9 \rbrack to destroy Walls 2 and 3.
  • Second, punch \lbrack 1, 3 \rbrack to destroy Wall 1.

Sample Input 2

3 3
1 2
4 7
4 9

Sample Output 2

1

The difference from Sample Input/Output 1 is that Wall 3 now covers \lbrack 4, 9 \rbrack, not \lbrack 5, 9 \rbrack.
In this case, he can punch \lbrack 2, 4 \rbrack to destroy all walls with one punch.


Sample Input 3

5 2
1 100
1 1000000000
101 1000
9982 44353
1000000000 1000000000

Sample Output 3

3
H - Flip Edge

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425 点

問題文

N 頂点 M 辺の有向グラフが与えられます。 i 番目 (1\leq i\leq M) の辺は頂点 u _ i から頂点 v _ i への有向辺です。

はじめ、あなたは頂点 1 におり、以下の操作を繰り返して頂点 N にたどり着きたいです。

  • 次の操作のうちのいずれかを行う。
    • 今いる頂点から辺をたどって移動する。コストが 1 かかる。より厳密には、今いる頂点を v として、v から u への有向辺が存在するような u を 1 つ選び、頂点 u へ移動する。
    • すべての辺の向きを反転する。コストが X かかる。より厳密には、この操作の直前に v から u への有向辺が存在しているとき、かつそのときに限り、この操作の直後に u から v への有向辺が存在する。

与えられるグラフにおいて、上の操作を繰り返して頂点 1 から頂点 N にたどり着くことができることが保証されます。

頂点 N にたどりつくまでにかかるコストの総和の最小値を求めてください。

制約

  • 2\leq N\leq2\times10 ^ 5
  • 1\leq M\leq2\times10 ^ 5
  • 1\leq X\leq10 ^ 9
  • 1\leq u _ i\leq N\ (1\leq i\leq M)
  • 1\leq v _ i\leq N\ (1\leq i\leq M)
  • 与えられるグラフにおいて、上記の操作を繰り返して頂点 1 から頂点 N にたどり着くことができる。
  • 入力はすべて整数

入力

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

N M X
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ M v _ M

出力

頂点 N にたどりつくまでにかかるコストの総和の最小値を出力せよ。


入力例 1

5 6 5
1 2
2 4
3 1
3 5
4 3
5 2

出力例 1

4

与えられたグラフは以下のようになります。

次のように操作を行うことで、コストの総和を 4 として頂点 5 にたどり着くことができます。

  • コストを 1 かけて頂点 2 に移動する。
  • コストを 1 かけて頂点 4 に移動する。
  • コストを 1 かけて頂点 3 に移動する。
  • コストを 1 かけて頂点 5 に移動する。

コストの総和を 3 以下として頂点 5 にたどり着くことはできないため、4 を出力してください。


入力例 2

5 6 1
1 2
2 4
3 1
3 5
4 3
5 2

出力例 2

3

与えられたグラフは入出力例 1 と同じですが、辺を反転させるのにかかるコストが異なります。

次のように操作を行うことで、コストの総和を 3 として頂点 5 にたどり着くことができます。

  • コストを 1 かけて頂点 2 に移動する。
  • コストを 1 かけてすべての辺を反転させる。
  • コストを 1 かけて頂点 5 に移動する。

コストの総和を 2 以下として頂点 5 にたどり着くことはできないため、3 を出力してください。


入力例 3

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

出力例 3

4294967299

答えが 32\operatorname{bit} 整数に収まらない場合があることに注意してください。


入力例 4

20 13 5
1 3
14 18
18 17
12 19
3 5
4 6
13 9
8 5
14 2
20 18
8 14
4 9
14 8

出力例 4

21

Score : 425 points

Problem Statement

You are given a directed graph with N vertices and M edges. The i-th edge (1 \leq i \leq M) is a directed edge from vertex u _ i to vertex v _ i.

Initially, you are at vertex 1. You want to repeat the following operations until you reach vertex N:

  • Perform one of the two operations below:
    • Move along a directed edge from your current vertex. This incurs a cost of 1. More precisely, if you are at vertex v, choose a vertex u such that there is a directed edge from v to u, and move to vertex u.
    • Reverse the direction of all edges. This incurs a cost of X. More precisely, if and only if there was a directed edge from v to u immediately before this operation, there is a directed edge from u to v immediately after this operation.

It is guaranteed that, for the given graph, you can reach vertex N from vertex 1 by repeating these operations.

Find the minimum total cost required to reach vertex N.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq X \leq 10^9
  • 1 \leq u _ i \leq N \ (1 \leq i \leq M)
  • 1 \leq v _ i \leq N \ (1 \leq i \leq M)
  • For the given graph, it is guaranteed that you can reach vertex N from vertex 1 by the operations described.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M X
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ M v _ M

Output

Print the minimum total cost required to reach vertex N.


Sample Input 1

5 6 5
1 2
2 4
3 1
3 5
4 3
5 2

Sample Output 1

4

The given graph looks like this:

You can reach vertex 5 with a total cost of 4 by doing the following:

  • Move to vertex 2 at a cost of 1.
  • Move to vertex 4 at a cost of 1.
  • Move to vertex 3 at a cost of 1.
  • Move to vertex 5 at a cost of 1.

It is impossible to reach vertex 5 with a total cost of 3 or less, so print 4.


Sample Input 2

5 6 1
1 2
2 4
3 1
3 5
4 3
5 2

Sample Output 2

3

The graph is the same as in Sample 1, but the cost to reverse edges is different.

You can reach vertex 5 with a total cost of 3 as follows:

  • Move to vertex 2 at a cost of 1.
  • Reverse all edges at a cost of 1.
  • Move to vertex 5 at a cost of 1.

It is impossible to reach vertex 5 with a total cost of 2 or less, so print 3.


Sample Input 3

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

Sample Output 3

4294967299

Note that the answer may exceed the 32-bit integer range.


Sample Input 4

20 13 5
1 3
14 18
18 17
12 19
3 5
4 6
13 9
8 5
14 2
20 18
8 14
4 9
14 8

Sample Output 4

21
I - Palindrome Query

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 525 点

問題文

英小文字からなる長さ N の文字列 S が与えられます。
以下で説明されるクエリを与えられる順に Q 個処理してください。
クエリは次の 2 種類のいずれかです。

  • 1 x c : S の x 文字目を英小文字 c に変更する。
  • 2 L R : S の L 文字目から R 文字目までからなる部分文字列が回文であるならば Yes を、そうでないならば No を出力する。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq Q \leq 10^5
  • S は英小文字からなる長さ N の文字列
  • 1 \leq x \leq N
  • c は英小文字
  • 1 \leq L \leq R \leq N
  • N, Q, x, L, R は整数

入力

入力は以下の形式で標準入力から与えられる。ここで \text{query}_i は i 番目に処理するクエリである。

N Q
S
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 x c
2 L R

出力

問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。


入力例 1

7 8
abcbacb
2 1 5
2 4 7
2 2 2
1 5 c
2 1 5
2 4 7
1 4 c
2 3 6

出力例 1

Yes
No
Yes
No
Yes
Yes

はじめ、S = abcbacb です。
1 番目のクエリについて、S の 1 文字目から 5 文字目までからなる文字列は abcba で、これは回文です。よって Yes を出力します。
2 番目のクエリについて、S の 4 文字目から 7 文字目までからなる文字列は bacb で、これは回文ではありません。よって No を出力します。
3 番目のクエリについて、S の 2 文字目から 2 文字目までからなる文字列は b で、これは回文です。よって Yes を出力します。
4 番目のクエリについて、S の 5 文字目を c に変更します。S は abcbccb になります。
5 番目のクエリについて、S の 1 文字目から 5 文字目までからなる文字列は abcbc で、これは回文ではありません。よって No を出力します。
6 番目のクエリについて、S の 4 文字目から 7 文字目までからなる文字列は bccb で、これは回文です。よって Yes を出力します。
7 番目のクエリについて、S の 4 文字目を c に変更します。S は abccccb になります。
8 番目のクエリについて、S の 3 文字目から 6 文字目までからなる文字列は cccc で、これは回文です。よって Yes を出力します。

Score : 525 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.
Process Q queries described below in the order they are given.
There are two types of queries:

  • 1 x c : Change the x-th character of S to the lowercase English letter c.
  • 2 L R : If the substring formed by the L-th through R-th characters of S is a palindrome, print Yes; otherwise, print No.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq Q \leq 10^5
  • S is a string of length N consisting of lowercase English letters.
  • 1 \leq x \leq N
  • c is a lowercase English letter.
  • 1 \leq L \leq R \leq N
  • N, Q, x, L, R are integers.

Input

The input is given from Standard Input in the following format. Here, \text{query}_i is the i-th query to be processed.

N Q
S
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in one of the following formats:

1 x c
2 L R

Output

Follow the instructions in the problem statement and print the answers to the queries, separated by newlines.


Sample Input 1

7 8
abcbacb
2 1 5
2 4 7
2 2 2
1 5 c
2 1 5
2 4 7
1 4 c
2 3 6

Sample Output 1

Yes
No
Yes
No
Yes
Yes

Initially, S = abcbacb.
For the first query, the string formed by the 1-st through 5-th characters of S is abcba, which is a palindrome. Thus, print Yes.
For the second query, the string formed by the 4-th through 7-th character of S is bacb, which is not a palindrome. Thus, print No.
For the third query, the string formed by the 2-nd through 2-nd character of S is b, which is a palindrome. Thus, output Yes.
For the fourth query, change the 5-th character of S to c. S becomes abcbccb.
For the fifth query, the string formed by the 1-st through 5-th character of S is abcbc, which is not a palindrome. Thus, output No.
For the sixth query, the string formed by the 4-th through 7-th character of S is bccb, which is a palindrome. Thus, output Yes.
For the seventh query, change the 4-th character of S to c. S becomes abccccb.
For the eighth query, the string formed by the 3-rd through 6-th character of cccc, which is a palindrome. Thus, output Yes.