A - 12435

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

配点 : 150 点

問題文

(1,2,3,4,5) を並び替えた整数列 A=(A_1,A_2,A_3,A_4,A_5) が与えられます。

A の隣り合う 2 つの項を入れ替える操作を ちょうど 1 回 行うことで A を昇順にすることができるか判定してください。

制約

  • A は (1,2,3,4,5) を並び替えてできる長さ 5 の整数列

入力

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

A_1 A_2 A_3 A_4 A_5

出力

ちょうど 1 回の操作で A を昇順にすることができるならば Yes を、できないならば No を出力せよ。


入力例 1

1 2 4 3 5

出力例 1

Yes

A_3 と A_4 を入れ替えることで A=(1,2,3,4,5) となり、 A を昇順に並び替えることができます。したがって、 Yes を出力してください。


入力例 2

5 3 2 4 1

出力例 2

No

どのような操作をしても A を昇順に並び替えることはできません。


入力例 3

1 2 3 4 5

出力例 3

No

ちょうど 1 回操作をする必要があります。


入力例 4

2 1 3 4 5

出力例 4

Yes

Score : 150 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,A_3,A_4,A_5) obtained by permuting (1,2,3,4,5).

Determine whether A can be sorted in ascending order by performing exactly one operation of swapping two adjacent elements in A.

Constraints

  • A is an integer sequence of length 5 obtained by permuting (1,2,3,4,5).

Input

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

A_1 A_2 A_3 A_4 A_5

Output

If A can be sorted in ascending order by exactly one operation, print Yes; otherwise, print No.


Sample Input 1

1 2 4 3 5

Sample Output 1

Yes

By swapping A_3 and A_4, A becomes (1,2,3,4,5), so it can be sorted in ascending order. Therefore, print Yes.


Sample Input 2

5 3 2 4 1

Sample Output 2

No

No matter what operation is performed, it is impossible to sort A in ascending order.


Sample Input 3

1 2 3 4 5

Sample Output 3

No

You must perform exactly one operation.


Sample Input 4

2 1 3 4 5

Sample Output 4

Yes
B - Yay!

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

配点 : 150 点

問題文

英小文字からなる文字列 S が与えられます。ここで S の長さは 3 以上 100 以下です。

S はある 1 文字を除いて全て同じ文字で構成されています。

他のどの文字とも異なる文字は前から何文字目でしょうか。

制約

  • S は 2 種類の英小文字からなる長さ 3 以上 100 以下の文字列
  • S はある 1 文字を除いて全て同じ文字

入力

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

S

出力

答えを出力せよ。


入力例 1

yay

出力例 1

2

yay の 2 文字目は、1 文字目とも 3 文字目とも異なります。


入力例 2

egg

出力例 2

1

入力例 3

zzzzzwz

出力例 3

6

Score: 150 points

Problem Statement

You are given a string S consisting of lowercase English letters. The length of S is between 3 and 100, inclusive.

All characters but one of S are the same.

Find x such that the x-th character of S differs from all other characters.

Constraints

  • S is a string of length between 3 and 100, inclusive, consisting of two different lowercase English letters.
  • All characters but one of S are the same.

Input

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

S

Output

Print the answer.


Sample Input 1

yay

Sample Output 1

2

The second character of yay differs from the first and third characters.


Sample Input 2

egg

Sample Output 2

1

Sample Input 3

zzzzzwz

Sample Output 3

6
C - レ

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

配点 : 200 点

問題文

高橋君は漢文の勉強をしていて、漢字を読む順番が分からず困っています。高橋君を助けましょう!

1 から N までの N 個の整数が小さい方から順に 1 列に並んでいます。
整数の間に M 個の「レ」が挟まっています。i 個目の「レ」は、整数 a_i と整数 a_i + 1 の間にあります。

あなたは次の手順に従って、N 個の整数を 1 回ずつ全て読みます。

  • まず、頂点に 1 から N までの番号がついた N 頂点 M 辺の無向グラフ G を考える。i 本目の辺は頂点 a_i と頂点 a_i + 1 を結んでいる。
  • そして、読まれていない整数が無くなるまで次の操作を繰り返す。
    • 読まれていない整数のうち最小のものを x とする。頂点 x が含まれる連結成分 C を選び、C に含まれる頂点の番号を大きい方から順に全て読む。

例えば、整数と「レ」が

image

という順番で並んでいる場合を考えます。(この場合 N = 5, M = 3, a = (1, 3, 4) です。)
このとき、整数が読まれる順番は以下の手順によって 2, 1, 5, 4, 3 に決定します。

  • 最初、読まれていない整数のうち最小のものは 1 であり、グラフ G の頂点 1 を含む連結成分に含まれる頂点は \lbrace 1, 2 \rbrace である。よって 2, 1 がこの順で読まれる。
  • 次に、読まれていない整数のうち最小のものは 3 であり、グラフ G の頂点 3 を含む連結成分に含まれる頂点は \lbrace 3, 4, 5 \rbrace である。よって 5, 4, 3 がこの順で読まれる。
  • すべての整数が読まれたので手順を終了する。

N, M, (a_1, a_2, \dots, a_M) が入力として与えられるので、 N 個の整数を読む順番を出力してください。

連結成分とは あるグラフの 部分グラフ とは、元のグラフのいくつかの頂点といくつかの辺を選んでできるグラフのことをいいます。
グラフが 連結 であるとは、グラフに含まれるすべての頂点同士が辺を経由して互いに行き来できることをいいます。
連結成分 とは、連結な部分グラフのうち、そのグラフを含んだより大きい連結な部分グラフが存在しないものをいいます。

制約

  • 1 \leq N \leq 100
  • 0 \leq M \leq N - 1
  • 1 \leq a_1 \lt a_2 \lt \dots \lt a_M \leq N-1
  • 入力される値は全て整数

入力

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

N M
a_1 a_2 \dots a_M

出力

答えを以下の形式で出力せよ。ここで p_i は、i 番目に読まれる整数を意味する。

p_1 p_2 \dots p_N

入力例 1

5 3
1 3 4

出力例 1

2 1 5 4 3

問題文の例にある通り、整数と「レ」が

image

という順で並んでいる場合は 2, 1, 5, 4, 3 の順で読みます。


入力例 2

5 0

出力例 2

1 2 3 4 5

「レ」が存在しない場合もあります。


入力例 3

10 6
1 2 3 7 8 9

出力例 3

4 3 2 1 5 6 10 9 8 7

Score : 200 points

Problem Statement

Studying Kanbun, Takahashi is having trouble figuring out the order to read words. Help him out!

There are N integers from 1 through N arranged in a line in ascending order.
Between them are M "レ" marks. The i-th "レ" mark is between the integer a_i and integer (a_i + 1).

You read each of the N integers once by the following procedure.

  • Consider an undirected graph G with N vertices numbered 1 through N and M edges. The i-th edge connects vertex a_i and vertex (a_i+1).
  • Repeat the following operation until there is no unread integer:
    • let x be the minimum unread integer. Choose the connected component C containing vertex x, and read all the numbers of the vertices contained in C in descending order.

For example, suppose that integers and "レ" marks are arranged in the following order:

image

(In this case, N = 5, M = 3, and a = (1, 3, 4).)
Then, the order to read the integers is determined to be 2, 1, 5, 4, and 3, as follows:

  • At first, the minimum unread integer is 1, and the connected component of G containing vertex 1 has vertices \lbrace 1, 2 \rbrace, so 2 and 1 are read in this order.
  • Then, the minimum unread integer is 3, and the connected component of G containing vertex 3 has vertices \lbrace 3, 4, 5 \rbrace, so 5, 4, and 3 are read in this order.
  • Now that all integers are read, terminate the procedure.

Given N, M, and (a_1, a_2, \dots, a_M), print the order to read the N integers.

What is a connected component? A subgraph of a graph is a graph obtained by choosing some vertices and edges from the original graph.
A graph is said to be connected if and only if one can travel between any two vertices in the graph via edges.
A connected component is a connected subgraph that is not contained in any larger connected subgraph.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq M \leq N - 1
  • 1 \leq a_1 \lt a_2 \lt \dots \lt a_M \leq N-1
  • All values in the input are integers.

Input

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

N M
a_1 a_2 \dots a_M

Output

Print the answer in the following format, where p_i is the i-th integers to read.

p_1 p_2 \dots p_N

Sample Input 1

5 3
1 3 4

Sample Output 1

2 1 5 4 3

As described in the Problem Statement, if integers and "レ" marks are arranged in the following order:

image

then the integers are read in the following order: 2, 1, 5, 4, and 3.


Sample Input 2

5 0

Sample Output 2

1 2 3 4 5

There may be no "レ" mark.


Sample Input 3

10 6
1 2 3 7 8 9

Sample Output 3

4 3 2 1 5 6 10 9 8 7
D - Spot the Difference

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

配点 : 150 点

問題文

縦 N マス、横 N マスのグリッドが 2 個与えられます。それぞれグリッド A, グリッド B と呼びます。
グリッドの各マスには英小文字が書かれています。
グリッド A の上から i 行目、左から j 列目に書かれている文字は A_{i, j} です。
グリッド B の上から i 行目、左から j 列目に書かれている文字は B_{i, j} です。

2 つのグリッドは 1 ヵ所だけ書かれている文字が異なります。すなわち、A_{i, j} \neq B_{i, j} である N 以下の正整数の組 (i, j) はちょうど 1 個存在します。この (i, j) を求めてください。

制約

  • 1 \leq N \leq 100
  • A_{i, j}, B_{i, j} は全て英小文字
  • A_{i, j} \neq B_{i, j} である (i, j) がちょうど 1 個存在する

入力

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

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
B_{1,1}B_{1,2}\dots B_{1,N}
B_{2,1}B_{2,2}\dots B_{2,N}
\vdots
B_{N,1}B_{N,2}\dots B_{N,N}

出力

A_{i, j} \neq B_{i, j} である N 以下の正整数の組を (i, j) とする。この時、(i, j) を以下の形式で出力せよ。

i j

入力例 1

3
abc
def
ghi
abc
bef
ghi

出力例 1

2 1

A_{2, 1} = d、B_{2, 1} = b なので A_{2, 1} \neq B_{2, 1} が成り立つため、(i, j) = (2, 1) は問題文の条件を満たします。


入力例 2

1
f
q

出力例 2

1 1

入力例 3

10
eixfumagit
vtophbepfe
pxbfgsqcug
ugpugtsxzq
bvfhxyehfk
uqyfwtmglr
jaitenfqiq
acwvufpfvv
jhaddglpva
aacxsyqvoj
eixfumagit
vtophbepfe
pxbfgsqcug
ugpugtsxzq
bvfhxyehok
uqyfwtmglr
jaitenfqiq
acwvufpfvv
jhaddglpva
aacxsyqvoj

出力例 3

5 9

Score: 150 points

Problem Statement

You are given two grids, each with N rows and N columns, referred to as grid A and grid B.
Each cell in the grids contains a lowercase English letter.
The character at the i-th row and j-th column of grid A is A_{i, j}.
The character at the i-th row and j-th column of grid B is B_{i, j}.

The two grids differ in exactly one cell. That is, there exists exactly one pair (i, j) of positive integers not greater than N such that A_{i, j} \neq B_{i, j}. Find this (i, j).

Constraints

  • 1 \leq N \leq 100
  • A_{i, j} and B_{i, j} are all lowercase English letters.
  • There exists exactly one pair (i, j) such that A_{i, j} \neq B_{i, j}.

Input

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

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
B_{1,1}B_{1,2}\dots B_{1,N}
B_{2,1}B_{2,2}\dots B_{2,N}
\vdots
B_{N,1}B_{N,2}\dots B_{N,N}

Output

Let (i, j) be the pair of positive integers not greater than N such that A_{i, j} \neq B_{i, j}. Print (i, j) in the following format:

i j

Sample Input 1

3
abc
def
ghi
abc
bef
ghi

Sample Output 1

2 1

From A_{2, 1} = d and B_{2, 1} = b, we have A_{2, 1} \neq B_{2, 1}, so (i, j) = (2, 1) satisfies the condition in the problem statement.


Sample Input 2

1
f
q

Sample Output 2

1 1

Sample Input 3

10
eixfumagit
vtophbepfe
pxbfgsqcug
ugpugtsxzq
bvfhxyehfk
uqyfwtmglr
jaitenfqiq
acwvufpfvv
jhaddglpva
aacxsyqvoj
eixfumagit
vtophbepfe
pxbfgsqcug
ugpugtsxzq
bvfhxyehok
uqyfwtmglr
jaitenfqiq
acwvufpfvv
jhaddglpva
aacxsyqvoj

Sample Output 3

5 9
E - Lock All Doors

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

配点 : 300 点

問題文

N + 1 個の部屋が一列に並んでおり、順に 0, 1, \ldots, N の番号が付けられています。

部屋の間には N 個のドアがあり、1, 2, \ldots, N の番号が付けられています。ドア i は部屋 i - 1 と部屋 i の間にあります。

各ドアについて鍵の状態を表す値 L_i が与えられ、L_i = 0 のときドア i の鍵は開いており、L_i = 1 のときドア i の鍵は閉まっています。

高橋君ははじめ部屋 R におり、ドア i の鍵が開いているときに限り、部屋 i - 1 と部屋 i の間を移動することができます。また、高橋君は部屋 i - 1 または部屋 i にいるときに限り、ドア i の鍵に対して 開閉操作 を行うことができます。ドア i の鍵に対して開閉操作を行ったとき、その鍵が開いているときは閉まり、閉まっているときは開きます。

すべてのドアの鍵が閉まった状態にするために行う鍵の開閉操作の回数として考えられる最小値を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq R \leq N
  • L_i \in \lbrace 0, 1 \rbrace
  • 入力される値はすべて整数

入力

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

N R
L_1 L_2 \ldots L_N

出力

答えを出力せよ。


入力例 1

6 3
1 0 0 1 0 0

出力例 1

6

高橋君は以下のように行動することで 6 回の開閉操作ですべてのドアの鍵が閉まった状態にすることができます。

  • 部屋 2 に移動する。
  • ドア 2 に対して開閉操作を行い、ドア 2 の鍵を閉める。
  • 部屋 3 に移動する。
  • ドア 4 に対して開閉操作を行い、ドア 4 の鍵を開ける。
  • ドア 3 に対して開閉操作を行い、ドア 3 の鍵を閉める。
  • 部屋 4 に移動する。
  • ドア 4 に対して開閉操作を行い、ドア 4 の鍵を閉める。
  • 部屋 5 に移動する。
  • ドア 5 に対して開閉操作を行い、ドア 5 の鍵を閉める。
  • ドア 6 に対して開閉操作を行い、ドア 6 の鍵を閉める。

入力例 2

2 1
0 0

出力例 2

2

入力例 3

8 2
0 1 0 0 1 0 1 1

出力例 3

8

Score : 300 points

Problem Statement

There are N + 1 rooms arranged in a line, numbered 0, 1, \ldots, N in order.

Between the rooms, there are N doors numbered 1, 2, \ldots, N. Door i is between rooms i - 1 and i.

For each door, a value L_i representing the lock state is given. When L_i = 0, door i is unlocked, and when L_i = 1, door i is locked.

Takahashi is initially in room R, and can move between rooms i - 1 and i only when door i is unlocked. Also, he can perform a switching operation on door i only when he is in room i - 1 or room i. When a switching operation is performed on door i, if the door is unlocked, it becomes locked, and if it is locked, it becomes unlocked.

Find the minimum number of switching operations needed to make all doors locked.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq R \leq N
  • L_i \in \lbrace 0, 1 \rbrace
  • All input values are integers.

Input

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

N R
L_1 L_2 \ldots L_N

Output

Output the answer.


Sample Input 1

6 3
1 0 0 1 0 0

Sample Output 1

6

Takahashi can make all doors locked with six switching operations by acting as follows:

  • Move to room 2.
  • Perform a switching operation on door 2 to lock door 2.
  • Move to room 3.
  • Perform a switching operation on door 4 to unlock door 4.
  • Perform a switching operation on door 3 to lock door 3.
  • Move to room 4.
  • Perform a switching operation on door 4 to lock door 4.
  • Move to room 5.
  • Perform a switching operation on door 5 to lock door 5.
  • Perform a switching operation on door 6 to lock door 6.

Sample Input 2

2 1
0 0

Sample Output 2

2

Sample Input 3

8 2
0 1 0 0 1 0 1 1

Sample Output 3

8
F - Perfect Standings

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

配点 : 300 点

問題文

高橋くんは、プログラミングコンテストを主催することにしました。

コンテストは A 問題、B 問題、C 問題、D 問題、E 問題の 5 問からなり、それぞれの配点は a 点、b 点、c 点、d 点、e 点です。

コンテストには 31 人が参加し、全員が 1 問以上解きました。

より具体的には、文字列 ABCDE の空でない(連続するとは限らない)部分列すべてについて、その部分列を名前とする参加者が存在し、その参加者は名前に含まれる文字に対応する問題をすべて解き、それ以外の問題は解きませんでした。

例えば、A さんは A 問題のみを、BCE さんは B 問題、C 問題、E 問題を解きました。

参加者の名前を、取った点数が大きいほうから順に出力してください。 ただし、参加者が取った点数は、その参加者が解いた問題の配点の合計です。

ただし、同じ点数を獲得した参加者については、名前が辞書順で小さいほうを先に出力してください。

辞書順で小さいとは?

辞書順とは、一言で説明すると「単語が辞書に載っている順番」を意味します。

より厳密には、英大文字からなる相異なる文字列 S,T について、S が T より辞書順で小さいとは、以下の条件のどちらかが成り立つことを意味します。

  • S の長さ |S| が T の長さより短く、T の先頭 |S| 文字が S と一致する
  • ある整数 1\leq i\leq\min\lbrace|S|,|T|\rbrace が存在して、次の 2 つの条件を両方を満たす
    • 1\leq j\lt i を満たすすべての整数 j に対して S の j 文字目と T の j 文字目が等しい
    • S の i 文字目が T の i 文字目よりアルファベット順で小さい

例えば、S= AB ,T= ABC とすると、ひとつめの条件が成り立つため S は T より小さいです。 また、S= ABD ,T= ACD とすると、ふたつめの条件が i=2 で成り立つため S は T より小さいです。

制約

  • 100\leq a\leq b\leq c\leq d\leq e\leq 2718
  • 入力はすべて整数

入力

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

a b c d e

出力

31 行出力せよ。 i 行目 (1\leq i\leq 31) には、i 番目に高い得点を獲得した参加者の名前を出力せよ。 同じ得点を獲得した参加者については、それらのうち辞書順で小さい名前をもつ参加者を先に出力せよ。


入力例 1

400 500 600 700 800

出力例 1

ABCDE
BCDE
ACDE
ABDE
ABCE
ABCD
CDE
BDE
ADE
BCE
ACE
BCD
ABE
ACD
ABD
ABC
DE
CE
BE
CD
AE
BD
AD
BC
AC
AB
E
D
C
B
A

それぞれの参加者の得点は以下のようになります。

例えば、ADE さんと BCE さんは同じ得点を獲得していますが、ADE さんのほうが辞書順で小さい名前をもつため、ADE さんを先に出力してください。


入力例 2

800 800 900 900 1000

出力例 2

ABCDE
ACDE
BCDE
ABCE
ABDE
ABCD
CDE
ACE
ADE
BCE
BDE
ABE
ACD
BCD
ABC
ABD
CE
DE
AE
BE
CD
AC
AD
BC
BD
AB
E
C
D
A
B

入力例 3

128 256 512 1024 2048

出力例 3

ABCDE
BCDE
ACDE
CDE
ABDE
BDE
ADE
DE
ABCE
BCE
ACE
CE
ABE
BE
AE
E
ABCD
BCD
ACD
CD
ABD
BD
AD
D
ABC
BC
AC
C
AB
B
A

Score : 300 points

Problem Statement

Takahashi decided to hold a programming contest.

The contest consists of five problems: A, B, C, D, E, with scores a, b, c, d, e, respectively.

There are 31 participants, and all of them solved at least one problem.

More specifically, for every non-empty subsequence (not necessarily contiguous) of the string ABCDE, there is a participant named after that subsequence who solved the problems corresponding to the letters in their name and did not solve the other problems.

For example, participant A solved only problem A, and participant BCE solved problems B, C, and E.

Print the names of the participants in order of their obtained scores, from the largest to the smallest. The score obtained by a participant is the sum of the scores of the problems they solved.

If two participants obtained the same score, print the one whose name is lexicographically smaller first.

What does "lexicographically smaller" mean?

In short, "lexicographically smaller" refers to the order in which words would appear in a dictionary.

More precisely, for distinct strings S,T consisting of uppercase English letters, S is lexicographically smaller than T if either of the following conditions holds:

  • The length |S| of S is less than the length of T, and the first |S| characters of T match S.
  • There exists an integer 1\leq i\leq\min\{ |S|,|T|\} that satisfy both of the following two conditions:
    • For every integer j with 1\leq j\lt i, the j-th character of S equals the j-th character of T.
    • The i-th character of S is alphabetically smaller than the i-th character of T.

For example, if S= AB and T= ABC, the first condition holds, so S is lexicographically smaller than T. If S= ABD and T= ACD, the second condition holds for i=2, so S is lexicographically smaller than T.

Constraints

  • 100\leq a\leq b\leq c\leq d\leq e\leq 2718
  • All input values are integers.

Input

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

a b c d e

Output

Print 31 lines. The i-th line (1\leq i\leq 31) should contain the name of the participant who obtained the i-th highest score. If multiple participants have the same score, print them in lexicographical order.


Sample Input 1

400 500 600 700 800

Sample Output 1

ABCDE
BCDE
ACDE
ABDE
ABCE
ABCD
CDE
BDE
ADE
BCE
ACE
BCD
ABE
ACD
ABD
ABC
DE
CE
BE
CD
AE
BD
AD
BC
AC
AB
E
D
C
B
A

The score of each participant is as follows:

For example, ADE and BCE obtained the same score, and ADE is lexicographically smaller, so print ADE before BCE.


Sample Input 2

800 800 900 900 1000

Sample Output 2

ABCDE
ACDE
BCDE
ABCE
ABDE
ABCD
CDE
ACE
ADE
BCE
BDE
ABE
ACD
BCD
ABC
ABD
CE
DE
AE
BE
CD
AC
AD
BC
BD
AB
E
C
D
A
B

Sample Input 3

128 256 512 1024 2048

Sample Output 3

ABCDE
BCDE
ACDE
CDE
ABDE
BDE
ADE
DE
ABCE
BCE
ACE
CE
ABE
BE
AE
E
ABCD
BCD
ACD
CD
ABD
BD
AD
D
ABC
BC
AC
C
AB
B
A
G - Ulam-Warburton Automaton

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

配点 : 425 点

問題文

H 行 W 列のマス目があります。上から i 行目 (1\leq i\leq H)、左から j 列目 (1\leq j\leq W) のマスをマス (i,j) と呼ぶことにします。

はじめ、マス (i,j) は S_{i,j} が # のとき黒、. のとき白で塗られています。

以下の操作を 10^{100} 回行います。

  • 白で塗られているマスであって、そのマスに辺で隣接するマスのうちちょうど 1 つが黒で塗られているようなマスの集合を T とする。T の各マスに対して、そのマスを黒で塗る。ただし、2 つのマス (i_1,j_1),(i_2,j_2) が辺で隣接するとは、 |i_1-i_2|+|j_1-j_2|=1 であることをいう。

すべての操作が終わったあとに黒く塗られているマスの個数を求めてください。

制約

  • 2\leq H,W
  • HW\leq 3\times 10^5
  • H,W は整数
  • S_{i,j} は # または .

入力

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

H W
S_{1,1}S_{1,2}\ldots S_{1,W}
S_{2,1}S_{2,2}\ldots S_{2,W}
\vdots
S_{H,1}S_{H,2}\ldots S_{H,W}

出力

答えを出力せよ。


入力例 1

9 9
.........
.........
.........
.........
....#....
.........
.........
.........
.........

出力例 1

57

操作によってマス目は以下のように変化します。(上の数字は操作回数を表しており、10 回目の操作後まで表示しています。)

最終的に黒く塗られたマスは 57 個です。


入力例 2

2 2
..
..

出力例 2

0

入力例 3

10 10
..........
....#.....
#.......#.
......#...
.......#..
.....#....
..........
..........
..#...#...
.......#..

出力例 3

64

Score : 425 points

Problem Statement

There is a grid with H rows and W columns. We call the cell at the i-th row from the top (1\leq i\leq H) and j-th column from the left (1\leq j\leq W) as cell (i,j).

Initially, cell (i,j) is colored black if S_{i,j} is # and white if it is ..

Perform the following operation 10^{100} times:

  • Let T be the set of cells that are colored white and have exactly one adjacent cell colored black. Color each cell in T black. Here, two cells (i_1,j_1) and (i_2,j_2) are adjacent if and only if |i_1-i_2|+|j_1-j_2|=1.

Find the number of cells that are colored black after all operations are completed.

Constraints

  • 2\leq H,W
  • HW\leq 3\times 10^5
  • H and W are integers.
  • S_{i,j} is # or ..

Input

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

H W
S_{1,1}S_{1,2}\ldots S_{1,W}
S_{2,1}S_{2,2}\ldots S_{2,W}
\vdots
S_{H,1}S_{H,2}\ldots S_{H,W}

Output

Output the answer.


Sample Input 1

9 9
.........
.........
.........
.........
....#....
.........
.........
.........
.........

Sample Output 1

57

The grid changes as follows through the operations. (The number above represents the operation count. The first ten operations are displayed.)

Eventually, 57 cells are colored black.


Sample Input 2

2 2
..
..

Sample Output 2

0

Sample Input 3

10 10
..........
....#.....
#.......#.
......#...
.......#..
.....#....
..........
..........
..#...#...
.......#..

Sample Output 3

64
H - Mex and Update

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

配点 : 475 点

問題文

長さ N の数列 A=(A_1,A_2,\dots,A_N) が与えられます。
以下の Q 個のクエリに、与えられる順番で対応してください。

k 番目のクエリは以下の形式で与えられます。

i_k x_k
  • まず、 A_{i_k} = x_k と変更する。この変更は以降のクエリにも引き継がれる。
  • その後、 A の \rm{mex} を出力する。
    • A の \rm{mex} とは、 A に含まれない最小の非負整数を指す。

制約

  • 入力は全て整数
  • 1 \le N,Q \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • 1 \le i_k \le N
  • 0 \le x_k \le 10^9

入力

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

N Q
A_1 A_2 \dots A_N
i_1 x_1
i_2 x_2
\vdots
i_Q x_Q

出力

全体で Q 行出力せよ。
そのうち k 行目には k 番目のクエリに対する答えを整数として出力せよ。


入力例 1

8 5
2 0 2 2 1 1 2 5
4 3
4 4
6 3
8 1000000000
2 1

出力例 1

4
3
6
5
0

最初、数列 A は (2,0,2,2,1,1,2,5) です。
この入力では、 5 つのクエリを処理します。

  • 1 番目のクエリで A_4 = 3 と変更し、 A=(2,0,2,3,1,1,2,5) となりました。
    • この時点で、 A の \rm{mex} は 4 です。
  • 2 番目のクエリで A_4 = 4 と変更し、 A=(2,0,2,4,1,1,2,5) となりました。
    • この時点で、 A の \rm{mex} は 3 です。
  • 3 番目のクエリで A_6 = 3 と変更し、 A=(2,0,2,4,1,3,2,5) となりました。
    • この時点で、 A の \rm{mex} は 6 です。
  • 4 番目のクエリで A_8 = 1000000000 と変更し、 A=(2,0,2,4,1,3,2,1000000000) となりました。
    • この時点で、 A の \rm{mex} は 5 です。
  • 5 番目のクエリで A_2 = 1 と変更し、 A=(2,1,2,4,1,3,2,1000000000) となりました。
    • この時点で、 A の \rm{mex} は 0 です。

Score : 475 points

Problem Statement

You are given a sequence A=(A_1,A_2,\dots,A_N) of length N.
Respond to the following Q queries in the order they are given.

The k-th query is given in the following format:

i_k x_k
  • First, change A_{i_k} to x_k. This change will carry over to subsequent queries.
  • Then, print the \rm{mex} of A.
    • The \rm{mex} of A is the smallest non-negative integer not contained in A.

Constraints

  • All input values are integers.
  • 1 \le N,Q \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • 1 \le i_k \le N
  • 0 \le x_k \le 10^9

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 \dots A_N
i_1 x_1
i_2 x_2
\vdots
i_Q x_Q

Output

Print Q lines in total.
The k-th line should contain the answer to the k-th query as an integer.


Sample Input 1

8 5
2 0 2 2 1 1 2 5
4 3
4 4
6 3
8 1000000000
2 1

Sample Output 1

4
3
6
5
0

Initially, the sequence A is (2,0,2,2,1,1,2,5).
This input gives you five queries.

  • The first query changes A_4 to 3, making A=(2,0,2,3,1,1,2,5).
    • At this point, the \rm{mex} of A is 4.
  • The second query changes A_4 to 4, making A=(2,0,2,4,1,1,2,5).
    • At this point, the \rm{mex} of A is 3.
  • The third query changes A_6 to 3, making A=(2,0,2,4,1,3,2,5).
    • At this point, the \rm{mex} of A is 6.
  • The fourth query changes A_8 to 1000000000, making A=(2,0,2,4,1,3,2,1000000000).
    • At this point, the \rm{mex} of A is 5.
  • The fifth query changes A_2 to 1, making A=(2,1,2,4,1,3,2,1000000000).
    • At this point, the \rm{mex} of A is 0.
I - Road Blocked

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

配点 : 550 点

問題文

AtCoder国には 1 から N の番号がついた N 個の都市と、1 から M の番号がついた M 本の道路があります。
道路 i は都市 A_i と都市 B_i を双方向に結び長さは C_i です。

Q 個のクエリが与えられるので順に処理してください。クエリは以下の 2 種類のどちらかです。

  • 1 i:道路 i が通行止めとなる。
  • 2 x y:通行止めでない道路のみを通るときの都市 x から都市 y への最短距離を出力する。都市 x から都市 y に到達できないときは代わりに -1 を出力する。

なお、1 種類目のクエリはテストケースごとに 300 回以下であることが保証されます。

制約

  • 2 \leq N \leq 300
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1\leq A_i < B_i \leq N
  • (A_i,B_i) は相異なる
  • 1 \leq C_i \leq 10^9
  • 1 \leq Q \leq 2\times 10^5
  • 1 種類目のクエリにおいて、1\leq i \leq M
  • 1 種類目のクエリにおいて与えられる道路は、その時点で通行止めでない
  • 1 種類目のクエリは 300 回以下である
  • 2 種類目のクエリにおいて、1\leq x < y \leq N
  • 入力は全て整数である

入力

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

N M Q
A_1 B_1 C_1
\vdots
A_M B_M C_M
\mathrm{query}_1
\vdots
\mathrm{query}_Q

各クエリは次の 2 種類の形式のどちらかである。

1 i
2 x y

出力

クエリを順に処理せよ。


入力例 1

3 3 5
1 2 5
1 3 10
2 3 6
2 1 3
1 2
2 1 3
1 1
2 1 3

出力例 1

10
11
-1
  • 1 番目のクエリでは、都市 1 から都市 3 への最短距離である 10 を出力します。
  • 2 番目のクエリにより、道路 2 が通行止めとなりました。
  • 3 番目のクエリでは、都市 1 から都市 3 への最短距離である 11 を出力します。
  • 4 番目のクエリにより、道路 1 が通行止めとなりました。
  • 5 番目のクエリでは、都市 1 から都市 3 には到達できないので、-1 を出力します。

入力例 2

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

出力例 2

-1
-1
-1

Score : 550 points

Problem Statement

In the nation of AtCoder, there are N cities numbered 1 to N, and M roads numbered 1 to M.
Road i connects cities A_i and B_i bidirectionally and has a length of C_i.

You are given Q queries to process in order. The queries are of the following two types.

  • 1 i: Road i becomes closed.
  • 2 x y: Print the shortest distance from city x to city y, using only roads that are not closed. If city y cannot be reached from city x, print -1 instead.

It is guaranteed that each test case contains at most 300 queries of the first type.

Constraints

  • 2 \leq N \leq 300
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i < B_i \leq N
  • All pairs (A_i, B_i) are distinct.
  • 1 \leq C_i \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • In the queries of the first type, 1 \leq i \leq M.
  • The road given in a query of the first type is not already closed at that time.
  • The number of queries of the first type is at most 300.
  • In the queries of the second type, 1 \leq x < y \leq N.
  • All input values are integers.

Input

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

N M Q
A_1 B_1 C_1
\vdots
A_M B_M C_M
\mathrm{query}_1
\vdots
\mathrm{query}_Q

Each query is in one of the following two formats:

1 i
2 x y

Output

Process the queries in order.


Sample Input 1

3 3 5
1 2 5
1 3 10
2 3 6
2 1 3
1 2
2 1 3
1 1
2 1 3

Sample Output 1

10
11
-1
  • In the first query, print the shortest distance from city 1 to city 3, which is 10.
  • In the second query, road 2 becomes closed.
  • In the third query, print the shortest distance from city 1 to city 3, which is 11.
  • In the fourth query, road 1 becomes closed.
  • In the fifth query, city 3 cannot be reached from city 1, so print -1.

Sample Input 2

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

Sample Output 2

-1
-1
-1