A - 粗大ゴミ

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

問題文

あなたは縦 X センチメートル、横 Y センチメートル、高さ Z センチメートルの直方体の箱をゴミとして捨てようとしています。

縦横高さのいずれかが S センチメートル以上であるものは粗大ゴミになります。あなたが捨てようとしている箱は粗大ゴミですか?

制約

  • 1\leq X,Y,Z,S \leq 1000
  • 入力は全て整数

入力

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

X Y Z S

出力

あなたが捨てようとしている箱が粗大ゴミならば Yes、粗大ゴミでなければ No を出力せよ。


入力例 1

10 20 30 100

出力例 1

No

縦横高さがいずれも 100 センチメートル未満であるため粗大ゴミではありません。


入力例 2

50 100 150 130

出力例 2

Yes

高さが 130 センチメートル以上であるため粗大ゴミです。


入力例 3

50 50 50 50

出力例 3

Yes

Problem Statement

You are going to throw away a rectangular cuboid of depth X centimeters, width Y centimeters, and height Z centimeters.

An item is regarded as bulky trash if its depth, width, or height is at least S centimeters. Is the item you are discarding bulky trash?

Constraints

  • 1\leq X,Y,Z,S \leq 1000
  • All input values are integers.

Input

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

X Y Z S

Output

Print Yes if the item you are discarding is bulky trash, and No otherwise.


Sample Input 1

10 20 30 100

Sample Output 1

No

Its depth, width, and height are all strictly less than 100 centimeters, so it is not bulky trash.


Sample Input 2

50 100 150 130

Sample Output 2

Yes

Its height is not less than 130 centimeters, so it is bulky trash.


Sample Input 3

50 50 50 50

Sample Output 3

Yes
B - レーティング

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

問題文

競技プログラミングサイト AtCoder では、各ユーザーの実力を「レーティング」と呼ばれる数値で表します。 ユーザーがプログラミングコンテストに参加すると、そのコンテストにおけるユーザーの成績が「パフォーマンス」と呼ばれる数値で表され、その値に応じてユーザーのレーティングが変化します。

ユーザーのコンテスト前のレーティングが R であり、コンテストでのパフォーマンスが P のとき、 そのユーザーのコンテスト後のレーティング R' は、近似的に次の式で表されます。

R' = \lfloor 0.9 \times R + 0.1 \times P \rfloor

ただし、\lfloor x \rfloorx を超えない最大の整数を表します。

入力として RP が与えられるので、上記の近似式に基づき R' を計算して出力してください。

制約

  • 1 \leq R, P \leq 4400
  • R, P は整数

入力

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

R P

出力

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


入力例 1

2516 2320

出力例 1

2496

R' = \lfloor 0.9 \times 2516 + 0.1 \times 2320 \rfloor = \lfloor 2496.4 \rfloor = 2496 です。


入力例 2

2498 3200

出力例 2

2568

入力例 3

1 1

出力例 3

1

Problem Statement

On AtCoder, the competitive programming website, each user is graded by a value called rating. Every time a user participates in a programming contest, their performance is quantified and affects their rating.

If a user's rating before a contest is R and their performance in the contest is P, their rating after the contest R' is approximated by:

R' = \lfloor 0.9 \times R + 0.1 \times P \rfloor,

where \lfloor x \rfloor represents the maximum integer not exceeding x.

Given R and P as input, evaluate and print R' according to the approximation formula above.

Constraints

  • 1 \leq R, P \leq 4400
  • R and P are integers.

Input

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

R P

Output

Print the answer as an integer.


Sample Input 1

2516 2320

Sample Output 1

2496

We have R' = \lfloor 0.9 \times 2516 + 0.1 \times 2320 \rfloor = \lfloor 2496.4 \rfloor = 2496.


Sample Input 2

2498 3200

Sample Output 2

2568

Sample Input 3

1 1

Sample Output 3

1
C - 信号

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

問題文

2 つの信号、信号 1 と信号 2 があり、信号 i (i=1,2)B_i 秒の青信号と R_i 秒の赤信号を繰り返します。
すなわち信号 i がある瞬間に赤信号から青信号に変わったならば、その後の B_i 秒間は青信号であり、その次の R_i 秒は赤信号であり、以下同様に交互に青信号と赤信号を繰り返します。

今、2 つの信号が同時に赤信号から青信号に変わりました。
今から T 秒後までの間に信号 1 と信号 2 の両方が青信号になっている時間は何秒ありますか?

制約

  • 1\leq B_i,R_i\leq 100
  • 1\leq T\leq 10^4
  • 入力はすべて整数

入力

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

B_1 R_1 B_2 R_2 T

出力

2 つの信号が同時に赤信号から青信号に変わった瞬間から T 秒後までの間に信号 1 と信号 2 の両方が青信号になっている時間は何秒あるかを、整数として出力せよ。


入力例 1

2 3 5 1 10

出力例 1

3

2 つの信号が同時に赤信号から青信号に変わった瞬間から 10 秒間の間にそれぞれの信号は次のように変化します。

  • 変わった瞬間から 2 秒後までの間、信号 1 も 信号 2 も青信号である。
  • 2 秒後から 5 秒後までの間、信号 1 は赤信号であり、信号 2 は青信号である。
  • 5 秒後から 6 秒後までの間、信号 1 は青信号であり、信号 2 は赤信号である。
  • 6 秒後から 7 秒後までの間、信号 1 も 信号 2 も青信号である。
  • 7 秒後から 10 秒後までの間、信号 1 は赤信号であり、信号 2 は青信号である。

よって、ともに青信号である時間は、変わった瞬間から 2 秒後までの間と6 秒後から 7 秒後までの間をあわせて 3 秒間となります。


入力例 2

100 100 100 100 10000

出力例 2

5000

ともに青信号である時間と赤信号である時間が交互に 100 秒間ずつ繰り返されます。

Problem Statement

There are two traffic lights, light 1 and light 2. Light i (i=1,2) repeatedly shows green light for B_i seconds and red light for R_i seconds.
In other words, if light i turns from red to green at some point, it remains green for the next B_i seconds, and then remains red for the succeeding R_i seconds; this way, it alternates between green and red light indefinitely.

Now, the two lights have simultaneously turned from red to green.
Within the next T seconds, how many seconds do both light 1 and light 2 show green?

Constraints

  • 1\leq B_i,R_i\leq 100
  • 1\leq T\leq 10^4
  • All input values are integers.

Input

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

B_1 R_1 B_2 R_2 T

Output

Print the duration, in seconds as an integer, for which both light 1 and light 2 show green within the T seconds right after the two lights simultaneously turn from red to green.


Sample Input 1

2 3 5 1 10

Sample Output 1

3

Within the 10 seconds right after the two traffic lights simultaneously turn from red to green, the lights transition as follows:

  • From 0 to 2 seconds after the switch, both light 1 and light 2 are green.
  • From 2 to 5 seconds after the switch, light 1 is red and light 2 is green.
  • From 5 to 6 seconds after the switch, light 1 is green and light 2 is red.
  • From 6 to 7 seconds after the switch, both light 1 and light 2 are green.
  • From 7 to 10 seconds after the switch, light 1 is red and light 2 is green.

Therefore, both lights show green from 0 to 2 and from 6 to 7 seconds after the switch, for a total of 3 seconds.


Sample Input 2

100 100 100 100 10000

Sample Output 2

5000

Both lights show green for 100 seconds, followed by red for 100 seconds; this sequence repeats indefinitely.

D - 大乱闘

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

問題文

1 から N の番号がついた N 人のプレイヤーがとある対戦型格闘ゲームで遊びます。 対戦開始時と同時に、N 人のプレイヤーはゲーム上でリングに登場します。対戦開始時の全プレイヤーのスコア0 です。

その後、対戦中に M 個のイベントが起きます。各イベントは下記のタイプ 1 またはタイプ 2 のどちらかです。

  • タイプ 1 : 下記の形式で示される。プレイヤー x が、プレイヤー y攻撃する。
1 x y
  • タイプ 2 : 下記の形式で示される。プレイヤー z がリングから脱落する。
2 z

プレイヤーがリングから脱落すると、直ちに下記の出来事が起きます。

  • 脱落したプレイヤーのスコアが 1 だけ減少する。
  • 脱落したプレイヤーが直前にリングに登場した後に 1 回以上攻撃されていた場合、そのうち最後の攻撃を行ったプレイヤーのスコアが 1 だけ増加する。
  • その後、脱落したプレイヤーはリングに再び登場する。

各プレイヤーの最終的なスコアを出力してください。

制約

  • 入力はすべて整数
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq x, y, z \leq N
  • x \neq y

入力

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

N M
\mathrm{event}_1
\mathrm{event}_2
\vdots
\mathrm{event}_M

i = 1, 2, \ldots, M について、\mathrm{event}_ii 番目に起きたイベントを表し、 問題文中に示されたタイプ 1 とタイプ 2 のどちらかの形式である。

出力

i = 1, 2, \ldots, N について、プレイヤー i の最終的なスコア S_i を下記の形式で空白区切りで出力せよ。

S_1 S_2 \ldots S_N

入力例 1

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

出力例 1

0 -1 -2 1

対戦開始時、4 人のプレイヤーがリングに登場し、各プレイヤーのスコアは (S_1, S_2, S_3, S_4) = (0, 0, 0, 0) です。 その後、対戦中に下記の通りにイベントが起きます。

  • 1 番目のイベントでは、プレイヤー 2 がプレイヤー 3 に攻撃します。
  • 2 番目のイベントでは、プレイヤー 4 がプレイヤー 3 に攻撃します。
  • 3 番目のイベントでは、プレイヤー 3 が脱落します。その結果、プレイヤー 3 のスコアが 1 だけ減少します。また、プレイヤー 3 が直前にリングに登場したとき(すなわち、対戦開始時)より後に、プレイヤー 31 番目と 2 番目のイベントで攻撃されているため、そのうち最後の攻撃を行ったプレイヤー 4 のスコアが 1 だけ増加します。その後、プレイヤー 3 は再びリングに登場します。
  • 4 番目のイベントでは、プレイヤー 2 がプレイヤー 1 に攻撃します。
  • 5 番目のイベントでは、プレイヤー 2 が脱落します。その結果、プレイヤー 2 のスコアが 1 だけ減少します。プレイヤー 2 が直前にリングに登場したとき(すなわち、対戦開始時)より後に、プレイヤー 21 回も攻撃されていません。その後、プレイヤー 2 は再びリングに登場します。
  • 6 番目のイベントでは、プレイヤー 3 が脱落します。その結果、プレイヤー 3 のスコアが 1 だけ減少します。プレイヤー 3 が直前にリングに登場したとき(すなわち、3 番目のイベントの最後)より後に、プレイヤー 31 回も攻撃されていません。その後、プレイヤー 3 は再びリングに登場します。

各プレイヤーの最終的なスコアは (S_1, S_2, S_3, S_4) = (0, -1, -2, 1) となります。


入力例 2

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

出力例 2

-1 -1 -1 0 0

Problem Statement

N players numbered 1 through N are playing a multiplayer fighting game. When a match starts, the N players enter a ring at once. Initially, each player's score is 0.

Then, M events occur during the match. Each event is of type 1 or type 2 described below:

  • Type 1: represented in the following format. Player x attacks player y.
1 x y
  • Type 2: represented in the following format. Player z drops out of the ring.
2 z

When a player drops out of the ring, the following events immediately occur:

  • The score of the player who has dropped out decreases by 1.
  • If the player who has dropped out was attacked at least once since the last entrance, the score of the player who made the last such attack increases by 1.
  • Then, the player enters the ring again.

Print the final score of each player.

Constraints

  • All input values are integers.
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq x, y, z \leq N
  • x \neq y

Input

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

N M
\mathrm{event}_1
\mathrm{event}_2
\vdots
\mathrm{event}_M

For i = 1, 2, \ldots, M, \mathrm{event}_i represents the i-th event, and is in type-1 or type-2 format described in the problem statement.

Output

For i = 1, 2, \ldots, N, print the final score S_i of player i in the following format, separated by spaces:

S_1 S_2 \ldots S_N

Sample Input 1

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

Sample Output 1

0 -1 -2 1

When the match starts, 4 players enter the ring, and the players' scores are (S_1, S_2, S_3, S_4) = (0, 0, 0, 0). Then, the following events occur during the match:

  • In the 1-st event, player 2 attacks player 3.
  • In the 2-nd event, player 4 attacks player 3.
  • In the 3-rd event, player 3 drops out, so their score decreases by 1. Since player 3's last entrance (i.e. since the match begins), they are attacked in the 1-st and 2-nd event. The last one among them is made by player 4, so their score increases by 1. Subsequently, player 3 enters the ring again.
  • In the 4-th event, player 2 attacks player 1.
  • In the 5-th event, player 2 drops out, so their score decreases by 1. Since player 2's last entrance (i.e. since the match begins), they are never attacked. Subsequently, player 2 enters the ring again.
  • In the 6-th event, player 3 drops out, so their score decreases by 1. Since player 3's last entrance (i.e. since the 3-rd event), they are never attacked. Subsequently, player 3 enters the ring again.

The final scores of the players are (S_1, S_2, S_3, S_4) = (0, -1, -2, 1).


Sample Input 2

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

Sample Output 2

-1 -1 -1 0 0
E - 共通部分

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

問題文

N 個の整数集合 S_1, S_2, \dots, S_N があります。
S_i に含まれている整数は C_i 個あり、小さい方から順に A_{i, 1}, \dots, A_{i, C_i} です。
この中から 2 個以上の整数集合を選ぶ方法のうち、次の条件を満たす選び方は何通りありますか?

  • 選んだ集合の共通部分は奇数のみからなる。

ここで、選んだ集合の共通部分とは、全ての選んだ集合に含まれている整数全体からなる集合のことを言います。

制約

  • 2 \leq N \leq 10
  • 1 \leq C_i \leq 10
  • 1 \leq A_{i,1} \lt A_{i,2} \lt \dots \lt A_{i,C_i} \leq 100
  • 入力される値は全て整数

入力

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

N
C_1 A_{1,1} A_{1,2} \dots A_{1,C_1}
C_2 A_{2,1} A_{2,2} \dots A_{2,C_2}
\vdots
C_N A_{N,1} A_{N,2} \dots A_{N,C_N}

出力

答えを出力せよ。


入力例 1

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

出力例 1

3

集合の選び方とその共通部分、および条件を満たすかどうかを全て列挙すると次の通りです。

  • S_1, S_2 の共通部分は \lbrace 1, 5 \rbrace であり、この選び方は条件を満たします。
  • S_1, S_3 の共通部分は \lbrace 2 \rbrace であり、この選び方は条件を満たしません。
  • S_2, S_3 の共通部分は \lbrace 3 \rbrace であり、この選び方は条件を満たします。
  • S_1, S_2, S_3 の共通部分は \lbrace \rbrace (空集合) であり、この選び方は条件を満たします。

共通部分が空集合である場合も条件を満たすのに注意してください。


入力例 2

5
6 4 5 6 9 10 17
5 1 7 9 13 20
6 2 5 8 15 16 20
6 2 5 6 7 16 19
1 3

出力例 2

23

Problem Statement

There are N sets of integers: S_1, S_2, \dots, S_N.
S_i contains C_i integers, A_{i, 1}, \dots, A_{i, C_i}, in ascending order.
Among the ways to choose two or more of the sets, how many of them satisfy the following condition?

  • The intersection of the chosen sets contains only odd numbers.

Here, the intersection of chosen sets is the set consisting of the integers contained in all of the chosen sets.

Constraints

  • 2 \leq N \leq 10
  • 1 \leq C_i \leq 10
  • 1 \leq A_{i,1} \lt A_{i,2} \lt \dots \lt A_{i,C_i} \leq 100
  • All input values are integers.

Input

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

N
C_1 A_{1,1} A_{1,2} \dots A_{1,C_1}
C_2 A_{2,1} A_{2,2} \dots A_{2,C_2}
\vdots
C_N A_{N,1} A_{N,2} \dots A_{N,C_N}

Output

Print the answer.


Sample Input 1

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

Sample Output 1

3

We enumerate the possible choices of sets, their intersections, and whether they satisfy the condition:

  • The intersection of S_1 and S_2 is \lbrace 1, 5 \rbrace, which satisfies the condition.
  • The intersection of S_1 and S_3 is \lbrace 2 \rbrace, which does not satisfy the condition.
  • The intersection of S_2 and S_3 is \lbrace 3 \rbrace, which satisfies the condition.
  • The intersection of S_1, S_2, and S_3 is \lbrace \rbrace (an empty set), which satisfies the condition.

Note that the condition is satisfied even when the intersection is an empty set.


Sample Input 2

5
6 4 5 6 9 10 17
5 1 7 9 13 20
6 2 5 8 15 16 20
6 2 5 6 7 16 19
1 3

Sample Output 2

23
F - インタプリタをつくろう

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

問題文

一文字の変数と一桁の整数および +- からなる式 S が与えられます。
例えば、1+2+33-a+2+c+01 は与えられる場合があります。
対して、12+34-aba/2+5 は与えられません。それぞれ、12 が一桁の整数ではない、ab が一文字の変数ではない(a\times b と解釈することもありません)、/ が含まれているためです。

変数の値が N 個の組 (c _ 1,v _ 1),(c _ 2,v _ 2),\ldots,(c _ N,v _ N) として与えられます。 i 番目の組 (c _ i,v _ i) は、変数 c _ i の値が v _ i であることを表します。

S を計算した結果の値を求めてください。

制約

  • S は長さ 1 以上 100 未満の文字列
  • S の長さは奇数
  • S の奇数番目の文字は英小文字か数字
  • S の偶数番目の文字は +- のどちらか
  • 1\leq N\leq 26
  • c _ i は英小文字 (1\leq i\leq N)
  • i\neq j\implies c _ i\neq c _ j\ (1\leq i,j\leq N)
  • 英小文字 cS に出現するなら、ある i\ (1\leq i\leq N) が存在して c=c _ i
  • -100\leq v _ i\leq100\ (1\leq i\leq N)
  • N および v _ 1,v _ 2,\ldots,v _ N はすべて整数

入力

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

S
N
c _ 1 v _ 1
c _ 2 v _ 2
\vdots
c _ N v _ N

出力

答えを出力せよ。


入力例 1

2+a-7
1
a 18

出力例 1

13

a=18 のとき、2+a-7=2+18-7=13 です。 よって、13 と出力してください。


入力例 2

1
4
s 29
b -1
m -8
f -2

出力例 2

1

S に変数が含まれなかったり、S に含まれない変数の情報が与えられる場合もあります。


入力例 3

0-1+8-i+m-d-3-a-m+4+a+m-2+5+5-5
10
a 68
b -88
d -10
e -19
i -33
l -44
m -39
n 50
r -36
y 63

出力例 3

15

Problem Statement

You are given an expression S consisting of one-letter variables, one-digit integers, and + and - signs.
For example, 1+2+3, 3-a+2+c+0, and 1 can be given,
but 12+3, 4-ab, and a/2+5 cannot, because 12 is not a one-digit integer, ab is not a one-letter variable (nor is it interpreted as a\times b), and it contains /, respectively.

You are given the values of the variables as N pairs, (c _ 1,v _ 1),(c _ 2,v _ 2),\ldots, and (c _ N,v _ N). The i-th pair (c _ i,v _ i) means that the variable c _ i should evaluate to the value v _ i.

Evaluate the expression S.

Constraints

  • S is a string of length between 1 and 99, inclusive.
  • S has an odd length.
  • Each character at an odd-numbered position of S is a lowercase English letter or a digit.
  • Each character at an even-numbered position of S is + or -.
  • 1\leq N\leq 26
  • c_i is a lowercase English letter (1\leq i\leq N).
  • i\neq j\implies c _ i\neq c _ j\ (1\leq i,j\leq N)
  • If a lowercase English letter c occurs in S, then there exists i\ (1\leq i\leq N) such that c=c _ i.
  • -100\leq v _ i\leq100\ (1\leq i\leq N)
  • N, v _ 1,v _ 2,\ldots, and v _ N are all integers.

Input

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

S
N
c _ 1 v _ 1
c _ 2 v _ 2
\vdots
c _ N v _ N

Output

Print the answer.


Sample Input 1

2+a-7
1
a 18

Sample Output 1

13

When a=18, we have 2+a-7=2+18-7=13. Thus, 13 should be printed.


Sample Input 2

1
4
s 29
b -1
m -8
f -2

Sample Output 2

1

S may not contain a variable. Also, a value may be given for a variable that is not contained in S.


Sample Input 3

0-1+8-i+m-d-3-a-m+4+a+m-2+5+5-5
10
a 68
b -88
d -10
e -19
i -33
l -44
m -39
n 50
r -36
y 63

Sample Output 3

15
G - 二回の交換

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

問題文

長さ N の数列 A = (A_1, A_2, \ldots, A_N), B = (B_1, B_2, \ldots, B_N) が与えられます。

数列 A に以下の操作をちょうど 2 回行います。

  • 1 \leq i < N なる整数 i を選び、A_i の値と A_{i + 1} の値を入れ替える

ちょうど 2 回の操作後に A = B となることがあるか判定してください。

制約

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

入力

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

ちょうど 2 回の操作後に A = B となることがあるならば Yes を、そうでないならば No を出力せよ。


入力例 1

5
1 3 5 5 2
3 1 5 2 5

出力例 1

Yes

以下のようにすることでちょうど 2 回の操作後に A = B とすることができます。

  • i = 1 を選ぶ。A_1 の値と A_2 の値を入れ替える。A = (3, 1, 5, 5, 2) となる。
  • i = 4 を選ぶ。A_4 の値と A_5 の値を入れ替える。A = (3, 1, 5, 2, 5) となる。

入力例 2

3
2 2 2
3 3 3

出力例 2

No

Problem Statement

You are given length-N sequences A = (A_1, A_2, \ldots, A_N) and B = (B_1, B_2, \ldots, B_N).

You will apply the following operation against the sequence A exactly twice.

  • Choose an integer i with 1 \leq i < N, and swap the values of A_i and A_{i + 1}.

Determine if you can make A = B by operating exactly twice.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • 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
B_1 B_2 \ldots B_N

Output

Print Yes if you can make A = B by operating exactly twice, and No otherwise.


Sample Input 1

5
1 3 5 5 2
3 1 5 2 5

Sample Output 1

Yes

You can make A = B by operating exactly twice as follows:

  • Choose i = 1. Swap the values of A_1 and A_2 to make A = (3, 1, 5, 5, 2).
  • Choose i = 4. Swap the values of A_4 and A_5 to make A = (3, 1, 5, 2, 5).

Sample Input 2

3
2 2 2
3 3 3

Sample Output 2

No
H - Cを探せ!

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

問題文

グリッド(マス目)上に書かれた以下のような図形を C と呼びます。

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

上図にあるのは、左からレベル 1,2,3C です。

厳密には、正整数 l に対し、レベル lC を以下のように定義します。

  • レベル lC は、 (l+2) \times (l+2) の正方形状のグリッドである。
  • 正方形のうち、上・左・下の境界に接するマスには # が書かれている。
  • それ以外のマスには . が書かれている。

N \times N のグリッドが与えられ、各マスには #. が書かれています。
グリッドは N 個の文字列 S_1,S_2,\dots,S_N として与えられ、 S_ij 文字目が上から i 行目、左から j 列目のマスに書かれた文字を表します。

このグリッドから (連結な) 正方形を抜き出して得られる最もレベルの高い C のレベルを答えてください。
但し、グリッドからどのような正方形を抜き出してもそれが C とならない場合、 0 と答えてください。

制約

  • N3 以上 300 以下の整数
  • S_i は長さ N#. からなる文字列

入力

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

N
S_1
S_2
\vdots
S_N

出力

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


入力例 1

4
####
.#..
.###
#..#

出力例 1

1

グリッドのうち上から 1 行目から 3 行目、左から 2 列目から 4 列目の正方形を抜き出すと、これはレベル 1C です。
このグリッドから、レベル 2 以上の C を抜き出すことはできません。


入力例 2

5
.####
.###.
####.
##..#
###.#

出力例 2

0

このグリッドから C を抜き出すことはできません。


入力例 3

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

出力例 3

3

Problem Statement

An object on a grid like these is called a C.

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

Illustrated above are Cs of level 1, 2, and 3, from left to right.

Formally, for a positive integer l, a C* of level l is defined as follows.

  • A C of level l is an (l+2) \times (l+2) square grid.
  • Every cell on the upper, left, or lower border of the square has # written on it.
  • Every other cell has . written on it.

You are given an N \times N grid. Each cell has # or . written on it.
The grid is described by N strings S_1,S_2,\dots,S_N; the j-th character of S_i represents the character written on the cell in the i-th row from the top and j-th column from the left.

Find the maximum level of a C that can be obtained by extracting a (connected) square from this grid.
If no square extracted from the grid forms a C, print 0.

Constraints

  • N is an integer between 3 and 300, inclusive.
  • S_i is a length-N string consisting of # and ..

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer as an integer.


Sample Input 1

4
####
.#..
.###
#..#

Sample Output 1

1

If you extract the square consisting of 1-st through 3-rd rows from the top and 2-nd through 4-th columns, it forms a C of level 1.
One cannot extract a C of level 2 or greater from this grid.


Sample Input 2

5
.####
.###.
####.
##..#
###.#

Sample Output 2

0

One cannot extract a C from this grid.


Sample Input 3

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

Sample Output 3

3
I - 改行コード

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

問題文

右下方向に無限に広がるグリッド状のテキストエディタがあります。テキストエディタの上から i 行目、左から j 列目のマスを (i, j) と呼びます。各マスは文字が書かれている状態か何も書かれていない状態のいずれかです。初期状態ではどのマスにも文字は書かれていません。
また、文字の入力位置を示す カーソル と呼ばれるマークがあります。カーソルははじめ (1, 1) にあります。

あなたは Q 個のクエリを与えられた順に処理します。
クエリは次の 3 種類のいずれかです。

  • 1 c : カーソルのあるマスに文字 c を挿入する。厳密に説明すると、次の一連の操作を行う。
    • まず、現在カーソルのあるマスに書かれている文字および同じ行内でそのマスより右にある文字が存在すれば、それらを全て同時に右に 1 マス動かす。
    • 次に、現在カーソルのあるマスに文字 c を書きこむ。
    • 最後に、カーソルを右に 1 マス動かす。
  • 2 : カーソルを今いる行の左端に動かす。
  • 3 : カーソルを今いる 1 つ下の行の左端に動かす。

クエリを全て処理した後のテキストエディタの状態を出力してください。

制約

  • 1 \leq Q \leq 1.5 \times 10^6
  • c は英小文字

入力

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

Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

出力

  • クエリを全て処理した時点でのカーソルのあるマスを (H, W)
  • 上から i 行目に書かれている文字の個数を C_i
  • マス (i, j) に書かれている文字を S_{i, j}

として、以下の形式で H + 1 行出力せよ。(C_i = 0 である行が存在する可能性があることに注意せよ。)
2 行目以降は行頭に # および半角スペースを付ける必要がある点に注意せよ。

H W
# S_{1, 1}S_{1, 2} \dots S_{1, C_1}
# S_{2, 1}S_{2, 2} \dots S_{2, C_2}
\vdots
# S_{H, 1}S_{H, 2} \dots S_{H, C_H}

入力例 1

8
1 b
1 c
1 d
2
1 a
3
3
1 e

出力例 1

3 2
# abcd
# 
# e

クエリを処理する手順を説明すると次のようになります。

  • 1 番目のクエリでは、(1, 1)b を書きこみカーソルを (1, 2) に動かします。
  • 2 番目のクエリでは、(1, 2)c を書きこみカーソルを (1, 3) に動かします。
  • 3 番目のクエリでは、(1, 3)d を書きこみカーソルを (1, 4) に動かします。
  • 4 番目のクエリでは、(1, 1) にカーソルを動かします。
  • 5 番目のクエリでは、はじめに (1, 1) に書かれた b(1, 2) に書かれた c(1, 3) に書かれた d を全て同時に右に 1 マス動かします。そして、(1, 1)a を書きこみカーソルを (1, 2) に動かします。
  • 6 番目のクエリでは、(2, 1) にカーソルを動かします。
  • 7 番目のクエリでは、(3, 1) にカーソルを動かします。
  • 8 番目のクエリでは、(3, 1)e を書きこみカーソルを (3, 2) に動かします。

image

Problem Statement

There is a text editor that can be regarded as a grid extending infinitely toward right and downward. The cell in the i-th row from the top and j-th column from the left of the editor is denoted by (i, j). Each cell can have a character written on it, or be empty; initially, all cells are empty.
The editor also has a marker called a cursor, which indicates the position where a character is inserted. The cursor is initially located at (1, 1).

You are to process Q queries in given order.
Each query is one of the following three types.

  • 1 c: Insert the character c at the current position of the cursor. Formally, perform the following procedure.
    • First, move all characters in the current cell of the cursor or in any cell to the right within the same row, one cell to the right, if any.
    • Next, write the character c onto the cell where the cursor is currently located at.
    • Finally, move the cursor one cell to the right.
  • 2: Move the cursor to the leftmost cell of the current row.
  • 3: Move the cursor to the leftmost cell of the row right below the current row.

Print the state of the editor after processing all the queries.

Constraints

  • 1 \leq Q \leq 1.5 \times 10^6
  • c is a lowercase English character.

Input

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

Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Output

  • Let cell (H, W) be the position of the cursor when all the queries are processed.
  • Let C_i be the number of characters in the i-th row from the top.
  • Let S_{i, j} be the character written on cell (i, j).

Print (H+1) lines in the following format. (Note that C_i = 0 may apply for some rows.)
Beware that the second and succeeding lines must be preceded by # and a space.

H W
# S_{1, 1}S_{1, 2} \dots S_{1, C_1}
# S_{2, 1}S_{2, 2} \dots S_{2, C_2}
\vdots
# S_{H, 1}S_{H, 2} \dots S_{H, C_H}

Sample Input 1

8
1 b
1 c
1 d
2
1 a
3
3
1 e

Sample Output 1

3 2
# abcd
# 
# e

The query is processed as follows.

  • In the 1-st query, write b onto (1, 1), and move the cursor to (1, 2).
  • In the 2-nd query, write c onto (1, 2), and move the cursor to (1, 3).
  • In the 3-rd query, write d onto (1, 3), and move the cursor to (1, 4).
  • In the 4-th query, move the cursor to (1, 1).
  • In the 5-th query, simultaneously move the following characters one cell to the right: b written on (1, 1), c written on (1, 2), and d written on (1, 3). Then, write a onto (1, 1), and move the cursor to (1, 2).
  • In the 6-th query, move the cursor to (2, 1).
  • In the 7-th query, move the cursor to (3, 1).
  • In the 8-th query, write e onto (3, 1), and move the cursor to (3, 2).

image

J - 反転ゲーム

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

問題文

H マス横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスをマス (i,j) と表します。
最初、マス (i,j)S_{i,j}. のとき白マスであり、# のとき黒マスです。

あなたは最初マス (1,1) にいて、上下左右の 4 方向に隣接するいずれかのマスへ移動することができます。グリッドの外に移動することはできません。
あなたが移動すると、移動先のマスの白黒が反転します。

あなたの目的は全てのマスが白マスである状態にすることです。達成するまでに必要な最小移動回数を求めてください。

なお、制約の条件下で必ず全てのマスを白くできることが証明できます。

制約

  • 1 \leq H,W \leq 4
  • H\times W\geq 2
  • H,W は整数である
  • S_{i,j}. または #

入力

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

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

出力

答えを出力せよ。


入力例 1

4 4
.#..
....
.#..
....

出力例 1

4

(1,1)\to(1,2)\to(2,2)\to(3,2)\to(2,2) と順に移動することで、4 回の移動により全てのマスを白くすることができます。


入力例 2

3 2
..
..
..

出力例 2

0

最初から全てのマスが白いため、移動する必要はありません。


入力例 3

4 4
#..#
#.##
....
#.##

出力例 3

18

Problem Statement

There is a grid with H horizontal rows and W vertical columns. The cell in the i-th row from the top and j-th column from the left is denoted by cell (i, j).
Initially, cell (i, j) is white if S_{i,j} is ., and black if it is #.

You are initially at cell (1,1). You can move to an adjacent cell in one of the four directions: up, down, left, and right. You cannot exit the grid.
Whenever you make a move, the color of the cell you step into flips (from white to black and vice versa).

Your objective is to make all the cells white. Find the minimum number of moves required to do so.

One can prove that you can always make all the cells white under the given constraints.

Constraints

  • 1 \leq H,W \leq 4
  • H\times W\geq 2
  • 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}\ldots S_{1,W}
\vdots
S_{H,1}\ldots S_{H,W}

Output

Print the answer.


Sample Input 1

4 4
.#..
....
.#..
....

Sample Output 1

4

By making four moves (1,1)\to(1,2)\to(2,2)\to(3,2)\to(2,2), you can make all the cells white.


Sample Input 2

3 2
..
..
..

Sample Output 2

0

All the cells are initially white, so no moves are required.


Sample Input 3

4 4
#..#
#.##
....
#.##

Sample Output 3

18
K - 2で割り切れる回数

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

問題文

正整数 x に対して、 \rm{ord}_2(x) を 「 x2 で割り切れる回数」で定義します。
例えば \rm{ord}_2(24)=3, \rm{ord}_2(17)=0, \rm{ord}_2(32)=5 です。

長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
\displaystyle \sum^{N}_{ i=1 } \displaystyle \sum^{N}_{ j=i+1 } \rm{ord}_2(A_i + A_j) を求めてください。

制約

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

入力

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

N
A_1 A_2 \dots A_N

出力

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


入力例 1

5
2 3 5 7 11

出力例 1

12
  • \rm{ord}_2(2+3) = \rm{ord}_2(5) = 0
  • \rm{ord}_2(2+5) = \rm{ord}_2(7) = 0
  • \rm{ord}_2(2+7) = \rm{ord}_2(9) = 0
  • \rm{ord}_2(2+11) = \rm{ord}_2(13) = 0
  • \rm{ord}_2(3+5) = \rm{ord}_2(8) = 3
  • \rm{ord}_2(3+7) = \rm{ord}_2(10) = 1
  • \rm{ord}_2(3+11) = \rm{ord}_2(14) = 1
  • \rm{ord}_2(5+7) = \rm{ord}_2(12) = 2
  • \rm{ord}_2(5+11) = \rm{ord}_2(16) = 4
  • \rm{ord}_2(7+11) = \rm{ord}_2(18) = 1

これらの合計は 12 です。


入力例 2

2
8 13

出力例 2

0

入力例 3

10
101736933 345226906 942125603 745512475 134406781 47730141 402464131 460079462 359290644 211134700

出力例 3

161

サンプルには含まれませんが、答えが 32 bit 符号付き整数型に収まらない場合があるので注意してください。

Problem Statement

For a positive integer x, let \rm{ord}_2(x) be the number of times 2 divides x.
For example, \rm{ord}_2(24)=3, \rm{ord}_2(17)=0, and \rm{ord}_2(32)=5.

You are given a length-N sequence of integers A=(A_1,A_2,\dots,A_N).
Find \displaystyle \sum^{N}_{ i=1 } \displaystyle \sum^{N}_{ j=i+1 } \rm{ord}_2(A_i + A_j).

Constraints

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

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

5
2 3 5 7 11

Sample Output 1

12
  • \rm{ord}_2(2+3) = \rm{ord}_2(5) = 0
  • \rm{ord}_2(2+5) = \rm{ord}_2(7) = 0
  • \rm{ord}_2(2+7) = \rm{ord}_2(9) = 0
  • \rm{ord}_2(2+11) = \rm{ord}_2(13) = 0
  • \rm{ord}_2(3+5) = \rm{ord}_2(8) = 3
  • \rm{ord}_2(3+7) = \rm{ord}_2(10) = 1
  • \rm{ord}_2(3+11) = \rm{ord}_2(14) = 1
  • \rm{ord}_2(5+7) = \rm{ord}_2(12) = 2
  • \rm{ord}_2(5+11) = \rm{ord}_2(16) = 4
  • \rm{ord}_2(7+11) = \rm{ord}_2(18) = 1

Their sum is 12.


Sample Input 2

2
8 13

Sample Output 2

0

Sample Input 3

10
101736933 345226906 942125603 745512475 134406781 47730141 402464131 460079462 359290644 211134700

Sample Output 3

161

Note that the answer may not fit into 32-bit integer type, which is not exemplified in the samples.

L - 書き換え

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

問題文

黒板に 1 個のある整数が書かれています。

高橋君は次に述べる N 個の操作からなる手順を行います。 i = 1, 2, \ldots, N について、i 番目に行う操作は文字列 s_i と 整数 p_i の組で表され、その意味は下記の通りです。

  • s_iNEGATE のとき、黒板に書かれた整数を、それを -1 倍したものに書き換える( p_i の値は無視する)。
  • s_iCHMIN のとき、もし黒板に書かれた整数が p_i より大きいならば、黒板に書かれた整数を p_i に書き換える。
  • s_iCHMAX のとき、もし黒板に書かれた整数が p_i より小さいならば、黒板に書かれた整数を p_i に書き換える。

あなたに Q 個の質問が与えられます。i = 1, 2, \ldots, Q について、i 番目の質問は下記の通りです。

  • 高橋君が上記の手順を始める前の時点で黒板に書かれている整数が q_i である場合の、高橋君が上記の手順を終えた後の時点で黒板に書かれている整数を求めよ。

Q 個の質問それぞれに対する答えを出力してください。

制約

  • N, Q, p_i, q_i は整数
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • -10^9 \leq p_i, q_i \leq 10^9
  • s_iNEGATECHMINCHMAX のいずれか

入力

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

N Q
s_1 p_1
s_2 p_2
\vdots
s_N p_N
q_1
q_2
\vdots
q_Q

出力

Q 行出力せよ。 下記の形式の通り、i = 1, 2, \ldots, Q について i 行目には i 番目の質問に対する答え X_i を出力せよ。

X_1
X_2
\vdots
X_Q

入力例 1

4 5
CHMAX 3
NEGATE 5
CHMAX -11
NEGATE -12
2
7
15
9
-6

出力例 1

3
7
11
9
3

1 個目の質問の場合、すなわち、高橋君が問題文中の手順を始める前の時点で黒板に書かれている整数が 2 である場合の手順は下記の通りです。

  • 1 番目の操作では、黒板に書かれた整数 23 より小さいので、黒板に書かれた整数を 3 に書き換えます。
  • 2 番目の操作では、黒板に書かれた整数 3 を、それを -1 倍した -3 に書き換えます。
  • 3 番目の操作では、黒板に書かれた整数 -3-11 より小さくはないので、何もしません。
  • 4 番目の操作では、黒板に書かれた整数 -3 を、それを -1 倍した 3 に書き換えます。

よって、この場合の高橋君が上記の手順を終えた後の時点で黒板に書かれている整数は 3 であり、1 番目の質問に対する答えは 3 です。


入力例 2

15 10
CHMAX -14
CHMIN 12
CHMIN 6
NEGATE 10
CHMAX -8
NEGATE -2
NEGATE -20
NEGATE 14
NEGATE 15
NEGATE 5
NEGATE 12
CHMIN 13
CHMIN 20
CHMIN 16
NEGATE -3
-2
-7
6
-18
-20
-18
14
18
-13
-4

出力例 2

-2
-7
6
-13
-13
-13
6
6
-13
-4

Problem Statement

An integer is written on a blackboard.

Takahashi will perform a procedure consisting of N operations described below. For i = 1, 2, \ldots, N, the i-th operation is represented by a pair of a string s_i and an integer p_i, whose meanings are as follows:

  • If s_i is NEGATE, replace the integer on the blackboard with -1 times that integer (ignoring the value p_i).
  • If s_i is CHMIN, if the integer on the blackboard is greater than p_i, replace it with p_i.
  • If s_i is CHMAX, if the integer on the blackboard is less than p_i, replace it with p_i.

You are given Q queries. For i = 1, 2, \ldots, Q, the i-th query is as follows.

  • If the integer initially written on the blackboard before the procedure above is q_i, find the integer finally written on the blackboard after the procedure.

Print the answer for each of the Q queries.

Constraints

  • N, Q, p_i, and q_i are integers.
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • -10^9 \leq p_i, q_i \leq 10^9
  • s_i is NEGATE, CHMIN, or CHMAX.

Input

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

N Q
s_1 p_1
s_2 p_2
\vdots
s_N p_N
q_1
q_2
\vdots
q_Q

Output

Print Q lines. As shown below, for i = 1, 2, \ldots, Q, the i-th line should contain the answer X_i to the i-th query.

X_1
X_2
\vdots
X_Q

Sample Input 1

4 5
CHMAX 3
NEGATE 5
CHMAX -11
NEGATE -12
2
7
15
9
-6

Sample Output 1

3
7
11
9
3

For the first query, that is, if 2 is initially written on the blackboard before the procedure in the problem statement, the operations proceed as follows:

  • In the 1-st operation, the integer on the blackboard 2 is less than 3, so replace it with 3.
  • In the 2-nd operation, multiply the integer on the blackboard 3 by -1, replacing it with -3.
  • In the 3-rd operation, the integer on the blackboard -3 is no less than -11, so do nothing.
  • In the 4-th operation, multiply the integer on the blackboard -3 by -1, replacing it with 3.

Thus, in this case, the integer finally written on the blackboard after the procedure is 3, which is the answer to the 1-st query.


Sample Input 2

15 10
CHMAX -14
CHMIN 12
CHMIN 6
NEGATE 10
CHMAX -8
NEGATE -2
NEGATE -20
NEGATE 14
NEGATE 15
NEGATE 5
NEGATE 12
CHMIN 13
CHMIN 20
CHMIN 16
NEGATE -3
-2
-7
6
-18
-20
-18
14
18
-13
-4

Sample Output 2

-2
-7
6
-13
-13
-13
6
6
-13
-4
M - グループ分け

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

問題文

1 から N の番号がついた N 人の人がいます。人 i は飴を A_i 個持っています。

また、仲が悪い人の組 M(X_1,Y_1),\ldots,(X_M,Y_M) が与えられます。

以下の 2 つの条件をともに満たすように N 人をいくつかのグループに分けるとき、グループ数の最小値はいくつですか?

  • どのグループも、そのグループに属する人が持っている飴の個数の合計が S 以下
  • 仲が悪い人の組が同じグループに属さない

制約

  • 1 \leq N \leq 20
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1\leq X_i < Y_i \leq N
  • (X_i,Y_i) は相異なる
  • 0 \leq A_i \leq 10^7
  • \max_i A_i \leq S \leq 10^9
  • 入力は全て整数である

入力

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

N M S
A_1 A_2 \ldots A_N
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M

出力

答えを出力せよ。


入力例 1

3 1 10
2 3 4
1 2

出力例 1

2

1 と人 2 は仲が悪いため同じグループに属することができません。 人 1 のみからなるグループと、人 2,3 からなるグループの 2 グループに分けることで条件を満たします。


入力例 2

5 0 10
2 3 4 10 10

出力例 2

3

1,2,3 からなるグループ、人 4 のみからなるグループ、人 5 のみからなるグループの 3 グループに分けることで条件を満たします。


入力例 3

19 10 13639949
6248137 1929297 1115672 3165903 771666 2658398 3460632 3239969 5759071 1396990 5625214 7940774 1755330 7704375 8252319 2891254 3580852 7211614 6847141
11 17
1 11
9 10
10 16
11 19
4 14
2 9
9 19
9 11
17 19

出力例 3

7

Problem Statement

There are N people numbered 1 to N. Person i has A_i candies.

Additionally, you are given M pairs of people (X_1,Y_1),\ldots,(X_M,Y_M) who are on bad terms with each other.

What is the minimum number of groups into which the N people can be divided so that both of the following conditions are satisfied?

  • The people in each group have at most S candies in total.
  • No two people on bad terms are in the same group.

Constraints

  • 1 \leq N \leq 20
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1\leq X_i < Y_i \leq N
  • The pairs (X_i,Y_i) are distinct.
  • 0 \leq A_i \leq 10^7
  • \max_i A_i \leq S \leq 10^9
  • All inputs are integers.

Input

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

N M S
A_1 A_2 \ldots A_N
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M

Output

Print the answer.


Sample Input 1

3 1 10
2 3 4
1 2

Sample Output 1

2

Person 1 and person 2 are on bad terms and thus cannot be in the same group. The conditions are satisfied by dividing them into two groups, one composed only of person 1 and the other composed of persons 2 and 3.


Sample Input 2

5 0 10
2 3 4 10 10

Sample Output 2

3

The conditions are satisfied by dividing them into three groups: one composed of persons 1,2,3, one composed only of person 4, and one composed only of person 5.


Sample Input 3

19 10 13639949
6248137 1929297 1115672 3165903 771666 2658398 3460632 3239969 5759071 1396990 5625214 7940774 1755330 7704375 8252319 2891254 3580852 7211614 6847141
11 17
1 11
9 10
10 16
11 19
4 14
2 9
9 19
9 11
17 19

Sample Output 3

7
N - 平面徒競走

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

問題文

T 君と A 君が 2 次元平面上で徒競走をしています。 T 君は (T_x,T_y) がスタート地点であり、1 秒あたり V_T の速さで走ります。 A 君は (A_x,A_y) がスタート地点であり、1 秒あたり V_A の速さで走ります。

笛がなると、2 人はそれぞれのスタート地点から一斉にゴール地点に向かって走り出します。 ゴール地点は 2 人に共通で、その x 座標は 0 と決まっていますが、y 座標はまだ決まっていません(ここで、T_x \neq 0 かつ A_x \neq 0 が保証されます)。

あなたは、T 君がこの徒競走に負けないように(すなわち、T 君が先にゴール地点に到着するか、2 人が同時にゴール地点に到着するように)ゴール地点の y 座標を決めたいです。 この目的を達成できる y 座標の値の集合を S としたとき、

  • S が空ならば、0
  • S が空でなく、かつ S の最小値と最大値が共に存在するならば、最大値と最小値の差
  • S が空でないが、S の最小値と最大値のうち少なくとも一方が存在しないならば、inf

をそれぞれ出力してください。

制約

  • -10^4 \leq T_x,T_y,A_x,A_y \leq 10^4
  • T_x \neq 0
  • A_x \neq 0
  • 1 \leq V_T,V_A \leq 10^2
  • 入力は全て整数

入力

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

T_x T_y V_T
A_x A_y V_A

出力

答えを実数または文字列 inf として出力せよ。 答えが実数の場合、出力は真の値との絶対誤差または相対誤差が 10^{−9} 以下のとき正解と判定される。


入力例 1

1 0 1
1 3 2

出力例 1

3.464101615137754

ゴール地点の y 座標について、2 つの具体例を挙げます。

  • y=0 のとき:ゴール地点に到達するまでに、T 君は \frac{1}{1}=1 秒、A 君は \frac{\sqrt{10}}{2}=1.58\dots 秒かかります。
  • y=2 のとき:ゴール地点に到達するまでに、T 君は \frac{\sqrt{5}}{1}=2.23\dots 秒、A 君は \frac{\sqrt{2}}{2}=0.70\dots 秒かかります。

T 君が先にゴール地点に到着するか、2 人が同時にゴール地点に到着する条件は -1-\sqrt{3} \leq y \leq -1+\sqrt{3} であることが証明できます。
よって、S の最大値は -1+\sqrt{3}、最小値は -1-\sqrt{3} であり、答えは (-1+\sqrt{3})-(-1-\sqrt{3})=2\sqrt{3}=3.46\dots です。


入力例 2

3 0 1
3 3 2

出力例 2

0

ゴール地点の y 座標によらず A 君は T 君より先にゴール地点に到達するため、S は空です。


入力例 3

4 -10 10
-1 3 5

出力例 3

inf

S には最大値も最小値も存在しません。

Problem Statement

Two runners, T and A, are competing in a footrace on a two-dimensional plane. T starts at (T_x,T_y) and runs at a speed of V_T per second. A starts at (A_x,A_y) and runs at a speed of V_A per second.

Once the whistle is blown, they start running simultaneously from their individual starting points toward a common goal. The goal has a fixed x coordinate of 0, but its y coordinate is not determined yet. (Here, it is guaranteed that T_x \neq 0 and A_x \neq 0.)

You want to determine the y coordinate of the goal so that T never loses the race (that is, T reaches the goal first, or the two runners reach the goal at the same time). Let S be the set of y coordinates that achieve the objective.

  • If S is empty, print 0;
  • if S is non-empty, and S has both the maximum and minimum values, then print the difference between them;
  • if S is non-empty, but S does not have both the maximum and minimum values, then print inf.

Constraints

  • -10^4 \leq T_x,T_y,A_x,A_y \leq 10^4
  • T_x \neq 0
  • A_x \neq 0
  • 1 \leq V_T,V_A \leq 10^2
  • All input values are integers.

Input

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

T_x T_y V_T
A_x A_y V_A

Output

Print the result as a real value, or inf. If the answer is a real value, your output is considered correct if its absolute or relative error from the true value is at most 10^{−9}.


Sample Input 1

1 0 1
1 3 2

Sample Output 1

3.464101615137754

We consider two y coordinates of the goal as examples.

  • If y=0: it takes \frac{1}{1}=1 seconds for T, and \frac{\sqrt{10}}{2}=1.58\dots seconds for A, to reach the goal.
  • If y=2: it takes \frac{\sqrt{5}}{1}=2.23\dots seconds for T, and \frac{\sqrt{2}}{2}=0.70\dots seconds for A, to reach the goal.

One can prove that T reaches the goal first, or the two runners reach the goal at the same time if and only if -1-\sqrt{3} \leq y \leq -1+\sqrt{3}.
Thus, the maximum value of S is -1+\sqrt{3}, and its minimum value is -1-\sqrt{3}, so the answer is (-1+\sqrt{3})-(-1-\sqrt{3})=2\sqrt{3}=3.46\dots.


Sample Input 2

3 0 1
3 3 2

Sample Output 2

0

Regardless of the y coordinate of the goal, A always reaches the goal earlier than T, so S is empty.


Sample Input 3

4 -10 10
-1 3 5

Sample Output 3

inf

S has neither the maximum nor minimum value.

O - 蟻の移動

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

問題文

1 から N までの番号が付けられた N 匹の蟻が数直線上に並んでいます。 この数直線上では右に行くほど座標が大きくなります。 初期状態において、蟻 i は初期座標 X_i におり、向いている向きは文字 D_i によって表されます。 具体的には、D_i= R のとき右を、D_i= L のとき左を向いています。 ここで、X_1,X_2,\dots,X_N はすべて偶数であり、互いに相異なることが保証されます。

すぬけくんが指を鳴らすと、蟻たちは以下の規則に従って動き始めます。

  • それぞれの蟻は、自分が向いている方向に向かって秒速 1 で数直線上を移動する。
  • 2 匹の蟻が同時にある座標に到達した場合は、それぞれの蟻が向きを反対方向に変えて移動を続ける。これを蟻の衝突とよぶ。 なお、3 匹以上の蟻が同時にある座標に到達することはないことが証明できる。

以下のいずれかの形式のクエリが合わせて Q 個与えられるので、順番に処理してください。

  • 1 x d : 初期座標が x、向いている向きが文字 d で表されるような蟻を初期状態に追加する。 ここで、x は偶数であり、既に存在するどの蟻の初期座標とも一致しないことが保証される。
  • 2 l r : 現在の初期状態において時刻 0 にすぬけくんが指を鳴らした場合に、蟻 1 と他の蟻との衝突が起こる時刻を早い順に t_1,t_2,\dots とする。 t_{l}+t_{l+1}+\dots +t_{r} を求め、出力する。 ただし、l \leq r、および蟻 1 と他の蟻との衝突は r 回以上起こることが保証される。

なお、2 種類目のクエリは独立であることに注意してください。 すなわち、2 種類目のクエリを処理することで、蟻たちの初期状態が変わることはありません。

制約

  • N,Q は整数
  • 1\leq N,Q \leq 10^5
  • X_i は偶数
  • |X_i| \leq 10^9
  • X_i \neq X_j\ (i \neq j)
  • D_iR または L
  • 1 種類目のクエリにおいて、
    • x は偶数
    • |x| \leq 10^9
    • そのクエリの段階において、初期座標が x であるような蟻は存在しない
    • dR または L
  • 2 種類目のクエリにおいて、
    • l,r は整数
    • 1 \leq l \leq r \leq M  ただし M はそのクエリの段階での初期状態においてすぬけくんが指を鳴らした場合に蟻 1 と他の蟻との衝突が起こる回数

入力

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

N Q
X_1 D_1
X_2 D_2
\vdots
X_N D_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

ここで、\mathrm{query}_ii 番目のクエリを表し、以下のいずれかの形式である。

1 x d
2 l r

出力

2 種類目のクエリの数を T としたとき、T 行出力せよ。 i\ (1\leq i \leq T) 行目には、2 種類目のクエリのうち i 番目のものに対する答えを整数として出力せよ。


入力例 1

2 5
2 R
10 L
2 1 1
1 0 L
1 -2 R
2 1 2
2 2 2

出力例 1

4
10
6

クエリを処理する前の段階での初期状態においてすぬけくんが指を鳴らした場合、蟻たちは以下のように行動します。

  • 時刻 0 にすべての蟻が移動を始める。それぞれの蟻が向いている向きは右、左である。
  • 時刻 4\ (:=t_1) に座標 6 で蟻 1,2 が衝突する。それぞれの蟻が向いている向きは左、右になる。

よって、1 番目のクエリに対する答えは t_1=4 です。

3 番目のクエリまで処理した段階での初期状態においてすぬけくんが指を鳴らした場合、蟻たちは以下のように行動します。 ただし、2,3 番目のクエリで追加された蟻の番号をそれぞれ 3,4 とします。

  • 時刻 0 にすべての蟻が移動を始める。それぞれの蟻が向いている向きは右、左、左、右である。
  • 時刻 1 に座標 -1 で蟻 3,4 が衝突する。それぞれの蟻が向いている向きは右、左、右、左になる。
  • 時刻 4\ (:=t_1) に座標 6 で蟻 1,2 が衝突する。それぞれの蟻が向いている向きは左、右、右、左になる。
  • 時刻 6\ (:=t_2) に座標 4 で蟻 1,3 が衝突する。それぞれの蟻が向いている向きは右、右、左、左になる。

よって、4 番目のクエリに対する答えは t_1+t_2=105 番目のクエリに対する答えは t_2=6 です。


入力例 2

10 10
0 R
16 L
94 L
-96 R
-24 L
52 R
-10 L
-38 L
50 R
98 L
1 -66 L
2 2 3
1 48 L
1 34 R
2 2 2
1 8 L
2 2 3
2 2 3
1 62 L
2 1 3

出力例 2

151
56
108
108
112

Problem Statement

N ants numbered 1 through N are on a number line, whose positive direction points to the right. In the initial state, ant i is at the initial coordinate X_i and facing the direction described by a character D_i. Specifically, D_i= R means it is facing right, and D_i= L means it is facing left. Here, it is guaranteed that X_1,X_2,\dots, and X_N are pairwise distinct even numbers.

Once Snuke snaps his fingers, the ants starts moving according to the following rules.

  • Each ant moves at a speed of 1 per second in the direction it is facing.
  • Whenever two ants reach the same coordinate, they flip their directions and continue moving. This event is called a collision of ants. One can prove that three or more ants never reach the same coordinate at the same time.

You are given a total of Q queries, each of one of the following types. Process them in order.

  • 1 x d : Add an ant with initial coordinate x and initial direction d to the initial state. Here, it is guaranteed that x is even and does not coincide with the initial coordinate of any ant.
  • 2 l r : If Snuke snaps his fingers at time 0, let t_1,t_2,\dots be the times at which ant 1 collides with the other ants, listed in chronological order. Evaluate t_{l}+t_{l+1}+\dots +t_{r} and print it. It is guaranteed that l \leq r and ant 1 collides with the other ants at least r times.

Note that queries of the second type are independent. In other words, processing a query of the second type does not actually modify the initial states of the ants.

Constraints

  • N and Q are integers.
  • 1\leq N,Q \leq 10^5
  • X_i is even.
  • |X_i| \leq 10^9
  • X_i \neq X_j\ (i \neq j)
  • D_i is R or L.
  • For each query of the first kind,
    • x is even.
    • |x| \leq 10^9
    • At the point the query is processed, there is no ant with initial coordinate x.
    • d is R or L.
  • For each query of the second kind,
    • l and r are integers.
    • 1 \leq l \leq r \leq M, where M is the number of times ant 1 collides with the other ants, starting from the initial state at the point the query is processed.

Input

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

N Q
X_1 D_1
X_2 D_2
\vdots
X_N D_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Here, \mathrm{query}_i denotes the i-th query and is of one of the following formats:

1 x d
2 l r

Output

If there are T queries of the second kind, print T lines. In the i-th (1\leq i \leq T) line, print the answer to the i-th such query.


Sample Input 1

2 5
2 R
10 L
2 1 1
1 0 L
1 -2 R
2 1 2
2 2 2

Sample Output 1

4
10
6

If Snuke snaps his fingers in the initial state before the queries are processed, the ants move as follows.

  • At time 0, all the ants start moving. The two ants are facing right and left.
  • At time 4\ (:=t_1), ants 1 and 2 collide at coordinate 6. Now the two ants are facing left and right.

Thus, the answer to the 1-st query is t_1=4.

If Snuke snaps his fingers in the initial state where the first three queries are processed, the ants move as follows. Here we call the ants added by the 2-nd and 3-rd query ant 3 and ant 4, respectively.

  • At time 0, all the ants start moving. The four ants are facing right, left, left, and right.
  • At time 1, ants 3 and 4 collides at coordinate -1. Now, the four ants are facing right, left, right, and left.
  • At time 4\ (:=t_1), ants 1 and 2 collides at coordinate 6. Now, the four ants are facing left, right, right, and left.
  • At time 6\ (:=t_2), ants 1 and 3 collides at coordinate 4. Now, the four ants are facing right, right, left, and left.

Therefore, the answer to the 4-th query is t_1+t_2=10, and the answer to the 5-th query is t_2=6.


Sample Input 2

10 10
0 R
16 L
94 L
-96 R
-24 L
52 R
-10 L
-38 L
50 R
98 L
1 -66 L
2 2 3
1 48 L
1 34 R
2 2 2
1 8 L
2 2 3
2 2 3
1 62 L
2 1 3

Sample Output 2

151
56
108
108
112