A - First Player

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

配点 : 100 点

問題文

人 1 、人 2 、\ldots 、人 N と番号付けられた N 人が、この順番で時計回りに円卓に座っています。 特に、時計回りで人 N の次の位置には人 1 が座っています。

i = 1, 2, \ldots, N について、人 i の名前は S_i 、年齢は A_i です。 ここで、異なる 2 人が同じ名前や同じ年齢であることはありません。

年齢が最も小さい人を起点として、座っている位置の時計回りの順番で、N 人全員の名前を出力してください。

制約

  • 2 \leq N \leq 100
  • N は整数
  • S_i は英小文字のみからなる長さ 1 以上 10 以下の文字列
  • i \neq j \implies S_i \neq S_j
  • 0 \leq A_i \leq 10^9
  • A_i は整数
  • i \neq j \implies A_i \neq A_j

入力

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

N
S_1 A_1
S_2 A_2
\vdots
S_N A_N

出力

N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には、年齢が最も小さい人を起点として時計回りで i 番目の位置に座っている人の名前を出力せよ。


入力例 1

5
alice 31
bob 41
carol 5
dave 92
ellen 65

出力例 1

carol
dave
ellen
alice
bob

年齢が最も小さい人は人 3 です。よって、人 3 を起点として座っている位置の時計回りの順番、すなわち、人 3 、人 4 、人 5 、人 1 、人 2 の順に名前を出力します。


入力例 2

2
takahashi 1000000000
aoki 999999999

出力例 2

aoki
takahashi

Score : 100 points

Problem Statement

There are N people numbered 1, 2, \ldots, N, sitting in this clockwise order around a round table. In particular, person 1 is sitting next to person N in the clockwise direction.

For each i = 1, 2, \ldots, N, person i has a name S_i and an age A_i. Here, no two people have the same name or the same age.

Starting from the youngest person, print the names of all N people in the order of their seating positions in clockwise order.

Constraints

  • 2 \leq N \leq 100
  • N is an integer.
  • S_i is a string of length between 1 and 10, consisting of lowercase English letters.
  • i \neq j \implies S_i \neq S_j
  • 0 \leq A_i \leq 10^9
  • A_i is an integer.
  • i \neq j \implies A_i \neq A_j

Input

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

N
S_1 A_1
S_2 A_2
\vdots
S_N A_N

Output

Print N lines. For each i = 1, 2, \ldots, N, the i-th line should contain the name of the person sitting in the i-th position clockwise from the youngest person.


Sample Input 1

5
alice 31
bob 41
carol 5
dave 92
ellen 65

Sample Output 1

carol
dave
ellen
alice
bob

The youngest person is person 3. Therefore, starting from person 3, print the names in the clockwise order of their seating positions: person 3, person 4, person 5, person 1, and person 2.


Sample Input 2

2
takahashi 1000000000
aoki 999999999

Sample Output 2

aoki
takahashi
B - Triangular Number

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

配点 : 100 点

問題文

正整数 N が与えられます。
1 以上 N 以下の整数をすべて足し合わせた値 1+2+\cdots+N を出力してください。

制約

  • 1 \leq N \leq 100
  • N は整数

入力

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

N

出力

1 以上 N 以下の整数をすべて足し合わせた値を出力せよ。


入力例 1

5

出力例 1

15

1+2+3+4+5=15 のため、15 を出力します。


入力例 2

1

出力例 2

1

入力例 3

29

出力例 3

435

Score : 100 points

Problem Statement

You are given a positive integer N.
Output the sum of all integers from 1 to N, that is, 1+2+\cdots+N.

Constraints

  • 1 \leq N \leq 100
  • N is an integer.

Input

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

N

Output

Output the sum of all integers from 1 to N.


Sample Input 1

5

Sample Output 1

15

Since 1+2+3+4+5=15, output 15.


Sample Input 2

1

Sample Output 2

1

Sample Input 3

29

Sample Output 3

435
C - Trimmed Mean

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

配点 : 200 点

問題文

高橋君は体操の大会に参加しています。
大会では、5N 人の審査員それぞれが高橋君の演技に対して評点をつけ、それらを元に次のように高橋君の得点が決定されます。

  • 高い評点をつけた方から順に N 人の審査員による評点を無効にする。
  • 低い評点をつけた方から順に N 人の審査員による評点を無効にする。
  • 残りの 3N 人の審査員による評点の平均点を高橋君の得点とする。

より厳密には、審査員がつけた得点の多重集合 S (|S|=5N) に対して次の操作を行って得られたものが高橋君の得点となります。

  • 「S の最大の要素(複数ある場合はそのうちの 1 つ)を選び、S から取り除く」という操作を N 回繰り返す。
  • 「S の最小の要素(複数ある場合はそのうちの 1 つ)を選び、S から取り除く」という操作を N 回繰り返す。
  • S に残った 3N 個の要素の平均を高橋君の得点とする。

高橋君の演技に対する、i 人目 (1\leq i\leq 5N) の審査員の評点は X_i 点でした。 高橋君の得点を計算してください。

制約

  • 1\leq N\leq 100
  • 0\leq X_i\leq 100
  • 入力はすべて整数

入力

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

N
X_1 X_2 \ldots X_{5N}

出力

高橋君の得点を出力せよ。
なお、真の値との絶対誤差または相対誤差が 10^{-5} 以下であれば正解として扱われる。


入力例 1

1
10 100 20 50 30

出力例 1

33.333333333333336

N=1 であるので、評点が高い方と低い方からそれぞれ 1 人ずつの審査員による評点を無効にします。
1 番高い評点をつけた審査員は 2 人目 (100 点) であるため、これを無効にします。
また、1 番低い評点をつけた審査員は 1 人目 (10 点) であるため、これも無効にします。
よって、最終的な平均点は \displaystyle\frac{20+50+30}{3}=33.333\cdots となります。

出力は、真の値との絶対誤差または相対誤差が 10^{-5} 以下であれば正解として扱われる事に注意してください。


入力例 2

2
3 3 3 4 5 6 7 8 99 100

出力例 2

5.500000000000000

N=2 であるので、評点が高い方と低い方からそれぞれ 2 人ずつの審査員による評点を無効にします。
1,2 番目に高い評点をつけた審査員は順に 10 人目 (100 点), 9 人目 (99 点) であるため、これを無効にします。
1 番低い評点をつけた審査員は 1,2,3 人目 (3 点) の 3 人がいるため、このうち 2 人を無効とします。
よって、答えは \displaystyle\frac{3+4+5+6+7+8}{6}=5.5 となります。

1 番低い評点をつけた 3 人のうちどの 2 人を無効にしたかは、答えに影響しない事に注意してください。

Score : 200 points

Problem Statement

Takahashi is participating in a gymnastic competition.
In the competition, each of 5N judges grades Takahashi's performance, and his score is determined as follows:

  • Invalidate the grades given by the N judges who gave the highest grades.
  • Invalidate the grades given by the N judges who gave the lowest grades.
  • Takahashi's score is defined as the average of the remaining 3N judges' grades.

More formally, Takahashi's score is obtained by performing the following procedure on the multiset of the judges' grades S (|S|=5N):

  • Repeat the following operation N times: choose the maximum element (if there are multiple such elements, choose one of them) and remove it from S.
  • Repeat the following operation N times: choose the minimum element (if there are multiple such elements, choose one of them) and remove it from S.
  • Takahashi's score is defined as the average of the 3N elements remaining in S.

The i-th (1\leq i\leq 5N) judge's grade for Takahashi's performance was X_i points. Find Takahashi's score.

Constraints

  • 1\leq N\leq 100
  • 0\leq X_i\leq 100
  • All values in the input are integers.

Input

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

N
X_1 X_2 \ldots X_{5N}

Output

Print Takahashi's score.
Your answer will be considered correct if the absolute or relative error from the true value is at most 10^{-5}.


Sample Input 1

1
10 100 20 50 30

Sample Output 1

33.333333333333336

Since N=1, the grade given by one judge who gave the highest grade, and one with the lowest, are invalidated.
The 2-nd judge gave the highest grade (100 points), which is invalidated.
Additionally, the 1-st judge gave the lowest grade (10 points), which is also invalidated.
Thus, the average is \displaystyle\frac{20+50+30}{3}=33.333\cdots.

Note that the output will be considered correct if the absolute or relative error from the true value is at most 10^{-5}.


Sample Input 2

2
3 3 3 4 5 6 7 8 99 100

Sample Output 2

5.500000000000000

Since N=2, the grades given by the two judges who gave the highest grades, and two with the lowest, are invalidated.
The 10-th and 9-th judges gave the highest grades (100 and 99 points, respectively), which are invalidated.
Three judges, the 1-st, 2-nd, and 3-rd, gave the lowest grade (3 points), so two of them are invalidated.
Thus, the average is \displaystyle\frac{3+4+5+6+7+8}{6}=5.5.

Note that the choice of the two invalidated judges from the three with the lowest grades does not affect the answer.

D - Get Min

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

配点 : 200 点

問題文

中に何も入っていない袋があります。

Q 個のクエリが与えられるので、これらのクエリを順に処理したときのタイプ 2 の各クエリの答えを出力してください。

各クエリは、以下のいずれかの形式です。

  • タイプ 1 : 1 x の形式で入力として与えられる。整数 x が書かれたボールを 1 つ袋の中に入れる。

  • タイプ 2 : 2 の形式で入力として与えられる。袋に入っているボールのうち書かれている整数が最小のものを 1 つ取り出し、その整数を答えとする。ただし、袋の中にボールが入っていないときにはこのクエリは与えられない。

制約

  • 2 \leq Q \leq 100
  • タイプ 1 のクエリにおいて、1 \leq x \leq 100
  • タイプ 2 のクエリが与えられたとき、袋は空ではない
  • タイプ 2 のクエリが 1 つ以上与えられる
  • 入力される値はすべて整数

入力

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

Q 
\text{query}_1
\text{query}_2
\ldots
\text{query}_Q

ただし、\text{query}_i は i 番目のクエリであり、以下のいずれかの形式で与えられる。

1 x
2

出力

タイプ 2 のクエリの個数を q として、q 行出力せよ。

i 行目には、i 番目のタイプ 2 のクエリの答えを出力せよ。


入力例 1

5
1 6
1 7
2
1 1
2

出力例 1

6
1

はじめ、袋の中にボールは入っていません。

1 番目のクエリでは、袋の中に 6 が書かれたボールを入れます。

2 番目のクエリでは、袋の中に 7 が書かれたボールを入れます。

3 番目のクエリでは、袋の中には 6 が書かれたボールと 7 が書かれたボールが入っているため、6 が書かれたボールを取り出します。このクエリの答えは 6 となります。

4 番目のクエリでは、袋の中に 1 が書かれたボールを入れます。

5 番目のクエリでは、袋の中には 1 が書かれたボールと 7 が書かれたボールが入っているため、1 が書かれたボールを取り出します。このクエリの答えは 1 となります。


入力例 2

8
1 5
1 1
1 1
1 9
2
2
1 2
2

出力例 2

1
1
2

同じ整数が書かれたボールを複数個袋の中に入れることもあります。

Score : 200 points

Problem Statement

There is an empty bag.

You are given Q queries. Process these queries in order and output the answer to each type-2 query.

Each query is of one of the following types.

  • Type 1: Given as input in the format 1 x. Put a ball with the integer x written on it into the bag.

  • Type 2: Given as input in the format 2. Pick out one ball with the minimum integer written on it from the balls in the bag, and report that integer as the answer. This query is not given when the bag contains no balls.

Constraints

  • 2 \leq Q \leq 100
  • In a type-1 query, 1 \leq x \leq 100.
  • When a type-2 query is given, the bag is not empty.
  • At least one type-2 query is given.
  • All input values are integers.

Input

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

Q 
\text{query}_1
\text{query}_2
\ldots
\text{query}_Q

Here, \text{query}_i is the i-th query and is given in one of the following formats:

1 x
2

Output

Let q be the number of type-2 queries, and output q lines.

The i-th line should contain the answer to the i-th type-2 query.


Sample Input 1

5
1 6
1 7
2
1 1
2

Sample Output 1

6
1

Initially, the bag contains no balls.

The 1st query puts a ball with 6 written on it into the bag.

The 2nd query puts a ball with 7 written on it into the bag.

In the 3rd query, the bag contains a ball with 6 written on it and a ball with 7 written on it, so you pick out the ball with 6 written on it. The answer to this query is 6.

The 4th query puts a ball with 1 written on it into the bag.

In the 5th query, the bag contains a ball with 1 written on it and a ball with 7 written on it, so you pick out the ball with 1 written on it. The answer to this query is 1.


Sample Input 2

8
1 5
1 1
1 1
1 9
2
2
1 2
2

Sample Output 2

1
1
2

The bag may contain multiple balls with the same integer.

E - Cards Query Problem

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

配点 : 300 点

問題文

1 から N までの番号がついた N 個の空の箱と、何も書かれていない無数のカードがあります。
Q 個のクエリが与えられるので、順番に処理してください。クエリは次の 3 種類のいずれかです。

  • 1 i j \colon カードを 1 枚選んで数 i を書き込み、そのカードを箱 j に入れる
  • 2 i \colon 箱 i に入っているカードに書かれた数を昇順ですべて答える
  • 3 i \colon 数 i が書かれたカードが入っている箱の番号を昇順ですべて答える

ただし、以下の点に注意してください。

  • 2 番目の形式のクエリにおいては、箱 i の中に同じ数が書かれたカードが複数枚あるときは、入っている枚数と同じ回数だけその数を出力する
  • 3 番目の形式のクエリにおいては、数 i が書かれたカードが同じ箱に複数枚入っている場合でも、その箱の番号は 1 度だけ出力する

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 番目の形式のクエリについて、
    • 1 \leq i \leq 2 \times 10^5
    • 1 \leq j \leq N
  • 2 番目の形式のクエリについて、
    • 1 \leq i \leq N
    • このクエリが与えられる時点で箱 i にカードが入っている
  • 3 番目の形式のクエリについて、
    • 1 \leq i \leq 2 \times 10^5
    • このクエリが与えられる時点で数 i が書かれたカードが入っている箱が存在する
  • 出力するべき数は合計で 2 \times 10^5 個以下
  • 入力はすべて整数

入力

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

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

ただし、\mathrm{query}_q は q 個目のクエリを表しており、次のいずれかの形式で与えられる。

1 i j
2 i
3 i

出力

2 番目の形式のクエリと 3 番目の形式のクエリに対し、順番に答えを出力せよ。
各クエリでは出力するべき要素を昇順に空白区切りで出力し、クエリごとに改行せよ。


入力例 1

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

出力例 1

1 2
1 1 2
1 4
4

クエリを順に処理していきます。

  • カードに 1 を書き込み、箱 1 に入れます。
  • カードに 2 を書き込み、箱 4 に入れます。
  • カードに 1 を書き込み、箱 4 に入れます。
  • 箱 4 に入っているカードに書かれた数は 1, 2 です。
    • 1, 2 の順に出力してください。
  • カードに 1 を書き込み、箱 4 に入れます。
  • 箱 4 に入っているカードに書かれた数は 1, 1, 2 です。
    • 1 を 2 度出力することに注意してください。
  • 数 1 が書かれたカードが入っている箱は箱 1, 4 です。
    • 箱 4 には数 1 が書かれたカードが 2 枚入っていますが、4 は 1 回しか出力しないことに注意してください。
  • 数 2 が書かれたカードが入っている箱は箱 4 です。

入力例 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

出力例 2

1 2 200000
1

Score : 300 points

Problem Statement

We have N boxes numbered 1 to N that are initially empty, and an unlimited number of blank cards.
Process Q queries in order. There are three kinds of queries as follows.

  • 1 i j \colon Write the number i on a blank card and put it into box j.
  • 2 i \colon Report all numbers written on the cards in box i, in ascending order.
  • 3 i \colon Report all box numbers of the boxes that contain a card with the number i, in ascending order.

Here, note the following.

  • In a query of the second kind, if box i contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards.
  • In a query of the third kind, even if a box contains multiple cards with the number i, the box number of that box should be printed only once.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • For a query of the first kind:
    • 1 \leq i \leq 2 \times 10^5
    • 1 \leq j \leq N
  • For a query of the second kind:
    • 1 \leq i \leq N
    • Box i contains some cards when this query is given.
  • For a query of the third kind:
    • 1 \leq i \leq 2 \times 10^5
    • Some boxes contain a card with the number i when this query is given.
  • At most 2 \times 10^5 numbers are to be reported.
  • All values in the input are integers.

Input

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

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

Here, \mathrm{query}_q denotes the q-th query, which is in one of the following formats:

1 i j
2 i
3 i

Output

Respond to the queries of the second and third kinds in order.
For each of those queries, print one line containing the elements to be reported in ascending order, with spaces in between.


Sample Input 1

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

Sample Output 1

1 2
1 1 2
1 4
4

Let us process the queries in order.

  • Write 1 on a card and put it into box 1.
  • Write 2 on a card and put it into box 4.
  • Write 1 on a card and put it into box 4.
  • Box 4 contains cards with the numbers 1 and 2.
    • Print 1 and 2 in this order.
  • Write 1 on a card and put it into box 4.
  • Box 4 contains cards with the numbers 1, 1, and 2.
    • Note that you should print 1 twice.
  • Boxes 1 and 4 contain a card with the number 1.
    • Note that you should print 4 only once, even though box 4 contains two cards with the number 1.
  • Boxes 4 contains a card with the number 2.

Sample Input 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

Sample Output 2

1 2 200000
1
F - Product

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

配点 : 300 点

問題文

N 個の袋があります。
袋 i には L_i 個のボールが入っていて、袋 i の j(1\leq j\leq L_i) 番目のボールには正の整数 a_{i,j} が書かれています。

それぞれの袋から 1 つずつボールを取り出します。
取り出したボールに書かれた数の総積が X になるような取り出し方は何通りありますか?

ただし、書かれた数が同じであっても全てのボールは区別します。

制約

  • N \geq 2
  • L_i \geq 2
  • 袋に入っているボールの個数の総積は 10^5 を超えない。すなわち、\displaystyle\prod_{i=1}^{N}L_i \leq 10^5
  • 1 \leq a_{i,j} \leq 10^9
  • 1 \leq X \leq 10^{18}
  • 入力に含まれる値は全て整数である。

入力

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

N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}

出力

答えを出力せよ。


入力例 1

2 40
3 1 8 4
2 10 5

出力例 1

2

袋 1 の 3 番目のボールと袋 2 の 1 番目のボールを選ぶと、a_{1,3} \times a_{2,1} = 4 \times 10 = 40 となります。
袋 1 の 2 番目のボールと袋 2 の 2 番目のボールを選ぶと、a_{1,2} \times a_{2,2} = 8 \times 5 = 40 となります。
これ以外に総積が 40 になる取り出し方は存在しないので、答えは 2 です。


入力例 2

3 200
3 10 10 10
3 10 10 10
5 2 2 2 2 2

出力例 2

45

書かれた数が同じであっても全てのボールは区別することに注意してください。


入力例 3

3 1000000000000000000
2 1000000000 1000000000
2 1000000000 1000000000
2 1000000000 1000000000

出力例 3

0

総積が X になる取り出し方が 1 つも存在しないこともあります。

Score : 300 points

Problem Statement

We have N bags.
Bag i contains L_i balls. The j-th ball (1\leq j\leq L_i) in Bag i has a positive integer a_{i,j} written on it.

We will pick out one ball from each bag.
How many ways are there to pick the balls so that the product of the numbers written on the picked balls is X?

Here, we distinguish all balls, even with the same numbers written on them.

Constraints

  • N \geq 2
  • L_i \geq 2
  • The product of the numbers of balls in the bags is at most 10^5: \displaystyle\prod_{i=1}^{N}L_i \leq 10^5.
  • 1 \leq a_{i,j} \leq 10^9
  • 1 \leq X \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}

Output

Print the answer.


Sample Input 1

2 40
3 1 8 4
2 10 5

Sample Output 1

2

When choosing the 3-rd ball in Bag 1 and 1-st ball in Bag 2, we have a_{1,3} \times a_{2,1} = 4 \times 10 = 40.
When choosing the 2-nd ball in Bag 1 and 2-nd ball in Bag 2, we have a_{1,2} \times a_{2,2} = 8 \times 5 = 40.
There are no other ways to make the product 40, so the answer is 2.


Sample Input 2

3 200
3 10 10 10
3 10 10 10
5 2 2 2 2 2

Sample Output 2

45

Note that we distinguish all balls, even with the same numbers written on them.


Sample Input 3

3 1000000000000000000
2 1000000000 1000000000
2 1000000000 1000000000
2 1000000000 1000000000

Sample Output 3

0

There may be no way to make the product X.

G - Cross Explosion

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

配点 : 400 点

問題文

縦 H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i, j) と表します。
はじめ、全てのマスには壁が 1 個ずつ立てられています。
以下で説明されるクエリを Q 個与えられる順に処理した後に、残っている壁の個数を求めてください。

q 番目のクエリでは整数 R_q, C_q が与えられます。
あなたは (R_q, C_q) に爆弾を置いて壁を爆破します。その結果、以下の処理が行われます。

  • (R_q, C_q) に壁が存在する場合は、その壁を破壊して処理を終了する。
  • (R_q, C_q) に壁が存在しない場合は、(R_q, C_q) から上下左右に見て最初に現れる壁を破壊する。厳密に述べると、以下の 4 個の処理が同時に行われる。
    • (i, C_q) に壁が存在して (k, C_q) (i \lt k \lt R_q) に壁が存在しないような i \lt R_q が存在する時、(i, C_q) に存在する壁を破壊する。
    • (i, C_q) に壁が存在して (k, C_q) (R_q \lt k \lt i) に壁が存在しないような i \gt R_q が存在する時、(i, C_q) に存在する壁を破壊する。
    • (R_q, j) に壁が存在して (R_q, k) (j \lt k \lt C_q) に壁が存在しないような j \lt C_q が存在する時、(R_q, j) に存在する壁を破壊する。
    • (R_q, j) に壁が存在して (R_q, k) (C_q \lt k \lt j) に壁が存在しないような j \gt C_q が存在する時、(R_q, j) に存在する壁を破壊する。

制約

  • 1 \leq H, W
  • H \times W \leq 4 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq R_q \leq H
  • 1 \leq C_q \leq W
  • 入力される値は全て整数

入力

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

H W Q
R_1 C_1
R_2 C_2
\vdots
R_Q C_Q

出力

クエリを全て処理した後に、残っている壁の個数を出力せよ。


入力例 1

2 4 3
1 2
1 2
1 3

出力例 1

2

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

  • 1 番目のクエリでは (R_1, C_1) = (1, 2) である。 (1, 2) に壁は存在するので (1, 2) の壁が爆破される。
  • 2 番目のクエリでは (R_2, C_2) = (1, 2) である。 (1, 2) に壁は存在しないので、(1, 2) から上下左右に見て最初に現れる壁である (2,2),(1,1),(1,3) が爆破される。
  • 3 番目のクエリでは (R_2, C_2) = (1, 3) である。 (1, 3) に壁は存在しないので、(1, 3) から上下左右に見て最初に現れる壁である (2,3),(1,4) が爆破される。

全てのクエリを処理した後に残っている壁は (2, 1), (2, 4) の 2 個です。


入力例 2

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

出力例 2

10

入力例 3

4 3 10
2 2
4 1
1 1
4 2
2 1
3 1
1 3
1 2
4 3
4 2

出力例 3

2

Score : 400 points

Problem Statement

There is a grid with H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.
Initially, there is one wall in each cell.
After processing Q queries explained below in the order they are given, find the number of remaining walls.

In the q-th query, you are given two integers R_q and C_q.
You place a bomb at (R_q, C_q) to destroy walls. As a result, the following process occurs.

  • If there is a wall at (R_q, C_q), destroy that wall and end the process.
  • If there is no wall at (R_q, C_q), destroy the first walls that appear when looking up, down, left, and right from (R_q, C_q). More precisely, the following four processes occur simultaneously:
    • If there exists an i \lt R_q such that a wall exists at (i, C_q) and no wall exists at (k, C_q) for all i \lt k \lt R_q, destroy the wall at (i, C_q).
    • If there exists an i \gt R_q such that a wall exists at (i, C_q) and no wall exists at (k, C_q) for all R_q \lt k \lt i, destroy the wall at (i, C_q).
    • If there exists a j \lt C_q such that a wall exists at (R_q, j) and no wall exists at (R_q, k) for all j \lt k \lt C_q, destroy the wall at (R_q, j).
    • If there exists a j \gt C_q such that a wall exists at (R_q, j) and no wall exists at (R_q, k) for all C_q \lt k \lt j, destroy the wall at (R_q, j).

Constraints

  • 1 \leq H, W
  • H \times W \leq 4 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq R_q \leq H
  • 1 \leq C_q \leq W
  • All input values are integers.

Input

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

H W Q
R_1 C_1
R_2 C_2
\vdots
R_Q C_Q

Output

Print the number of remaining walls after processing all queries.


Sample Input 1

2 4 3
1 2
1 2
1 3

Sample Output 1

2

The process of handling the queries can be explained as follows:

  • In the 1st query, (R_1, C_1) = (1, 2). There is a wall at (1, 2), so the wall at (1, 2) is destroyed.
  • In the 2nd query, (R_2, C_2) = (1, 2). There is no wall at (1, 2), so the walls at (2,2),(1,1),(1,3), which are the first walls that appear when looking up, down, left, and right from (1, 2), are destroyed.
  • In the 3rd query, (R_3, C_3) = (1, 3). There is no wall at (1, 3), so the walls at (2,3),(1,4), which are the first walls that appear when looking up, down, left, and right from (1, 3), are destroyed.

After processing all queries, there are two remaining walls, at (2, 1) and (2, 4).


Sample Input 2

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

Sample Output 2

10

Sample Input 3

4 3 10
2 2
4 1
1 1
4 2
2 1
3 1
1 3
1 2
4 3
4 2

Sample Output 3

2
H - Heavy Buckets

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

配点 : 475 点

問題文

N 人の人がおり、N 個のバケツがあります。人とバケツにはそれぞれ 1, 2, \ldots, N の番号が付けられています。

人 i ははじめバケツ i のみを持っており、バケツ i には何も入っていません。

これから、以下の操作を 10^9 回行います。

  • i = 1, 2, \ldots, N について同時に、人 i が持っているバケツすべてに水を i ずつ入れ、それらのバケツを人 A_i に渡す。

ただし、バケツに入れることのできる水の量に制限はありません。

i = 1, 2, \ldots, Q について以下の形式のクエリに答えてください。

  • T_i 回目の操作の直後にバケツ B_i に入っている水の量を求めてください。

制約

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

入力

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

N Q
A_1 A_2 \ldots A_N
T_1 B_1
T_2 B_2
\vdots
T_Q B_Q

出力

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


入力例 1

5 6
3 4 2 2 5
4 3
6 5
1 4
10 1
10 2
1000000000 1

出力例 1

11
30
4
28
30
2999999998

1 番目のクエリに対する説明を行います。

はじめ、バケツ 3 は人 3 が持っています。

1 回目の操作の直後、バケツ 3 は人 2 が持っており、水が 3 入っています。

2 回目の操作の直後、バケツ 3 は人 4 が持っており、水が 5 入っています。

3 回目の操作の直後、バケツ 3 は人 2 が持っており、水が 9 入っています。

4 回目の操作の直後、バケツ 3 は人 4 が持っており、水が 11 入っています。

したがって、1 番目のクエリに対する答えは 11 となります。

Score : 475 points

Problem Statement

There are N people and N buckets. Both the people and buckets are numbered 1, 2, \ldots, N.

Initially, person i holds only bucket i, and bucket i is empty.

From now on, the following operation will be performed 10^9 times:

  • For i = 1, 2, \ldots, N simultaneously, person i adds i units of water to each of the buckets they are holding and passes those buckets to person A_i.

Here, there is no limit on the amount of water that can be put in a bucket.

For i = 1, 2, \ldots, Q, answer the following queries:

  • Find the amount of water in bucket B_i immediately after the T_i-th operation.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • 1 \leq T_i \leq 10^9
  • 1 \leq B_i \leq N
  • All input values are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
T_1 B_1
T_2 B_2
\vdots
T_Q B_Q

Output

Output Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

5 6
3 4 2 2 5
4 3
6 5
1 4
10 1
10 2
1000000000 1

Sample Output 1

11
30
4
28
30
2999999998

We will explain the first query.

Initially, bucket 3 is held by person 3.

Immediately after the 1-st operation, bucket 3 is held by person 2 and contains 3 units of water.

Immediately after the 2-nd operation, bucket 3 is held by person 4 and contains 5 units of water.

Immediately after the 3-rd operation, bucket 3 is held by person 2 and contains 9 units of water.

Immediately after the 4-th operation, bucket 3 is held by person 4 and contains 11 units of water.

Thus, the answer to the first query is 11.

I - Product Equality

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

配点 : 550 点

問題文

N 個の整数 A_1,A_2,\dots,A_N が与えられます。
以下の条件を満たす整数の組 (i,j,k) の個数を求めてください。

  • 1 \le i,j,k \le N
  • A_i \times A_j = A_k

制約

  • 1 \le N \le 1000
  • \color{red}{1 \le A_i < 10^{1000}}

入力

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

N
A_1
A_2
\vdots
A_N

出力

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


入力例 1

5
2
3
6
12
24

出力例 1

6

問題文中の条件を満たす (i,j,k) の組は以下の 6 通りです。

  • (1,2,3)
  • (1,3,4)
  • (1,4,5)
  • (2,1,3)
  • (3,1,4)
  • (4,1,5)

入力例 2

11
1
2
3
4
5
6
123456789123456789
123456789123456789
987654321987654321
987654321987654321
121932631356500531347203169112635269

出力例 2

40

各整数 A_i の値が非常に大きくなりうることに注意してください。


入力例 3

9
4
4
4
2
2
2
1
1
1

出力例 3

162

A_i の値に重複がありうることに注意してください。

Score: 550 points

Problem Statement

You are given N integers A_1, A_2, \dots, A_N.
Find the number of triples of integers (i, j, k) that satisfy the following conditions:

  • 1 \le i, j, k \le N
  • A_i \times A_j = A_k

Constraints

  • 1 \le N \le 1000
  • \color{red}{1 \le A_i < 10^{1000}}

Input

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

N
A_1
A_2
\vdots
A_N

Output

Print the answer as an integer.


Sample Input 1

5
2
3
6
12
24

Sample Output 1

6

The following six triples (i, j, k) satisfy the conditions in the problem statement:

  • (1, 2, 3)
  • (1, 3, 4)
  • (1, 4, 5)
  • (2, 1, 3)
  • (3, 1, 4)
  • (4, 1, 5)

Sample Input 2

11
1
2
3
4
5
6
123456789123456789
123456789123456789
987654321987654321
987654321987654321
121932631356500531347203169112635269

Sample Output 2

40

Note that the values of each integer A_i can be huge.


Sample Input 3

9
4
4
4
2
2
2
1
1
1

Sample Output 3

162

Note that there may be duplicates among the values of A_i.