A - Chord

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

英大文字からなる長さ 3 の文字列 S が与えられます。SACEBDFCEGDFAEGBFACGBD のいずれかと等しいとき Yes を、そうでないとき No を出力してください。

制約

  • S は英大文字からなる長さ 3 の文字列

入力

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

S

出力

SACEBDFCEGDFAEGBFACGBD のいずれかと等しいとき Yes を、そうでないとき No を出力せよ。


入力例 1

ABC

出力例 1

No

S = ABC のとき、SACEBDFCEGDFAEGBFACGBD のいずれとも等しくないので No を出力します。


入力例 2

FAC

出力例 2

Yes

入力例 3

XYX

出力例 3

No

Score : 100 points

Problem Statement

Given a length-3 string S consisting of uppercase English letters, print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.

Constraints

  • S is a length-3 string consisting of uppercase English letters.

Input

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

S

Output

Print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.


Sample Input 1

ABC

Sample Output 1

No

When S = ABC, S does not equal any of ACE, BDF, CEG, DFA, EGB, FAC, and GBD, so No should be printed.


Sample Input 2

FAC

Sample Output 2

Yes

Sample Input 3

XYX

Sample Output 3

No
B - Overall Winner

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋くんと青木くんが N 回の試合を行いました。 これらの試合の結果を表す長さ N の文字列 S が与えられます。 i 回目の試合の勝者は、Si 文字目が T ならば高橋くん、A ならば青木くんです。

高橋くんと青木くんのうち、勝った試合の数が多い方を総合勝者とします。 ただし、勝った試合の数が同じである場合は、先にその勝ち数に達した者を総合勝者とします。 高橋くんと青木くんのどちらが総合勝者であるか求めてください。

制約

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

入力

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

N
S

出力

総合勝者が高橋くんならば T を、青木くんならば A を出力せよ。


入力例 1

5
TTAAT

出力例 1

T

高橋くんは 3 回の試合に勝ち、青木くんは 2 回の試合に勝ちました。 よって、勝った試合の数が多い高橋くんが総合勝者です。


入力例 2

6
ATTATA

出力例 2

T

高橋くんと青木くんのどちらも 3 回の試合に勝ちました。 また、高橋くんは 5 回目の試合で 3 勝目に達し、青木くんは 6 回目の試合で 3 勝目に達しました。 よって、先に 3 勝目に達した高橋くんが総合勝者です。


入力例 3

1
A

出力例 3

A

Score : 100 points

Problem Statement

Takahashi and Aoki played N games. You are given a string S of length N, representing the results of these games. Takahashi won the i-th game if the i-th character of S is T, and Aoki won that game if it is A.

The overall winner between Takahashi and Aoki is the one who won more games than the other. If they had the same number of wins, the overall winner is the one who reached that number of wins first. Find the overall winner: Takahashi or Aoki.

Constraints

  • 1\leq N \leq 100
  • N is an integer.
  • S is a string of length N consisting of T and A.

Input

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

N
S

Output

If the overall winner is Takahashi, print T; if it is Aoki, print A.


Sample Input 1

5
TTAAT

Sample Output 1

T

Takahashi won three games, and Aoki won two. Thus, the overall winner is Takahashi, who won more games.


Sample Input 2

6
ATTATA

Sample Output 2

T

Both Takahashi and Aoki won three games. Takahashi reached three wins in the fifth game, and Aoki in the sixth game. Thus, the overall winner is Takahashi, who reached three wins first.


Sample Input 3

1
A

Sample Output 3

A
C - Unique Nicknames

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

1, 人 2, \dotsNN 人の人がいます。人 i の姓は s_i、名は t_i です。

N 人の人すべてにあだ名をつけることを考えます。人 i のあだ名 a_i は以下の条件を満たす必要があります。

  • a_i は人 i の姓あるいは名と一致する。言い換えると、a_i = s_i または a_i = t_i の少なくとも一方が成り立つ。
  • a_i は自分以外の人の姓および名のどちらとも一致しない。言い換えると、1 \leq j \leq N, i \neq j を満たすすべての整数 j について a_i \neq s_j かつ a_i \neq t_j が成り立つ。

N 人全員に条件を満たすあだ名をつけることは可能でしょうか。可能ならば Yes を、そうでないならば No を出力してください。

制約

  • 2 \leq N \leq 100
  • N は整数である。
  • s_i,t_i は英小文字からなる 1 文字以上 10 文字以下の文字列である。

入力

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

N
s_1 t_1
s_2 t_2
\vdots
s_N t_N

出力

N 人すべてにあだ名をつけることが可能ならば Yes を、そうでないならば No を出力せよ。


入力例 1

3
tanaka taro
tanaka jiro
suzuki hanako

出力例 1

Yes

a_1 = taro, a_2 = jiro, a_3 = hanako とすれば、これは問題文にあるあだ名の条件を満たしています。(a_3suzuki でもよいです。)
ここで、a_1 = tanaka とはできないことに注意してください。なぜならば 人 2 の姓 s_2 もまた tanaka であるため、あだ名の条件の 2 つ目を満たさなくなるからです。


入力例 2

3
aaa bbb
xxx aaa
bbb yyy

出力例 2

No

問題文の条件を満たすあだ名のつけ方は存在しません。


入力例 3

2
tanaka taro
tanaka taro

出力例 3

No

同姓同名である人の組が存在する場合もあります。


入力例 4

3
takahashi chokudai
aoki kensho
snu ke

出力例 4

Yes

a_1 = chokudai, a_2 = kensho, a_3 = ke とすればよいです。

Score : 200 points

Problem Statement

There are N people numbered Person 1, Person 2, \dots, and Person N. Person i has a family name s_i and a given name t_i.

Consider giving a nickname to each of the N people. Person i's nickname a_i should satisfy all the conditions below.

  • a_i coincides with Person i's family name or given name. In other words, a_i = s_i and/or a_i = t_i holds.
  • a_i does not coincide with the family name and the given name of any other person. In other words, for all integer j such that 1 \leq j \leq N and i \neq j, it holds that a_i \neq s_j and a_i \neq t_j.

Is it possible to give nicknames to all the N people? If it is possible, print Yes; otherwise, print No.

Constraints

  • 2 \leq N \leq 100
  • N is an integer.
  • s_i and t_i are strings of lengths between 1 and 10 (inclusive) consisting of lowercase English alphabets.

Input

Input is given from Standard Input in the following format:

N
s_1 t_1
s_2 t_2
\vdots
s_N t_N

Output

If it is possible to give nicknames to all the N people, print Yes; otherwise, print No.


Sample Input 1

3
tanaka taro
tanaka jiro
suzuki hanako

Sample Output 1

Yes

The following assignment satisfies the conditions of nicknames described in the Problem Statement: a_1 = taro, a_2 = jiro, a_3 = hanako. (a_3 may be suzuki, too.)
However, note that we cannot let a_1 = tanaka, which violates the second condition of nicknames, since Person 2's family name s_2 is tanaka too.


Sample Input 2

3
aaa bbb
xxx aaa
bbb yyy

Sample Output 2

No

There is no way to give nicknames satisfying the conditions in the Problem Statement.


Sample Input 3

2
tanaka taro
tanaka taro

Sample Output 3

No

There may be a pair of people with the same family name and the same given name.


Sample Input 4

3
takahashi chokudai
aoki kensho
snu ke

Sample Output 4

Yes

We can let a_1 = chokudai, a_2 = kensho, and a_3 = ke.

D - Farthest Point

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

xy 平面上に 1 から N までの番号が付いた N 個の点があります。点 i は座標 (X_i, Y_i) にあり、相異なる 2 点の座標は異なります。

各点について、その点からの距離が最大である点を求めてその点の番号を出力してください。 ただし、距離が最大である点が複数ある場合はその中で最も番号が小さい点の番号を出力してください。

ここで、距離はユークリッド距離、すなわち 2(x_1,y_1)(x_2,y_2) に対し、この 2 点間の距離が \sqrt{(x_1-x_2)^{2}+(y_1-y_2)^{2}} であるものとして定められています。

制約

  • 2 \leq N \leq 100
  • -1000 \leq X_i, Y_i \leq 1000
  • i \neq j ならば (X_i, Y_i) \neq (X_j, Y_j)
  • 入力は全て整数である。

入力

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

N 行出力せよ。i 行目には点 i からの距離が最大である点の番号を出力せよ。


入力例 1

4
0 0
2 4
5 0
3 4

出力例 1

3
3
1
1

以下の図のように点が並んでいます。ここで P_i は点 i を表します。 1 からの距離が最大である点は点 3,4 で、その中で番号が小さいのは点 3 です。

2 からの距離が最大である点は点 3 です。

3 からの距離が最大である点は点 1,2 で、その中で番号が小さいのは点 1 です。

4 からの距離が最大である点は点 1 です。


入力例 2

6
3 2
1 6
4 5
1 3
5 5
9 8

出力例 2

6
6
6
6
6
4

Score: 200 points

Problem Statement

On the xy-plane, there are N points with ID numbers from 1 to N. Point i is located at coordinates (X_i, Y_i), and no two points have the same coordinates.

From each point, find the farthest point and print its ID number. If multiple points are the farthest, print the smallest of the ID numbers of those points.

Here, we use the Euclidean distance: for two points (x_1,y_1) and (x_2,y_2), the distance between them is \sqrt{(x_1-x_2)^{2}+(y_1-y_2)^{2}}.

Constraints

  • 2 \leq N \leq 100
  • -1000 \leq X_i, Y_i \leq 1000
  • (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
  • All input values are integers.

Input

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print N lines. The i-th line should contain the ID number of the farthest point from point i.


Sample Input 1

4
0 0
2 4
5 0
3 4

Sample Output 1

3
3
1
1

The following figure shows the arrangement of the points. Here, P_i represents point i. The farthest point from point 1 are points 3 and 4, and point 3 has the smaller ID number.

The farthest point from point 2 is point 3.

The farthest point from point 3 are points 1 and 2, and point 1 has the smaller ID number.

The farthest point from point 4 is point 1.


Sample Input 2

6
3 2
1 6
4 5
1 3
5 5
9 8

Sample Output 2

6
6
6
6
6
4
E - One More aab aba baa

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

文字列 S の各文字を並べ替えて作ることが可能な文字列を辞書順にすべて列挙したとき、前から K 番目にくる文字列を求めてください。

「各文字を並べ替えて作ることが可能な文字列」とは? 「文字列 A が文字列 B の各文字を並べ替えて作ることが可能な文字列である」とは、任意の文字が文字列 A と文字列 B に同数含まれるということを指します。

制約

  • 1 \le |S| \le 8
  • S は英小文字のみからなる
  • S の各文字を並べ替えてできる文字列は K 種類以上存在する

入力

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

S K

出力

答えを出力せよ。


入力例 1

aab 2

出力例 1

aba

文字列 aab の各文字を並べ替えて作ることが可能な文字列は \{ aab, aba, baa \}3 つですが、このうち辞書順で前から 2 番目にくるものは aba です。


入力例 2

baba 4

出力例 2

baab

入力例 3

ydxwacbz 40320

出力例 3

zyxwdcba

Score : 300 points

Problem Statement

Find the K-th lexicographically smallest string among the strings that are permutations of a string S.

What is a permutation of a string?A string A is said to be a permutation of a string B when any character occurs the same number of times in A and B.

Constraints

  • 1 \le |S| \le 8
  • S consists of lowercase English letters.
  • There are at least K distinct strings that are permutations of S.

Input

Input is given from Standard Input in the following format:

S K

Output

Print the answer.


Sample Input 1

aab 2

Sample Output 1

aba

There are three permutations of a string aab: \{ aab, aba, baa \}. The 2-nd lexicographically smallest of them is aba.


Sample Input 2

baba 4

Sample Output 2

baab

Sample Input 3

ydxwacbz 40320

Sample Output 3

zyxwdcba
F - Swiss-System Tournament

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

1 から 2N の番号がついた 2N 人でじゃんけん大会をします。

大会は M ラウンドからなり、各ラウンドは、全ての人が 1 度ずつ参加するような 11N 試合からなります。

i=0,1,\ldots,M について、i ラウンド目の終了時点での順位を次のように決めます。

  • i ラウンド目までの勝数が多い方が上位
  • i ラウンド目までの勝数が同じときは、番号が小さい方が上位

また、i=1,\ldots,M について、i ラウンド目の各試合の組み合わせを次のように決めます。

  • k=1,2,\ldots,N について、i-1 ラウンド目終了時点の順位が 2k-1 位の人と 2k 位の人が試合をする

各試合では、対戦する 2 人がそれぞれ 1 度だけ手を出し、勝ち・負け・引き分けのいずれかの結果が発生します。

未来予知ができる高橋君は、人 ij ラウンド目の試合で出す手が A_{i,j} であることを知っています。
A_{i,j}G, C, P のいずれかであり、それぞれグー、チョキ、パーを表します。

M ラウンド目終了時点の順位を求めてください。

じゃんけんのルール じゃんけんの結果は、2 人の出した手に応じて次のように決まります。
  • 一方がグーで他方がチョキのとき、グーを出した人が勝ち、チョキを出した人は負け
  • 一方がチョキで他方がパーのとき、チョキを出した人が勝ち、パーを出した人は負け
  • 一方がパーで他方がグーのとき、パーを出した人が勝ち、グーを出した人は負け
  • 両者が同じ手を出したとき、引き分け

制約

  • 1 \leq N \leq 50
  • 1 \leq M \leq 100
  • A_{i,j}G, C, P のいずれか

入力

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

N M
A_{1,1}A_{1,2}\ldots A_{1,M}
A_{2,1}A_{2,2}\ldots A_{2,M}
\vdots
A_{2N,1}A_{2N,2}\ldots A_{2N,M}

出力

2N 行出力せよ。

i 行目には、M ラウンド目終了時点での順位が i 位である人の番号を出力せよ。


入力例 1

2 3
GCP
PPP
CCC
PPC

出力例 1

3
1
2
4

1 ラウンド目では人 1234 がそれぞれ試合をし、前者の試合は人 2 が、後者の試合は人 3 が勝ちます。
2 ラウンド目では人 2314 がそれぞれ試合をし、前者の試合は人 3 が、後者の試合は人 1 が勝ちます。
3 ラウンド目では人 3124 がそれぞれ試合をし、前者の試合は人 3 が、後者の試合は人 4 が勝ちます。
よって最終的な順位は、上位から順に人 3,1,2,4 となります。


入力例 2

2 2
GC
PG
CG
PP

出力例 2

1
2
3
4

1 ラウンド目では人 1234 がそれぞれ試合をし、前者の試合は人 2 が、後者の試合は人 3 が勝ちます。
2 ラウンド目では人 2314 がそれぞれ試合をし、前者の試合は引き分け、後者の試合は人 1 が勝ちます。
よって最終的な順位は、上位から順に人 1,2,3,4 となります。

Score : 300 points

Problem Statement

2N players, with ID numbers 1 through 2N, will participate in a rock-scissors-paper contest.

The contest has M rounds. Each round has N one-on-one matches, where each player plays in one of them.

For each i=0, 1, \ldots, M, the players' ranks at the end of the i-th round are determined as follows.

  • A player with more wins in the first i rounds ranks higher.
  • Ties are broken by ID numbers: a player with a smaller ID number ranks higher.

Additionally, for each i=1, \ldots, M, the matches in the i-th round are arranged as follows.

  • For each k=1, 2, \ldots, N, a match is played between the players who rank (2k-1)-th and 2k-th at the end of the (i-1)-th round.

In each match, the two players play a hand just once, resulting in one player's win and the other's loss, or a draw.

Takahashi, who can foresee the future, knows that Player i will play A_{i, j} in their match in the j-th round, where A_{i,j} is G, C, or P.
Here, G stands for rock, C stands for scissors, and P stands for paper. (These derive from Japanese.)

Find the players' ranks at the end of the M-th round.

Rules of rock-scissors-paper The result of a rock-scissors-paper match is determined as follows, based on the hands played by the two players.
  • If one player plays rock (G) and the other plays scissors (C), the player playing rock (G) wins.
  • If one player plays scissors (C) and the other plays paper (P), the player playing scissors (C) wins.
  • If one player plays paper (P) and the other plays rock (G), the player playing paper (P) wins.
  • If the players play the same hand, the match is drawn.

Constraints

  • 1 \leq N \leq 50
  • 1 \leq M \leq 100
  • A_{i,j} is G, C, or P.

Input

Input is given from Standard Input in the following format:

N M
A_{1,1}A_{1,2}\ldots A_{1,M}
A_{2,1}A_{2,2}\ldots A_{2,M}
\vdots
A_{2N,1}A_{2N,2}\ldots A_{2N,M}

Output

Print 2N lines.

The i-th line should contain the ID number of the player who ranks i-th at the end of the M-th round.


Sample Input 1

2 3
GCP
PPP
CCC
PPC

Sample Output 1

3
1
2
4

In the first round, matches are played between Players 1 and 2, and between Players 3 and 4. Player 2 wins the former, and Player 3 wins the latter.
In the second round, matches are played between Players 2 and 3, and between Players 1 and 4. Player 3 wins the former, and Player 1 wins the latter.
In the third round, matches are played between Players 3 and 1, and between Players 2 and 4. Player 3 wins the former, and Player 4 wins the latter.
Therefore, in the end, the players are ranked in the following order: 3,1,2,4, from highest to lowest.


Sample Input 2

2 2
GC
PG
CG
PP

Sample Output 2

1
2
3
4

In the first round, matches are played between Players 1 and 2, and between Players 3 and 4. Player 2 wins the former, and Player 3 wins the latter.
In the second round, matches are played between Players 2 and 3, and between Players 1 and 4. The former is drawn, and Player 1 wins the latter.
Therefore, in the end, the players are ranked in the following order: 1,2,3,4, from highest to lowest.

G - Shortest Path 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

N 頂点 M 辺の単純連結無向グラフが与えられます。頂点 i\,(1\leq i \leq N) は重み A_i を持ちます。また、辺 j\,(1\leq j \leq M) は頂点 U_j,V_j を双方向に結び、重み B_j を持ちます。

このグラフ上のパスの重みを、パス上に現れる頂点の重みと辺の重みの総和と定義します。

i=2,3,\dots,N について、以下の問題を解いてください。

  • 頂点 1 から頂点 i までのパスであって、重みが最小となるものの重みを求めよ。

制約

  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq U_j < V_j \leq N
  • i\neq j なら (U_i,V_i) \neq (U_j,V_j)
  • グラフは連結である
  • 0 \leq A_i \leq 10^9
  • 0 \leq B_j \leq 10^9
  • 入力はすべて整数

入力

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

N M
A_1 A_2 \dots A_N
U_1 V_1 B_1
U_2 V_2 B_2
\vdots
U_M V_M B_M

出力

i=2,3,\dots,N に対する答えを、この順に空白区切りで一行で出力せよ。


入力例 1

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

出力例 1

4 9

頂点 1 から頂点 2 へのパスを考えます。 パス 1 \to 2 の重みは A_1+B_1+A_2=1+1+2=4、パス 1 \to 3 \to 2 の重みは A_1+B_2+A_3+B_3+A_2=1+6+3+2+2=14 であり、最小の重みは 4 です。

頂点 1 から頂点 3 へのパスを考えます。 パス 1 \to 3 の重みは A_1+B_2+A_3=1+6+3=10、パス 1 \to 2 \to 3 の重みは A_1+B_1+A_2+B_3+A_3=1+1+2+2+3=9 であり、最小の重みは 9 です。


入力例 2

2 1
0 1
1 2 3

出力例 2

4

入力例 3

5 8
928448202 994752369 906965437 942744902 907560126
2 5 975090662
1 2 908843627
1 5 969061140
3 4 964249326
2 3 957690728
2 4 942986477
4 5 948404113
1 3 988716403

出力例 3

2832044198 2824130042 4696218483 2805069468

答えが 32-bit 整数に収まらないことがあることに注意してください。

Score : 425 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges. Each vertex i\,(1\leq i \leq N) has a weight A_i. Each edge j\,(1\leq j \leq M) connects vertices U_j and V_j bidirectionally and has a weight B_j.

The weight of a path in this graph is defined as the sum of the weights of the vertices and edges that appear on the path.

For each i=2,3,\dots,N, solve the following problem:

  • Find the minimum weight of a path from vertex 1 to vertex i.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq U_j < V_j \leq N
  • (U_i, V_i) \neq (U_j, V_j) if i \neq j.
  • The graph is connected.
  • 0 \leq A_i \leq 10^9
  • 0 \leq B_j \leq 10^9
  • All input values are integers.

Input

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

N M
A_1 A_2 \dots A_N
U_1 V_1 B_1
U_2 V_2 B_2
\vdots
U_M V_M B_M

Output

Print the answers for i=2,3,\dots,N in a single line, separated by spaces.


Sample Input 1

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

Sample Output 1

4 9

Consider the paths from vertex 1 to vertex 2. The weight of the path 1 \to 2 is A_1 + B_1 + A_2 = 1 + 1 + 2 = 4, and the weight of the path 1 \to 3 \to 2 is A_1 + B_2 + A_3 + B_3 + A_2 = 1 + 6 + 3 + 2 + 2 = 14. The minimum weight is 4.

Consider the paths from vertex 1 to vertex 3. The weight of the path 1 \to 3 is A_1 + B_2 + A_3 = 1 + 6 + 3 = 10, and the weight of the path 1 \to 2 \to 3 is A_1 + B_1 + A_2 + B_3 + A_3 = 1 + 1 + 2 + 2 + 3 = 9. The minimum weight is 9.


Sample Input 2

2 1
0 1
1 2 3

Sample Output 2

4

Sample Input 3

5 8
928448202 994752369 906965437 942744902 907560126
2 5 975090662
1 2 908843627
1 5 969061140
3 4 964249326
2 3 957690728
2 4 942986477
4 5 948404113
1 3 988716403

Sample Output 3

2832044198 2824130042 4696218483 2805069468

Note that the answers may not fit in a 32-bit integer.

H - Laser Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

二次元平面上に N 体のモンスターがいます。 モンスターには 1 から N までの番号が付けられており、モンスター i がいる場所の座標は (X_i,Y_i) です。 ここで、(X_i,Y_i) \neq (0,0) です。 (なお、各モンスターは静止した点としてみなせるものとします。すなわち、モンスターは大きさを持ちません。)

この平面上の原点には高橋君が立っています。 高橋君の目からは強力なレーザーが常に照射されており、高橋君が向いている方向に存在するモンスターは即座に消滅します。 高橋君が向いている方向に複数のモンスターが存在する場合も、その全てが即座に消滅します。

青木君は、Q 個の 独立な 思考実験を行なっています。 j 個目の思考実験は以下のようなものです。

  • はじめ、高橋君はモンスター A_j がいる方向を向いている。今から高橋君は 時計回り に回転を行い、モンスター B_j がいる方向を向いた瞬間に停止する。 このとき、(モンスター A_j,B_j を含め)合計で何体のモンスターが消滅するか?なお、モンスター A_j,B_j が原点から見て同じ方向に存在する場合、高橋君は一切回転しない。

各思考実験に対する答えを求めてください。

制約

  • 2\leq N \leq 2\times 10^5
  • 1\leq Q \leq 2\times 10^5
  • -10^9\leq X_i,Y_i \leq 10^9
  • (X_i,Y_i)\neq (0,0)
  • 1\leq A_j,B_j\leq N
  • A_j\neq B_j
  • 入力は全て整数

入力

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

N Q
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
A_1 B_1
A_2 B_2
\vdots
A_Q B_Q

出力

Q 行出力せよ。 j 行目 (1\leq j \leq Q) には、j 個目の思考実験に対する答えを出力せよ。


入力例 1

5 4
0 1
1 -2
1 0
-2 0
3 0
4 1
1 4
5 4
3 5

出力例 1

2
5
4
2

  • 1 個目の思考実験:はじめ、高橋君はモンスター 4 がいる方向を向いています(このとき、モンスター 4 が消滅します)。 ここから時計回りに回転を続け、モンスター 1 がいる方向を向いた瞬間に停止します(このとき、モンスター 1 が消滅します)。 これら以外のモンスターがいる方向を向くことはないため、答えは 2 です。
  • 2 個目の思考実験:はじめ、高橋君はモンスター 1 がいる方向を向いています(このとき、モンスター 1 が消滅します)。 ここから時計回りに回転を続けると、途中モンスター 3,5 がいる方向を向くため、これらが消滅します。 さらに回転を続けると、途中モンスター 2 がいる方向を向くため、これが消滅します。 最終的にモンスター 4 がいる方向を向いた瞬間に停止します(このとき、モンスター 4 が消滅します)。 よって、答えは 5 です。
  • 3 個目の思考実験:モンスター 3,5,2,4 が消滅するため、答えは 4 です。
  • 4 個目の思考実験:モンスター 3,5 が消滅するため、答えは 2 です。 なお、モンスター 3,5 は原点から見て同じ方向に存在するため、高橋君は一切回転しないことに注意してください。

入力例 2

2 1
1 2
1 2
1 2

出力例 2

2

同じ座標に複数のモンスターが存在することもあります。


入力例 3

8 10
-84 -60
-100 8
77 55
-14 -10
50 -4
-63 -45
26 -17
-7 -5
3 7
2 4
8 4
8 4
7 1
1 7
6 3
4 7
4 5
2 6

出力例 3

3
8
4
4
5
8
6
8
7
8

Score : 450 points

Problem Statement

There are N monsters on a two-dimensional plane. The monsters are numbered from 1 to N, and the coordinates of monster i are (X_i,Y_i). Here, (X_i,Y_i) \neq (0,0). (Each monster can be regarded as a stationary point. That is, monsters have no size.)

Takahashi is standing at the origin of this plane. A powerful laser is always emitted from his eyes, instantly destroying monsters in the direction he is facing. If multiple monsters exist in the direction he is facing, all of them are instantly destroyed.

Aoki is conducting Q independent thought experiments. The j-th thought experiment is as follows:

  • Initially, Takahashi is facing the direction toward monster A_j. From now on, Takahashi will rotate clockwise and stop the moment he faces the direction toward monster B_j. Here, how many monsters in total (including monsters A_j and B_j) will be destroyed? If monsters A_j and B_j exist in the same direction from the origin, Takahashi does not rotate at all.

Find the answer to each thought experiment.

Constraints

  • 2\leq N \leq 2\times 10^5
  • 1\leq Q \leq 2\times 10^5
  • -10^9\leq X_i,Y_i \leq 10^9
  • (X_i,Y_i)\neq (0,0)
  • 1\leq A_j,B_j\leq N
  • A_j\neq B_j
  • All input values are integers.

Input

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

N Q
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
A_1 B_1
A_2 B_2
\vdots
A_Q B_Q

Output

Output Q lines. The j-th line (1\leq j \leq Q) should contain the answer to the j-th thought experiment.


Sample Input 1

5 4
0 1
1 -2
1 0
-2 0
3 0
4 1
1 4
5 4
3 5

Sample Output 1

2
5
4
2

  • 1-st thought experiment: Initially, Takahashi is facing the direction toward monster 4 (at this time, monster 4 is destroyed). From here, he continues to rotate clockwise and stops the moment he faces the direction toward monster 1 (at this time, monster 1 is destroyed). Since he does not face the direction toward any other monsters, the answer is 2.
  • 2-nd thought experiment: Initially, Takahashi is facing the direction toward monster 1 (at this time, monster 1 is destroyed). From here, as he continues to rotate clockwise, he faces the direction toward monsters 3 and 5 along the way, so they are destroyed. As he continues to rotate further, he faces the direction toward monster 2 along the way, so it is destroyed. Finally, he stops the moment he faces the direction toward monster 4 (at this time, monster 4 is destroyed). Therefore, the answer is 5.
  • 3-rd thought experiment: Monsters 3, 5, 2, 4 are destroyed, so the answer is 4.
  • 4-th thought experiment: Monsters 3, 5 are destroyed, so the answer is 2. Note that since monsters 3 and 5 exist in the same direction from the origin, Takahashi does not rotate at all.

Sample Input 2

2 1
1 2
1 2
1 2

Sample Output 2

2

Multiple monsters may exist at the same coordinates.


Sample Input 3

8 10
-84 -60
-100 8
77 55
-14 -10
50 -4
-63 -45
26 -17
-7 -5
3 7
2 4
8 4
8 4
7 1
1 7
6 3
4 7
4 5
2 6

Sample Output 3

3
8
4
4
5
8
6
8
7
8
I - Connecting Points

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

2 次元平面上に N 頂点 0 辺のグラフ G があります。頂点には 1 から N までの番号が付いており、頂点 i は座標 (x_i,y_i) にあります。

G の頂点 u,v に対して、u,v の距離 d(u,v) をマンハッタン距離 d(u,v)=|x_u-x_v|+|y_u-y_v| で定義します。

また、G2 つの連結成分 A,B に対して、A,B の頂点集合をそれぞれ V(A),V(B) としたとき、A,B の距離 d(A,B)d(A,B)=\min\lbrace d(u,v)\mid u \in V(A), v\in V(B)\rbrace で定義します。

以下で説明されるクエリを Q 個処理してください。クエリは次の 3 種類のいずれかです。

  • 1 a b : G の頂点数を n としたとき、頂点 n+1 の座標を (x_{n+1},y_{n+1})=(a,b) として、G に頂点 n+1 を追加する。
  • 2 : G の頂点数を n、連結成分数を m とする。
    • m=1 のとき、-1 を出力する。
    • m\geq 2 のとき、距離が最小である連結成分をすべてマージし、その最小の距離の値を出力する。厳密には、G の連結成分を A_1,A_2,\ldots,A_m として、\displaystyle k=\min_{1\leq i\lt j\leq m} d(A_i,A_j) とする。同じ連結成分にない頂点の組 (u,v)\ (1\leq u\lt v\leq n) であって d(u,v)=k を満たすようなものすべてについて、頂点 uv の間に辺を張る。その後、k を出力する。
  • 3 u v : 頂点 u,v が同じ連結成分にあるならば Yes を、そうでないならば No を出力する。

制約

  • 2\leq N\leq 1500
  • 1\leq Q\leq 1500
  • 0\leq x_i,y_i\leq 10^9
  • 1 種類目のクエリについて、0\leq a,b\leq 10^9
  • 3 種類目のクエリについて、そのクエリを処理する直前の G の頂点数を n としたとき、1\leq u\lt v\leq n
  • 入力は全て整数

入力

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

N Q
x_1 y_1
x_2 y_2
\vdots
x_N y_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

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

1 a b
2
3 u v

出力

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


入力例 1

4 11
3 4
3 3
7 3
2 2
3 1 2
2
3 1 2
1 6 4
2
3 2 5
2
3 2 5
2
1 2 2
2

出力例 1

No
1
Yes
2
No
3
Yes
-1
0

はじめ、頂点 1,2,3,4 はそれぞれ座標 (3,4),(3,3),(7,3),(2,2) にあります。

  • 1 番目のクエリについて、頂点 12 は連結でないため、No を出力します。
  • 2 番目のクエリについて、連結成分は 4 つあり、各連結成分に含まれる頂点集合は \lbrace 1\rbrace, \lbrace 2\rbrace, \lbrace 3\rbrace, \lbrace 4\rbrace です。異なる連結成分間の距離の最小値は 1 であり、頂点 12 の間に辺が張られます。1 を出力します。
  • 3 番目のクエリについて、頂点 12 は連結であるため、Yes を出力します。
  • 4 番目のクエリについて、座標 (6,4) に頂点 5 を追加します。
  • 5 番目のクエリについて、連結成分は 4 つあり、各連結成分に含まれる頂点集合は \lbrace 1,2\rbrace,\lbrace 3\rbrace,\lbrace 4\rbrace,\lbrace 5\rbrace です。異なる連結成分間の距離の最小値は 2 であり、頂点 24 の間および頂点 35 の間に辺が張られます。2 を出力します。
  • 6 番目のクエリについて、頂点 25 は連結でないため、No を出力します。
  • 7 番目のクエリについて、連結成分は 2 つあり、各連結成分に含まれる頂点集合は \lbrace 1,2,4\rbrace,\lbrace 3,5\rbrace です。異なる連結成分間の距離の最小値は 3 であり、頂点 15 の間に辺が張られます。3 を出力します。
  • 8 番目のクエリについて、頂点 25 は連結であるため、Yes を出力します。
  • 9 番目のクエリについて、連結成分は 1 つであるため -1 を出力します。
  • 10 番目のクエリについて、座標 (2,2) に頂点 6 を追加します。
  • 11 番目のクエリについて、連結成分は 2 つあり、各連結成分に含まれる頂点集合は \lbrace 1,2,3,4,5\rbrace,\lbrace 6\rbrace です。異なる連結成分間の距離の最小値は 0 であり、頂点 46 の間に辺が張られます。0 を出力します。

Score : 500 points

Problem Statement

There is a graph G with N vertices and 0 edges on a 2-dimensional plane. The vertices are numbered from 1 to N, and vertex i is located at coordinates (x_i,y_i).

For vertices u and v of G, the distance d(u,v) between u and v is defined as the Manhattan distance d(u,v)=|x_u-x_v|+|y_u-y_v|.

Also, for two connected components A and B of G, let V(A) and V(B) be the vertex sets of A and B, respectively. The distance d(A,B) between A and B is defined as d(A,B)=\min\lbrace d(u,v)\mid u \in V(A), v\in V(B)\rbrace.

Process Q queries as described below. Each query is one of the following three types:

  • 1 a b : Let n be the number of vertices in G. Add vertex n+1 to G with coordinates (x_{n+1},y_{n+1})=(a,b).
  • 2 : Let n be the number of vertices in G and m be the number of connected components.
    • If m=1, output -1.
    • If m\geq 2, merge all connected components with the minimum distance and output the value of that minimum distance. Formally, let the connected components of G be A_1,A_2,\ldots,A_m and let \displaystyle k=\min_{1\leq i\lt j\leq m} d(A_i,A_j). For all pairs of vertices (u,v)\ (1\leq u\lt v\leq n) that are not in the same connected component and satisfy d(u,v)=k, add an edge between vertices u and v. Then, output k.
  • 3 u v : If vertices u and v are in the same connected component, output Yes; otherwise, output No.

Constraints

  • 2\leq N\leq 1500
  • 1\leq Q\leq 1500
  • 0\leq x_i,y_i\leq 10^9
  • For queries of type 1, 0\leq a,b\leq 10^9.
  • For queries of type 3, let n be the number of vertices in G just before processing that query, then 1\leq u\lt v\leq n.
  • All input values are integers.

Input

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

N Q
x_1 y_1
x_2 y_2
\vdots
x_N y_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following three formats:

1 a b
2
3 u v

Output

Output the answers to the queries separated by newlines, following the instructions in the problem statement.


Sample Input 1

4 11
3 4
3 3
7 3
2 2
3 1 2
2
3 1 2
1 6 4
2
3 2 5
2
3 2 5
2
1 2 2
2

Sample Output 1

No
1
Yes
2
No
3
Yes
-1
0

Initially, vertices 1,2,3,4 are located at coordinates (3,4),(3,3),(7,3),(2,2), respectively.

  • For the 1st query, vertices 1 and 2 are not connected, so output No.
  • For the 2nd query, there are 4 connected components, and the vertex set of each connected component is \lbrace 1\rbrace, \lbrace 2\rbrace, \lbrace 3\rbrace, \lbrace 4\rbrace. The minimum distance between different connected components is 1, and an edge is added between vertices 1 and 2. Output 1.
  • For the 3rd query, vertices 1 and 2 are connected, so output Yes.
  • For the 4th query, add vertex 5 at coordinates (6,4).
  • For the 5th query, there are 4 connected components, and the vertex set of each connected component is \lbrace 1,2\rbrace,\lbrace 3\rbrace,\lbrace 4\rbrace,\lbrace 5\rbrace. The minimum distance between different connected components is 2, and edges are added between vertices 2 and 4 and between vertices 3 and 5. Output 2.
  • For the 6th query, vertices 2 and 5 are not connected, so output No.
  • For the 7th query, there are 2 connected components, and the vertex set of each connected component is \lbrace 1,2,4\rbrace,\lbrace 3,5\rbrace. The minimum distance between different connected components is 3, and an edge is added between vertices 1 and 5. Output 3.
  • For the 8th query, vertices 2 and 5 are connected, so output Yes.
  • For the 9th query, there is 1 connected component, so output -1.
  • For the 10th query, add vertex 6 at coordinates (2,2).
  • For the 11th query, there are 2 connected components, and the vertex set of each connected component is \lbrace 1,2,3,4,5\rbrace,\lbrace 6\rbrace. The minimum distance between different connected components is 0, and an edge is added between vertices 4 and 6. Output 0.