A - Conflict

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

N 個の商品があります。高橋君と青木君がどの商品を欲しがっているかを表す長さ N の文字列 T,A が与えられます。T,A の i\ (1\leq i\leq N) 文字目をそれぞれ T_i,A_i とします。

高橋君は T_i が o のとき i 番目の商品を欲しがっており、T_i が x のとき i 番目の商品を欲しがっていません。 同様に、青木君は A_i が o のとき i 番目の商品を欲しがっており、A_i が x のとき i 番目の商品を欲しがっていません。

2 人ともが欲しがっている商品が存在するか判定してください。

制約

  • 1\leq N\leq 100
  • N は整数
  • T,A は o および x からなる長さ N の文字列

入力

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

N
T
A

出力

2 人とも欲しがっている商品が存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

4
oxoo
xoox

出力例 1

Yes

3 つ目の商品は 2 人ともが欲しがっているため、Yes を出力します。


入力例 2

5
xxxxx
ooooo

出力例 2

No

2 人とも欲しがっている商品は存在しないため、No を出力します。


入力例 3

10
xoooxoxxxo
ooxooooxoo

出力例 3

Yes

Score : 100 points

Problem Statement

There are N items. You are given strings T and A of length N that represent which items Takahashi and Aoki want, respectively. Let T_i and A_i be the i-th (1\leq i\leq N) characters of T and A, respectively.

Takahashi wants the i-th item when T_i is o, and does not want the i-th item when T_i is x. Similarly, Aoki wants the i-th item when A_i is o, and does not want the i-th item when A_i is x.

Determine whether there exists an item that both of them want.

Constraints

  • 1\leq N\leq 100
  • N is an integer.
  • T and A are strings of length N consisting of o and x.

Input

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

N
T
A

Output

If there exists an item that both of them want, output Yes; otherwise, output No.


Sample Input 1

4
oxoo
xoox

Sample Output 1

Yes

The third item is wanted by both of them, so output Yes.


Sample Input 2

5
xxxxx
ooooo

Sample Output 2

No

There is no item that both of them want, so output No.


Sample Input 3

10
xoooxoxxxo
ooxooooxoo

Sample Output 3

Yes
B - Median?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

整数 a, b, c が与えられます。b がこれらの整数の中央値であるかどうか判定してください。

制約

  • 1 \leq a, b, c \leq 100
  • 入力は全て整数

入力

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

a b c

出力

b が与えられた整数の中央値であるならば Yes、そうでないならば No と出力せよ。


入力例 1

5 3 2

出力例 1

Yes

与えられた整数を小さい順に並べると 2, 3, 5 となり、b はこれらの整数の中央値です。


入力例 2

2 5 3

出力例 2

No

b は与えられた整数の中央値ではありません。


入力例 3

100 100 100

出力例 3

Yes

Score : 100 points

Problem Statement

Given integers a, b, and c, determine if b is the median of these integers.

Constraints

  • 1 \leq a, b, c \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

If b is the median of the given integers, then print Yes; otherwise, print No.


Sample Input 1

5 3 2

Sample Output 1

Yes

The given integers are 2, 3, 5 when sorted in ascending order, of which b is the median.


Sample Input 2

2 5 3

Sample Output 2

No

b is not the median of the given integers.


Sample Input 3

100 100 100

Sample Output 3

Yes
C - The Honest Woodcutters

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

N 人の木こり 1,2,\dots,N が斧を 1 個ずつ持っています。 全員が斧を池に落としてしまいました。
池に N 個の斧 1,2,\dots,N が沈んでいました。
各木こり i は「自分が持っていた斧は斧 A_i である」と主張しています。
一方、この池の女神は、各斧 i を持っていたのは木こり B_i であることを知っています。

N 人の木こり全員が本当のことを言っているかどうかを判定してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq N
  • 1 \leq B_i \leq N
  • A_i \neq A_j\;(i \neq j)
  • B_i \neq B_j\;(i \neq j)
  • 入力される値はすべて整数

入力

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

N  
A_1 A_2 \dots A_N  
B_1 B_2 \dots B_N  

出力

N 人の木こり全員が本当のことを言っているならば Yes を、そうでないならば No を出力せよ。


入力例 1

3
3 1 2
2 3 1

出力例 1

Yes

N 人の木こり全員が本当のことを言っています。


入力例 2

4
1 2 3 4
1 3 2 4

出力例 2

No

木こり 2,3 の 2 人は嘘をついています。


入力例 3

5
2 4 5 1 3
4 1 5 2 3

出力例 3

Yes

Score : 200 points

Problem Statement

N woodcutters 1, 2, \dots, N each have one axe. All of them dropped their axes into a pond.
N axes 1, 2, \dots, N were found sunk in the pond.
Each woodcutter i claims that "I owned axe A_i."
On the other hand, the goddess of this pond knows that the woodcutter who owned axe i is woodcutter B_i.

Determine whether all N woodcutters are telling the truth.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq N
  • 1 \leq B_i \leq N
  • A_i \neq A_j\;(i \neq j)
  • B_i \neq B_j\;(i \neq j)
  • All input values are integers.

Input

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

N  
A_1 A_2 \dots A_N  
B_1 B_2 \dots B_N  

Output

Output Yes if all N woodcutters are telling the truth, and No otherwise.


Sample Input 1

3
3 1 2
2 3 1

Sample Output 1

Yes

All N woodcutters are telling the truth.


Sample Input 2

4
1 2 3 4
1 3 2 4

Sample Output 2

No

Woodcutters 2 and 3 are lying.


Sample Input 3

5
2 4 5 1 3
4 1 5 2 3

Sample Output 3

Yes
D - Precondition

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

英小文字および英大文字のみからなる文字列 S, T が与えられます。

文字列 S が以下の条件を満たしているか判定してください。

  • S の先頭でない英大文字の直前の文字はすべて T に含まれる。より形式的には、2 \leq i \leq |S| なる整数 i について S の i 番目の文字が英大文字ならば、S の i-1 番目の文字は T に含まれる。

制約

  • S, T は長さ 1 以上 100 以下の英小文字および英大文字のみからなる文字列

入力

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

S
T

出力

S が問題文中の条件を満たしているとき Yes と出力せよ。そうでないとき、No と出力せよ。


入力例 1

AtCoder
Total

出力例 1

Yes

S の先頭でない英大文字は 3 番目の文字の C のみです。この直前の文字である t は T に含まれているため、Yes と出力すればよいです。


入力例 2

aBCdE
abcdcba

出力例 2

No

S の 3 番目の文字は英大文字 C であり、その直前の文字は B ですが、B は T に含まれていません。


入力例 3

abcde
XYZ

出力例 3

Yes

Score : 200 points

Problem Statement

You are given strings S and T consisting of lowercase and uppercase English letters.

Determine whether the string S satisfies the following condition:

  • Every uppercase letter in S that is not at the beginning is immediately preceded by a character contained in T. More formally, for all integers i such that 2 \leq i \leq |S|, if the i-th character of S is uppercase, then the (i-1)-th character of S is contained in T.

Constraints

  • Each of S and T is a string consisting of lowercase and uppercase English letters with length between 1 and 100, inclusive.

Input

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

S
T

Output

If S satisfies the condition in the problem statement, output Yes. Otherwise, output No.


Sample Input 1

AtCoder
Total

Sample Output 1

Yes

The only uppercase letter in S that is not at the beginning is the 3rd character C. The immediately preceding character t is contained in T, so output Yes.


Sample Input 2

aBCdE
abcdcba

Sample Output 2

No

The 3rd character of S is the uppercase letter C, and its immediately preceding character is B, but B is not contained in T.


Sample Input 3

abcde
XYZ

Sample Output 3

Yes
E - Sake or Water

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

N 個のカップがあり、それぞれのカップには無色透明な液体が入っています。
具体的には、i 番目 (1\leq i\leq N) のカップには A_i ml の液体が入っています。
また、これらのうちちょうど K 個のカップには日本酒が入っており、それ以外には水が入っていることが分かっています。
ただし、どのカップに日本酒が入っているかについては分かっていません。

高橋君は(1 つ以上の)いくつかのカップを選んでそれらに入った液体をすべて飲むことができます。
どのカップに日本酒が入っているかによらず、高橋君が確実に X ml 以上の日本酒を飲むためには、最低何個のカップを選ぶ必要があるか求めてください。
そのような選び方が不可能である場合には -1 を出力してください。

制約

  • 1 \leq K \leq N \leq 3\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq X \leq 3\times 10^{14}
  • 入力はすべて整数

入力

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

N K X
A_1 A_2 \ldots A_N

出力

条件をみたすために高橋君が選ぶ必要があるカップの個数の最小値を出力せよ。 そのような選び方が不可能である場合には -1 を出力せよ。


入力例 1

3 2 5
10 6 8

出力例 1

2

高橋君が 1 番目と 3 番目のカップを選んで飲んだ場合を考えます。

3 個のカップのうち 2 個に日本酒が入っているため、次の 3 通りが考えられます。

  • 1,2 番目のカップに日本酒が入っていた場合

高橋君は 10 ml の日本酒と 8 ml の水を飲むことになります。

  • 1,3 番目のカップに日本酒が入っていた場合

高橋君は 18 ml の日本酒を飲むことになります。

  • 2,3 番目のカップに日本酒が入っていた場合

高橋君は 8 ml の日本酒と 10 ml の水を飲むことになります。

よって、いずれの場合でも 5 ml 以上の日本酒を飲むことができます。
一方で、どのカップに日本酒が入っているか分かっていない状態で、1 つのみのカップを選んで条件をみたすようにすることは不可能です。

よって、2 を出力します。


入力例 2

2 1 8
6 10

出力例 2

-1

1 番目のカップに日本酒が入っていた場合、どのようにカップを選んでも 8 ml 以上の日本酒を飲むことは不可能です。
よって、-1 を出力します。


入力例 3

5 3 3000000000
1000000000 1000000000 1000000000 1000000000 1000000000

出力例 3

5

Score : 300 points

Problem Statement

There are N cups, each containing a colorless and transparent liquid.
Specifically, the i-th (1\leq i\leq N) cup contains A_i ml of liquid.
It is known that exactly K of these cups contain sake (rice wine), and the rest contain water.
However, it is not known which cups contain sake.

Takahashi can choose some (one or more) cups and drink all the liquid in them.
Find the minimum number of cups he needs to choose to ensure he drinks at least X ml of sake, regardless of which cups contain sake.
If such a choice is impossible, print -1.

Constraints

  • 1 \leq K \leq N \leq 3\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq X \leq 3\times 10^{14}
  • All input values are integers.

Input

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

N K X
A_1 A_2 \ldots A_N

Output

Print the minimum number of cups Takahashi needs to choose to satisfy the condition. If such a choice is impossible, print -1.


Sample Input 1

3 2 5
10 6 8

Sample Output 1

2

Consider the case where Takahashi chooses the first and third cups and drinks them.

Two out of the three cups contain sake, so the following three cases are possible:

  • If the first and second cups contain sake

He drinks 10 ml of sake and 8 ml of water.

  • If the first and third cups contain sake

He drinks 18 ml of sake.

  • If the second and third cups contain sake

He drinks 8 ml of sake and 10 ml of water.

Thus, in all cases, he can drink at least 5 ml of sake.
On the other hand, it is impossible to satisfy the condition by choosing only one cup without knowing which cups contain sake.

Therefore, print 2.


Sample Input 2

2 1 8
6 10

Sample Output 2

-1

If the first cup contained sake, it is impossible to drink 8 ml or more of sake no matter which cups are chosen.
Therefore, print -1.


Sample Input 3

5 3 3000000000
1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5
F - Final Day

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

N 人の生徒が 4 日間にわたる試験を受けています。

それぞれの日に行われる試験は 300 点満点です。すなわち、4 日間を通した試験の満点は 1200 点です。

現在 3 日目までの試験が終わり、これから 4 日目の試験が行われようとしています。i \, (1 \leq i \leq N) 番目の生徒は j \, (1 \leq j \leq 3) 日目の試験で P_{i, j} 点獲得しました。

それぞれの生徒について、4 日目の試験後に上位 K 位以内に入っていることがあり得るかどうか判定してください。
ただし、4 日目の試験後の生徒の順位は、その生徒よりも 4 日間の合計点が高い生徒の人数に 1 を加えた値として定めます。

制約

  • 1 \leq K \leq N \leq 10^5
  • 0 \leq P_{i, j} \leq 300 \, (1 \leq i \leq N, 1 \leq j \leq 3)
  • 入力は全て整数である。

入力

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

N K
P_{1,1} P_{1,2} P_{1,3}
\vdots
P_{N,1} P_{N,2} P_{N,3}

出力

N 行出力せよ。i \, (1 \leq i \leq N) 行目には、i 番目の生徒が 4 日目の試験後に上位 K 位以内に入っていることがあり得るならば Yes と、そうでないならば No と出力せよ。


入力例 1

3 1
178 205 132
112 220 96
36 64 20

出力例 1

Yes
Yes
No

4 日目に全員が 100 点を取ると、1 番目の生徒が 1 位になります。 4 日目に 2 番目の生徒が 100 点を取り、それ以外の生徒が 0 点を取ると、2 番目の生徒が 1 位になります。 3 番目の生徒が 1 位になることはあり得ません。


入力例 2

2 1
300 300 300
200 200 200

出力例 2

Yes
Yes

入力例 3

4 2
127 235 78
192 134 298
28 56 42
96 120 250

出力例 3

Yes
Yes
No
Yes

Score : 300 points

Problem Statement

N students are taking a 4-day exam.

There is a 300-point test on each day, for a total of 1200 points.

The first three days of the exam are already over, and the fourth day is now about to begin. The i-th student (1 \leq i \leq N) got P_{i, j} points on the j-th day (1 \leq j \leq 3).

For each student, determine whether it is possible that he/she is ranked in the top K after the fourth day.
Here, the rank of a student after the fourth day is defined as the number of students whose total scores over the four days are higher than that of the student, plus 1.

Constraints

  • 1 \leq K \leq N \leq 10^5
  • 0 \leq P_{i, j} \leq 300 \, (1 \leq i \leq N, 1 \leq j \leq 3)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_{1,1} P_{1,2} P_{1,3}
\vdots
P_{N,1} P_{N,2} P_{N,3}

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain Yes if it is possible that the i-th student is ranked in the top K after the fourth day, and No otherwise.


Sample Input 1

3 1
178 205 132
112 220 96
36 64 20

Sample Output 1

Yes
Yes
No

If every student scores 100 on the fourth day, the 1-st student will rank 1-st.
If the 2-nd student scores 100 and the other students score 0 on the fourth day, the 2-nd student will rank 1-st.
The 3-rd student will never rank 1-st.


Sample Input 2

2 1
300 300 300
200 200 200

Sample Output 2

Yes
Yes

Sample Input 3

4 2
127 235 78
192 134 298
28 56 42
96 120 250

Sample Output 3

Yes
Yes
No
Yes
G - Buildings

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

ビル 1, ビル 2, \ldots, ビル N の N 棟のビルがこの順で一列に並んでいます。ビル i\ (1\leq i\leq N) の高さは H_i です。

各 i=1,2,\ldots,N について、次を満たす整数 j\ (i\lt j\leq N) の個数を求めてください。

  • ビル i とビル j の間にビル j より高いビルが存在しない。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq H_i\leq N
  • H_i\neq H_j\ (i\neq j)
  • 入力は全て整数

入力

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

N
H_1 H_2 \ldots H_N

出力

各 i=1,2,\ldots,N に対して条件を満たす j の個数を c_i としたとき、c_1,c_2,\ldots,c_N をこの順で空白区切りで出力せよ。


入力例 1

5
2 1 4 3 5

出力例 1

3 2 2 1 0

i=1 について、条件を満たす j は 2,3,5 の 3 つです。(ビル 1 とビル 4 の間にはビル 4 より高いビル 3 が存在するため、j=4 は条件を満たしません。)よって、出力の 1 つ目は 3 になります。


入力例 2

4
1 2 3 4

出力例 2

3 2 1 0

入力例 3

10
1 9 6 5 2 7 10 4 8 3

出力例 3

2 3 3 3 2 1 2 1 1 0

Score : 400 points

Problem Statement

There are N buildings, Building 1, Building 2, \ldots, Building N, arranged in a line in this order. The height of Building i (1 \leq i \leq N) is H_i.

For each i = 1, 2, \ldots, N, find the number of integers j (i < j \leq N) satisfying the following condition:

  • There is no building taller than Building j between Buildings i and j.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq H_i \leq N
  • H_i\neq H_j\ (i\neq j)
  • All input values are integers.

Input

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

N
H_1 H_2 \ldots H_N

Output

For each i = 1, 2, \ldots, N, let c_i be the number of j satisfying the condition. Print c_1, c_2, \ldots, c_N in order, separated by spaces.


Sample Input 1

5
2 1 4 3 5

Sample Output 1

3 2 2 1 0

For i=1, the integers j satisfying the condition are 2, 3, and 5: there are three. (Between Buildings 1 and 4, there is a building taller than Building 4, which is Building 3, so j=4 does not satisfy the condition.) Therefore, the first number in the output is 3.


Sample Input 2

4
1 2 3 4

Sample Output 2

3 2 1 0

Sample Input 3

10
1 9 6 5 2 7 10 4 8 3

Sample Output 3

2 3 3 3 2 1 2 1 1 0
H - LCM on Whiteboard

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

N 個の整数 a_1,\ldots,a_N が白板に書かれています。
ここで、a_i は m_i 個の素数 p_{i,1} \lt \ldots \lt p_{i,m_i} と正整数 e_{i,1},\ldots,e_{i,m_i} を用いて a_i = p_{i,1}^{e_{i,1}} \times \ldots \times p_{i,m_i}^{e_{i,m_i}} と表せる整数です。
あなたは N 個の整数から 1 つ選んで 1 に書き換えます。
書き換えた後の N 個の整数の最小公倍数としてあり得る値の個数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq m_i
  • \sum{m_i} \leq 2 \times 10^5
  • 2 \leq p_{i,1} \lt \ldots \lt p_{i,m_i} \leq 10^9
  • p_{i,j} は素数
  • 1 \leq e_{i,j} \leq 10^9
  • 入力はすべて整数

入力

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

N
m_1
p_{1,1} e_{1,1}
\vdots
p_{1,m_1} e_{1,m_1}
m_2
p_{2,1} e_{2,1}
\vdots
p_{2,m_2} e_{2,m_2}
\vdots
m_N
p_{N,1} e_{N,1}
\vdots
p_{N,m_N} e_{N,m_N}

出力

答えを出力せよ。


入力例 1

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

出力例 1

3

白板に書かれている整数は a_1 =7^2=49, a_2=2^2 \times 5^1 = 20, a_3 = 5^1 = 5, a_4=2^1 \times 7^1 = 14 です。
a_1 を 1 に書き換えると白板に書かれている整数は 1,20,5,14 となり、これらの最小公倍数は 140 です。
a_2 を 1 に書き換えると白板に書かれている整数は 49,1,5,14 となり、これらの最小公倍数は 490 です。
a_3 を 1 に書き換えると白板に書かれている整数は 49,20,1,14 となり、これらの最小公倍数は 980 です。
a_4 を 1 に書き換えると白板に書かれている整数は 49,20,5,1 となり、これらの最小公倍数は 980 です。
以上より、書き換えた後の N 個の整数の最小公倍数としてあり得る値は 140,490,980 であり、この入力における答えが 3 と分かります。


入力例 2

1
1
998244353 1000000000

出力例 2

1

白板に書かれている整数はとても大きい場合があります。

Score : 500 points

Problem Statement

There are N integers a_1,\ldots,a_N written on a whiteboard.
Here, a_i can be represented as a_i = p_{i,1}^{e_{i,1}} \times \ldots \times p_{i,m_i}^{e_{i,m_i}} using m_i prime numbers p_{i,1} \lt \ldots \lt p_{i,m_i} and positive integers e_{i,1},\ldots,e_{i,m_i}.
You will choose one of the N integers to replace it with 1.
Find the number of values that can be the least common multiple of the N integers after the replacement.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq m_i
  • \sum{m_i} \leq 2 \times 10^5
  • 2 \leq p_{i,1} \lt \ldots \lt p_{i,m_i} \leq 10^9
  • p_{i,j} is prime.
  • 1 \leq e_{i,j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
m_1
p_{1,1} e_{1,1}
\vdots
p_{1,m_1} e_{1,m_1}
m_2
p_{2,1} e_{2,1}
\vdots
p_{2,m_2} e_{2,m_2}
\vdots
m_N
p_{N,1} e_{N,1}
\vdots
p_{N,m_N} e_{N,m_N}

Output

Print the answer.


Sample Input 1

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

Sample Output 1

3

The integers on the whiteboard are a_1 =7^2=49, a_2=2^2 \times 5^1 = 20, a_3 = 5^1 = 5, a_4=2^1 \times 7^1 = 14.
If you replace a_1 with 1, the integers on the whiteboard become 1,20,5,14, whose least common multiple is 140.
If you replace a_2 with 1, the integers on the whiteboard become 49,1,5,14, whose least common multiple is 490.
If you replace a_3 with 1, the integers on the whiteboard become 49,20,1,14, whose least common multiple is 980.
If you replace a_4 with 1, the integers on the whiteboard become 49,20,5,1, whose least common multiple is 980.
Therefore, the least common multiple of the N integers after the replacement can be 140, 490, or 980, so the answer is 3.


Sample Input 2

1
1
998244353 1000000000

Sample Output 2

1

There may be enormous integers on the whiteboard.

I - Shortest Path Query

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 525 点

問題文

3 行 N 列のグリッドが与えられます。上から i 行目、左から j 列目のマスをマス (i,j) と表します。マス (i,j) には S_{i,j} が # ならば壁マスで、 . ならば空きマスであり通行可能です。

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

各クエリでは整数 r,c が与えられるので、マス (r,c) の状態を反転させてください。つまり、マス (r,c) が壁マスならば空きマスにし、空きマスならば壁マスにしてください。その後、以下の問題の答えを出力してください。

マス (1,1) から上下左右に隣接する空きマスに移動する操作を繰り返してマス (3,N) に移動することを考えます。このとき、マス (3,N) に到達できるか判定し、到達できる場合は操作回数の最小値を求めてください。

制約

  • 2\le N\le 2\times 10^5
  • S_{i,j} は # または .
  • S_{1,1}=S_{3,N}= .
  • 1\le Q\le 2\times 10^5
  • 1\le r\le 3
  • 1\le c\le N
  • (r,c) \neq (1,1),(3,N)
  • N,Q,r,c は整数

入力

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

N
S_{1,1}S_{1,2}\ldots S_{1,N}
S_{2,1}S_{2,2}\ldots S_{2,N}
S_{3,1}S_{3,2}\ldots S_{3,N}
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

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

r c

出力

Q 行出力せよ。

i 行目 (1\le i\le Q) には、 i 番目のクエリにおいてマス (1,1) からマス (3,N) に到達不可能ならば -1 を、到達可能ならば操作回数の最小値を出力せよ。


入力例 1

5
.#...
.#.#.
...#.
3
1 2
1 2
2 3

出力例 1

6
10
-1

1 つ目のクエリではマス (1,2) の状態を反転させます。その結果、各マスの状態は以下のようになります。

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

このとき、マス (1,1) から順にマス (1,2),(1,3),(1,4),(1,5),(2,5),(3,5) と移動することで 6 回の操作でマス (3,5) に到達することができます。

2 つ目のクエリではマス (1,2) の状態を反転させます。その結果、各マスの状態は以下のようになります。

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

このとき、マス (1,1) から順にマス (2,1),(3,1),(3,2),(3,3),(2,3),(1,3),(1,4),(1,5),(2,5),(3,5) と移動することで 10 回の操作でマス (3,5) に到達することができます。

3 つ目のクエリではマス (2,3) の状態を反転させます。その結果、各マスの状態は以下のようになります。

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

このとき、どのように操作してもマス (1,1) からマス (3,5) に到達することはできません。


入力例 2

7
.#.....
.#..#..
...#...
6
2 5
3 4
3 5
2 5
1 4
1 4

出力例 2

10
8
10
12
-1
12

Score : 525 points

Problem Statement

You are given a grid with three rows and N columns. Denote the cell at the i-th row from the top and j-th column from the left as cell (i,j). Cell (i,j) is a wall cell if S_{i,j} is #, and an empty cell and passable if it is ..

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

Each query gives integers r and c, and you should flip the state of cell (r,c). That is, if cell (r,c) is a wall cell, make it an empty cell, and if it is an empty cell, make it a wall cell. Then, output the answer to the following problem:

Consider moving from cell (1,1) to cell (3,N) by repeatedly moving to an empty cell adjacent up, down, left, or right. Determine whether cell (3,N) is reachable, and if reachable, find the minimum number of moves.

Constraints

  • 2\le N\le 2\times 10^5
  • S_{i,j} is # or ..
  • S_{1,1}=S_{3,N}= .
  • 1\le Q\le 2\times 10^5
  • 1\le r\le 3
  • 1\le c\le N
  • (r,c) \neq (1,1),(3,N)
  • N,Q,r,c are integers.

Input

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

N
S_{1,1}S_{1,2}\ldots S_{1,N}
S_{2,1}S_{2,2}\ldots S_{2,N}
S_{3,1}S_{3,2}\ldots S_{3,N}
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in the following format:

r c

Output

Print Q lines.

On the i-th line (1\le i\le Q), if cell (3,N) is unreachable from cell (1,1) in the i-th query, print -1; if reachable, print the minimum number of moves.


Sample Input 1

5
.#...
.#.#.
...#.
3
1 2
1 2
2 3

Sample Output 1

6
10
-1

In the first query, flip the state of cell (1,2). As a result, the state of each cell becomes:

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

At this time, by moving from cell (1,1) through cells (1,2),(1,3),(1,4),(1,5),(2,5),(3,5) in order, you can reach cell (3,5) in six moves.

In the second query, flip the state of cell (1,2). As a result, the state of each cell becomes:

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

At this time, by moving from cell (1,1) through cells (2,1),(3,1),(3,2),(3,3),(2,3),(1,3),(1,4),(1,5),(2,5),(3,5) in order, you can reach cell (3,5) in ten moves.

In the third query, flip the state of cell (2,3). As a result, the state of each cell becomes:

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

At this time, no matter how you move, you cannot reach cell (3,5) from cell (1,1).


Sample Input 2

7
.#.....
.#..#..
...#...
6
2 5
3 4
3 5
2 5
1 4
1 4

Sample Output 2

10
8
10
12
-1
12