A - Spread

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

配点 : 100

問題文

英大文字からなる文字列 S が与えられます。S の各文字を空白で区切り、その順で 1 文字ずつ出力してください。

制約

  • S は長さ 2 以上 100 以下の英大文字からなる文字列

入力

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

S

出力

S の各文字を空白で区切り、1 文字ずつ出力せよ。


入力例 1

ABC

出力例 1

A B C

A, B, C を空白で区切り、1 文字ずつ出力してください。

C の後ろに空白を出力する必要がないことに注意してください。


入力例 2

ZZZZZZZ

出力例 2

Z Z Z Z Z Z Z

入力例 3

OOXXOO

出力例 3

O O X X O O

Score : 100 points

Problem Statement

You are given a string S consisting of uppercase English letters. Separate each character of S with a space and print them one by one in order.

Constraints

  • S is a string consisting of uppercase English letters with a length between 2 and 100, inclusive.

Input

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

S

Output

Separate each character of S with a space and print them one by one.


Sample Input 1

ABC

Sample Output 1

A B C

Separate A, B, and C with spaces and print them one by one.

There is no need to print a space after C.


Sample Input 2

ZZZZZZZ

Sample Output 2

Z Z Z Z Z Z Z

Sample Input 3

OOXXOO

Sample Output 3

O O X X O O
B - Next

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

配点 : 200

問題文

N 個の整数 A_1, A_2, \ldots, A_N が与えられます。 このうち最大でない整数の中で最大である整数を求めてください。

ただし、この問題の制約下で答えは必ず存在します。

制約

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 100
  • A_1, A_2, \ldots, A_N がすべて等しいということはない
  • 入力値はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

5
2 1 3 3 2

出力例 1

2

2,1,3,3,2 の中で最大である整数は 3 です。

2,1,3,3,2 の中で 3 でない整数は 2,1,23 つであり、これらの中で最大である整数は 2 です。


入力例 2

4
4 3 2 1

出力例 2

3

入力例 3

8
22 22 18 16 22 18 18 22

出力例 3

18

Score : 200 points

Problem Statement

You are given N integers A_1, A_2, \ldots, A_N. Find the largest among those integers that are not the largest.

The constraints of this problem guarantee that the answer exists.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 100
  • It is not the case that all A_1, A_2, \ldots, A_N are equal.
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

5
2 1 3 3 2

Sample Output 1

2

The largest integer among 2,1,3,3,2 is 3.

The integers that are not 3 among 2,1,3,3,2 are 2,1,2, among which the largest is 2.


Sample Input 2

4
4 3 2 1

Sample Output 2

3

Sample Input 3

8
22 22 18 16 22 18 18 22

Sample Output 3

18
C - Count xxx

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

配点 : 300

問題文

英小文字からなる長さ N の文字列 S が与えられます。

S の空でない部分文字列であって、1 種類の文字のみからなるものの数を求めてください。 ただし、文字列として等しい部分文字列同士は、取り出し方が異なっても区別しません

なお、S の空でない部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のうち、長さが 1 以上であるもののことをいいます。 例えば、ababcabc の空でない部分文字列ですが、ac や空文字列は abc の空でない部分文字列ではありません。

制約

  • 1 \leq N \leq 2\times 10^5
  • S は英小文字からなる長さ N の文字列

入力

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

N
S

出力

S の空でない部分文字列であって、1 種類の文字のみからなるものの数を出力せよ。


入力例 1

6
aaabaa

出力例 1

4

S の空でない部分文字列であって、1 種類の文字のみからなるものは a, aa, aaa, b4 つです。 S から aaa を取り出す方法は 1 通りではありませんが、それぞれ 1 回ずつしか数えないことに注意してください。


入力例 2

1
x

出力例 2

1

入力例 3

12
ssskkyskkkky

出力例 3

8

Score : 300 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Find the number of non-empty substrings of S that are repetitions of one character. Here, two substrings that are equal as strings are not distinguished even if they are obtained differently.

A non-empty substring of S is a string of length at least one obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S. For example, ab and abc are non-empty substrings of abc, while ac and the empty string are not.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • S is a string of length N consisting of lowercase English letters.

Input

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

N
S

Output

Print the number of non-empty substrings of S that are repetitions of one character.


Sample Input 1

6
aaabaa

Sample Output 1

4

The non-empty substrings of S that are repetitions of one character are a, aa, aaa, and b; there are four of them. Note that there are multiple ways to obtain a or aa from S, but each should only be counted once.


Sample Input 2

1
x

Sample Output 2

1

Sample Input 3

12
ssskkyskkkky

Sample Output 3

8
D - Election Quick Report

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

配点 : 350

問題文

1, 2, \ldots, N の番号のついた N 人の候補者から当選者を 1 人選ぶ選挙において、M 票の投票がありました。

各票ではそれぞれちょうど 1 人が投票先として選ばれており、i 票目の投票先は候補者 A_i です。

これから 1 票目から順に開票を行い、 1 票ごとにその時点で開票が終了した場合の当選者を更新して表示します。

開票された票において最も得票数が多かった候補者が当選となります。ただし、最も得票数が多かった候補者が複数いる場合は、その中で最も番号の小さい候補者が当選となります。

i = 1, 2, \ldots, M について、1, 2, \ldots, i 票目のみを開票した場合の当選者を求めてください。

制約

  • 1 \leq N,M \leq 200000
  • 1 \leq A_i \leq N
  • 入力される数値はすべて整数

入力

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

N M
A_1 A_2 \ldots A_M

出力

M 行出力せよ。

i 行目には 1, 2, \ldots, i 票目のみを開票した場合の当選者の番号を出力せよ。


入力例 1

3 7
1 2 2 3 1 3 3

出力例 1

1
1
2
2
1
1
3

候補者 i の得票数を C_i で表すこととします。

  • 1 票目までが開票された時点では、(C_1, C_2, C_3) = (1, 0, 0) なので当選者は 1 です。
  • 2 票目までが開票された時点では、(C_1, C_2, C_3) = (1, 1, 0) なので当選者は 1 です。
  • 3 票目までが開票された時点では、(C_1, C_2, C_3) = (1, 2, 0) なので当選者は 2 です。
  • 4 票目までが開票された時点では、(C_1, C_2, C_3) = (1, 2, 1) なので当選者は 2 です。
  • 5 票目までが開票された時点では、(C_1, C_2, C_3) = (2, 2, 1) なので当選者は 1 です。
  • 6 票目までが開票された時点では、(C_1, C_2, C_3) = (2, 2, 2) なので当選者は 1 です。
  • 7 票目までが開票された時点では、(C_1, C_2, C_3) = (2, 2, 3) なので当選者は 3 です。

入力例 2

100 5
100 90 80 70 60

出力例 2

100
90
80
70
60

入力例 3

9 8
8 8 2 2 8 8 2 2

出力例 3

8
8
8
2
8
8
8
2

Score : 350 points

Problem Statement

There is an election to choose one winner from N candidates with candidate numbers 1, 2, \ldots, N, and there have been M votes cast.

Each vote is for exactly one candidate, with the i-th vote being for candidate A_i.

The votes will be counted in order from first to last, and after each vote is counted, the current winner will be updated and displayed.

The candidate with the most votes among those counted is the winner. If there are multiple candidates with the most votes, the one with the smallest candidate number is the winner.

For each i = 1, 2, \ldots, M, determine the winner when counting only the first i votes.

Constraints

  • 1 \leq N, M \leq 200000
  • 1 \leq A_i \leq N
  • All input values are integers.

Input

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

N M
A_1 A_2 \ldots A_M

Output

Print M lines.

The i-th line should contain the winner's candidate number when counting only the first i votes.


Sample Input 1

3 7
1 2 2 3 1 3 3

Sample Output 1

1
1
2
2
1
1
3

Let C_i denote the number of votes for candidate i.

  • After the first vote is counted, (C_1, C_2, C_3) = (1, 0, 0), so the winner is 1.
  • After the second vote is counted, (C_1, C_2, C_3) = (1, 1, 0), so the winner is 1.
  • After the third vote is counted, (C_1, C_2, C_3) = (1, 2, 0), so the winner is 2.
  • After the fourth vote is counted, (C_1, C_2, C_3) = (1, 2, 1), so the winner is 2.
  • After the fifth vote is counted, (C_1, C_2, C_3) = (2, 2, 1), so the winner is 1.
  • After the sixth vote is counted, (C_1, C_2, C_3) = (2, 2, 2), so the winner is 1.
  • After the seventh vote is counted, (C_1, C_2, C_3) = (2, 2, 3), so the winner is 3.

Sample Input 2

100 5
100 90 80 70 60

Sample Output 2

100
90
80
70
60

Sample Input 3

9 8
8 8 2 2 8 8 2 2

Sample Output 3

8
8
8
2
8
8
8
2
E - Stamp

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

配点 : 475

問題文

英大文字からなる長さ N の文字列 S と、英大文字からなる長さ M\ (\leq N) の文字列 T が与えられます。

# のみからなる長さ N の文字列 X があります。 以下の操作を好きな回数行うことで、XS に一致させることができるか判定してください。

  • X の中から連続する M 文字を選び、T で置き換える。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq M \leq \min(N, 5)
  • S は英大文字からなる長さ N の文字列
  • T は英大文字からなる長さ M の文字列

入力

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

N M
S
T

出力

XS に一致させることができるならば Yes を、できないならば No を出力せよ。


入力例 1

7 3
ABCBABC
ABC

出力例 1

Yes

以下、Xl 文字目から r 文字目までの部分を X[l:r] と表記します。

次のように操作を行うことで、XS に一致させることができます。

  1. X[3:5]T で置き換える。X= ##ABC## になる。 
  2. X[1:3]T で置き換える。X= ABCBC## になる。 
  3. X[5:7]T で置き換える。X= ABCBABC になる。 

入力例 2

7 3
ABBCABC
ABC

出力例 2

No

どのように操作を行っても、XS に一致させることはできません。


入力例 3

12 2
XYXXYXXYYYXY
XY

出力例 3

Yes

Score : 475 points

Problem Statement

You are given two strings: S, which consists of uppercase English letters and has length N, and T, which also consists of uppercase English letters and has length M\ (\leq N).

There is a string X of length N consisting only of the character #. Determine whether it is possible to make X match S by performing the following operation any number of times:

  • Choose M consecutive characters in X and replace them with T.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq M \leq \min(N, 5)
  • S is a string consisting of uppercase English letters with length N.
  • T is a string consisting of uppercase English letters with length M.

Input

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

N M
S
T

Output

Print Yes if it is possible to make X match S; print No otherwise.


Sample Input 1

7 3
ABCBABC
ABC

Sample Output 1

Yes

Below, let X[l:r] denote the part from the l-th through the r-th character of X.

You can make X match S by operating as follows.

  1. Replace X[3:5] with T. X becomes ##ABC##.
  2. Replace X[1:3] with T. X becomes ABCBC##.
  3. Replace X[5:7] with T. X becomes ABCBABC.

Sample Input 2

7 3
ABBCABC
ABC

Sample Output 2

No

No matter how you operate, it is impossible to make X match S.


Sample Input 3

12 2
XYXXYXXYYYXY
XY

Sample Output 3

Yes
F - Colored Ball

実行時間制限: 4 sec / メモリ制限: 1024 MB

配点 : 500

問題文

1, 2, \ldots, N の番号がついた N 個の箱があり、はじめ箱 i には色 C_i のボールが 1 つ入っています。

Q 個のクエリが与えられるので、これらを順に処理してください。

各クエリは整数の組 (a,b) によって与えられ、その内容は以下の通りです。

  • a のボールをすべて箱 b に移し、その後箱 b に何種類の色のボールが入っているかを出力する。

ただし、箱 a や箱 b が空の場合もあることに注意してください。

制約

  • 1 \leq N, Q \leq 200000
  • 1 \leq C_i \leq N
  • 1 \leq a, b \leq N
  • a \neq b
  • 入力される数値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ここで、 \text{query}_ii 番目のクエリを意味する。

N Q
C_1 C_2 \ldots C_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

各クエリは次の形式で与えられる。

a b

出力

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


入力例 1

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

出力例 1

1
2
1
1
3
  • 1 番目のクエリでは、箱 1 のボールをすべて箱 2 に移します。箱 2 には色 1 のボールが 2 つ入っている状態となるため、1 を出力します。

  • 2 番目のクエリでは、箱 6 のボールをすべて箱 4 に移します。箱 4 には色 2 のボールが 1 つ、色 3 のボールが 1 つ入っている状態となるため、2 を出力します。

  • 3 番目のクエリでは、箱 5 のボールをすべて箱 1 に移します。箱 1 には色 2 のボールが 1 つ入っている状態となるため、1 を出力します。

  • 4 番目のクエリでは、箱 3 のボールをすべて箱 6 に移します。箱 6 には色 1 のボールが 1 つ入っている状態となるため、1 を出力します。

  • 5 番目のクエリでは、箱 4 のボールをすべて箱 6 に移します。箱 6 には色 1 のボールが 1 つ、色 2 のボールが 1 つ、色 3 のボールが 1 つ入っている状態となるため、3 を出力します。


入力例 2

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

出力例 2

1
2
0

Score : 500 points

Problem Statement

There are N boxes numbered 1, 2, \ldots, N. Initially, box i contains one ball of color C_i.

You are given Q queries, which you should process in order.

Each query is given by a pair of integers (a,b) and asks you to do the following:

  • Move all the balls from box a to box b, and then print the number of different colors of balls in box b.

Here, the boxes a and b may be empty.

Constraints

  • 1 \leq N, Q \leq 200000
  • 1 \leq C_i \leq N
  • 1 \leq a, b \leq N
  • a \neq b
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \text{query}_i represents the i-th query:

N Q
C_1 C_2 \ldots C_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in the following format:

a b

Output

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


Sample Input 1

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

Sample Output 1

1
2
1
1
3
  • For the first query, move all the balls from box 1 to box 2. Box 2 now contains two balls of color 1, so print 1.

  • For the second query, move all the balls from box 6 to box 4. Box 4 now contains one ball of color 2 and one ball of color 3, so print 2.

  • For the third query, move all the balls from box 5 to box 1. Box 1 now contains one ball of color 2, so print 1.

  • For the fourth query, move all the balls from box 3 to box 6. Box 6 now contains one ball of color 1, so print 1.

  • For the fifth query, move all the balls from box 4 to box 6. Box 6 now contains one ball of color 1, one ball of color 2, and one ball of color 3, so print 3.


Sample Input 2

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

Sample Output 2

1
2
0
G - Delivery on Tree

実行時間制限: 3 sec / メモリ制限: 1024 MB

配点 : 625

問題文

N 頂点の二分木が与えられます。 頂点には 1 から N までの番号が付けられており、頂点 1 が根です。 i\ (1\leq i \leq N-1) 番目の辺は、頂点 i+1 と頂点 P_i\ (\leq i) を双方向に結んでいます。

この木の上には 1 個のカゴと M 個のボールがあります。 ボールには 1 から M までの番号が付けられており、各ボール j についてスタート頂点 S_jゴール頂点 T_j が定められています。 最初、カゴは空の状態で頂点 1 に置かれており、ボールはそれぞれのスタート頂点に置かれています。

あなたは、以下の操作を好きな回数、好きな順序で行うことができます。

  • 今カゴが置かれている頂点を v として、以下のいずれかを行う。
    • 頂点 v に繋がる辺を 1 つ選び、カゴをその辺に沿って動かして隣接する頂点に移動させる。 このとき、カゴの中に入っているボールも一緒に移動する。
    • スタート頂点が v であり、今もスタート頂点に置かれているようなボールを 1 つ選んで、カゴの中に入れる。 この操作は、元々カゴの中に入っているボールが K 個未満である場合にのみ行える(すなわち、カゴの中に K+1 個以上のボールを入れることはできない)。
    • ゴール頂点が v であり、今カゴの中に入っているようなボールを 1 つ選んでカゴから取り出し、頂点 v に置く。

全ての操作が終了した時点で、カゴは空の状態で頂点 1 に置かれており、ボールはそれぞれのゴール頂点に置かれているような操作列を良い操作列と呼びます。

カゴを何度も動かすのは疲れるので、カゴが動く経路は、全ての辺をちょうど 2 回ずつ通り頂点 1 に戻ってくるようなものに限定したいです。 そのような経路のうち、その経路に従ってカゴを動かす良い操作列が存在するようなものの数を 998244353 で割った余りを求めてください。

制約

  • 2\leq N \leq 10^4
  • 1\leq M \leq 2\times 10^5
  • 1\leq K \leq 10^3
  • 1\leq P_i \leq i
  • 全ての v\ (1\leq v \leq N) について、P_i=v を満たす i の数は高々 2
  • 1\leq S_j, T_j \leq N
  • S_j \neq T_j
  • 入力は全て整数

入力

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

N M K
P_1 P_2 \dots P_{N-1}
S_1 T_1
S_2 T_2
\vdots
S_M T_M

出力

全ての辺をちょうど 2 回ずつ通り頂点 1 に戻ってくるような経路のうち、その経路に従ってカゴを動かす良い操作列が存在するようなものの数を 998244353 で割った余りを出力せよ。


入力例 1

5 2 1
1 1 3 3
2 4
5 3

出力例 1

1

与えられるグラフは以下の図の通りです。頂点の側に書かれた丸と四角は、それぞれその番号のボールのスタート頂点とゴール頂点を表します。

全ての辺をちょうど 2 回ずつ通り頂点 1 に戻ってくるような経路のうち、その経路に従ってカゴを動かす良い操作列が存在するようなものは以下の 1 通りのみです。

具体的には、以下のような良い操作列を構成できます。

  1. カゴを頂点 2 に動かす。
  2. ボール 1 をカゴに入れる。
  3. カゴを頂点 1 に動かす。
  4. カゴを頂点 3 に動かす。
  5. カゴを頂点 4 に動かす。
  6. ボール 1 をカゴから出して頂点 4 に置く。
  7. カゴを頂点 3 に動かす。
  8. カゴを頂点 5 に動かす。
  9. ボール 2 をカゴに入れる。
  10. カゴを頂点 3 に動かす。
  11. ボール 2 をカゴから出して頂点 3 に置く。
  12. カゴを頂点 1 に動かす。

入力例 2

5 2 2
1 1 3 3
2 4
5 3

出力例 2

2

入出力例 1 から K の値が 1 増えています。 これにより、上述した経路に加えて、以下の経路についても良い操作列を構成できるようになります。


入力例 3

15 4 2
1 2 1 4 2 3 4 7 3 7 5 9 11 8
14 12
5 4
13 15
5 12

出力例 3

8

Score : 625 points

Problem Statement

You are given a binary tree with N vertices. The vertices are numbered 1 to N, with vertex 1 being the root. The i-th edge (1\leq i \leq N-1) connects vertex i+1 and vertex P_i\ (\leq i) bidirectionally.

There are one basket and M balls on this tree. The balls are numbered 1 to M, and each ball j has a designated start vertex S_j and goal vertex T_j. Initially, the basket is empty and placed at vertex 1, and the balls are placed at their respective start vertices.

You can perform the following operation any number of times in any order.

  • Let v be the current vertex where the basket is placed. Do one of the following:
    • Choose one edge connected to vertex v, and move the basket along that edge to the adjacent vertex. The balls inside the basket move together with it.
    • Choose a ball with the start vertex v that is still placed at v, and put it into the basket. This operation can only be performed if the basket contains fewer than K balls (that is, the basket cannot contain K+1 or more balls).
    • Choose a ball with the goal vertex v from the basket and take it out, placing it at vertex v.

A sequence of operations is called a good operation sequence if and only if the basket is eventually empty and placed at vertex 1, and the balls are eventually placed at their respective goal vertices.

Since it is tiring to move the basket many times, the basket's movement path should traverse each edge exactly twice before returning to vertex 1. Find the number, modulo 998244353, of those paths such that a good operation sequence exists following that path.

Constraints

  • 2\leq N \leq 10^4
  • 1\leq M \leq 2\times 10^5
  • 1\leq K \leq 10^3
  • 1\leq P_i \leq i
  • For every v\ (1\leq v \leq N), there are at most two i's such that P_i=v.
  • 1\leq S_j, T_j \leq N
  • S_j \neq T_j
  • All input values are integers.

Input

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

N M K
P_1 P_2 \dots P_{N-1}
S_1 T_1
S_2 T_2
\vdots
S_M T_M

Output

Print the number, modulo 998244353, of paths traversing every edge exactly twice before returning to vertex 1 such that a good operation sequence exists following that path.


Sample Input 1

5 2 1
1 1 3 3
2 4
5 3

Sample Output 1

1

The given graph is shown in the figure below. The circles and squares written next to the vertices represent the start and goal vertices of the balls, respectively.

Among the paths where every edge is traversed exactly twice before returning to vertex 1, there is only one path such that a good operation sequence exists following that path, which is shown below:

Here, you can construct the following good operation sequence:

  1. Move the basket to vertex 2.
  2. Put ball 1 into the basket.
  3. Move the basket to vertex 1.
  4. Move the basket to vertex 3.
  5. Move the basket to vertex 4.
  6. Take ball 1 out of the basket and place it at vertex 4.
  7. Move the basket to vertex 3.
  8. Move the basket to vertex 5.
  9. Put ball 2 into the basket.
  10. Move the basket to vertex 3.
  11. Take ball 2 out of the basket and place it at vertex 3.
  12. Move the basket to vertex 1.

Sample Input 2

5 2 2
1 1 3 3
2 4
5 3

Sample Output 2

2

The value of K has increased by 1 from Sample Input 1. This allows you to construct a good operation sequence for the following path, in addition to the one illustrated above.


Sample Input 3

15 4 2
1 2 1 4 2 3 4 7 3 7 5 9 11 8
14 12
5 4
13 15
5 12

Sample Output 3

8