A - Sanitize Hands

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

消毒液の入ったボトルがあり、その消毒液によってちょうど M 本の手を消毒することができます。

N 人の宇宙人が順に手の消毒を行いに来ます。
i 人目 (1\leq i\leq N) の宇宙人は H_i 本の手を持っており、それぞれ自身のすべての手を 1 回ずつ消毒したいと考えています。

何人目の宇宙人までがすべての手を消毒できるか求めてください。
ただし、ある宇宙人が消毒を始める時点で、自身のすべての手を消毒する分の消毒液が残っていなかったとしても、その宇宙人はその消毒液を使い切ってしまうものとします。

制約

  • 1\leq N,M\leq 100
  • 1\leq H_i\leq 100
  • 入力はすべて整数

入力

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

N M
H_1 H_2 \ldots H_N

出力

何人目の宇宙人までが自身のすべての手を消毒できるか出力せよ。


入力例 1

5 10
2 3 2 5 3

出力例 1

3

次の手順で宇宙人は自身の手を消毒します。

  • 1 人目の宇宙人は自身の 2 本の手を消毒します。残りの消毒液によって、10-2=8 本の手を消毒できます。
  • 2 人目の宇宙人は自身の 3 本の手を消毒します。残りの消毒液によって、8-3=5 本の手を消毒できます。
  • 3 人目の宇宙人は自身の 2 本の手を消毒します。残りの消毒液によって、5-2=3 本の手を消毒できます。
  • 4 人目の宇宙人は 5 本の手を持っていますが、消毒液は 3 本分しかないため消毒液を使い切り、かつ自身のすべての手を消毒できません。

よって、3 人目の宇宙人までが自身のすべての手を消毒できるため、3 を出力します。


入力例 2

5 10
2 3 2 3 5

出力例 2

4

入力例 3

1 5
1

出力例 3

1

すべての宇宙人が自身の手を消毒することができます。

Score : 100 points

Problem Statement

There is a bottle of disinfectant that can disinfect exactly M hands.

N aliens come one by one to disinfect their hands.
The i-th alien (1 \leq i \leq N) has H_i hands and wants to disinfect all of their hands once.

Determine how many aliens can disinfect all of their hands.
Here, even if there is not enough disinfectant left for an alien to disinfect all of their hands when they start, they will use up the remaining disinfectant.

Constraints

  • 1 \leq N, M \leq 100
  • 1 \leq H_i \leq 100
  • All input values are integers.

Input

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

N M
H_1 H_2 \ldots H_N

Output

Print the number of aliens who can disinfect all of their hands.


Sample Input 1

5 10
2 3 2 5 3

Sample Output 1

3

The aliens disinfect their hands in the following steps:

  • The first alien disinfects their two hands. The remaining disinfectant can disinfect 10-2=8 hands.
  • The second alien disinfects their three hands. The remaining disinfectant can disinfect 8-3=5 hands.
  • The third alien disinfects their two hands. The remaining disinfectant can disinfect 5-2=3 hands.
  • The fourth alien has five hands, but there is only enough disinfectant for three hands, so they use up the disinfectant without disinfecting all of their hands.

Thus, the first three aliens can disinfect all of their hands, so print 3.


Sample Input 2

5 10
2 3 2 3 5

Sample Output 2

4

Sample Input 3

1 5
1

Sample Output 3

1

All aliens can disinfect their hands.

B - Buildings

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

N 個のビルが横一列に並んでいて、左から i 番目のビルの高さは H_i です。

左から 1 番目のビルより高いビルが存在するか判定し、存在する場合その内最も左のビルは左から何番目か求めてください。

制約

  • 1\leq N\leq 100
  • 1\leq H_i \leq 100
  • 入力される数値は全て整数

入力

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

N 
H_1 H_2 \ldots H_N

出力

左から 1 番目のビルより高いビルが存在しない場合 -1 を出力せよ。

存在する場合、その内最も左のビルは左から何番目か出力せよ。


入力例 1

4
3 2 5 2

出力例 1

3

左から 1 番目のビルより高いビルは、左から 3 番目のビルです。


入力例 2

3
4 3 2

出力例 2

-1

左から 1 番目のビルより高いビルは存在しません。


入力例 3

7
10 5 10 2 10 13 15

出力例 3

6

左から 1 番目のビルより高いビルは、左から 6 番目のビルと左から 7 番目のビルです。その内最も左のビルは左から 6 番目のビルです。

Score: 100 points

Problem Statement

There are N buildings aligned in a row. The i-th building from the left has a height of H_i.

Determine if there is a building taller than the first one from the left. If such a building exists, find the position of the leftmost such building from the left.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq H_i \leq 100
  • All input values are integers.

Input

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

N
H_1 H_2 \ldots H_N

Output

If no building is taller than the first one from the left, print -1.

If such a building exists, print the position (index) of the leftmost such building from the left.


Sample Input 1

4
3 2 5 2

Sample Output 1

3

The building taller than the first one from the left is the third one from the left.


Sample Input 2

3
4 3 2

Sample Output 2

-1

No building is taller than the first one from the left.


Sample Input 3

7
10 5 10 2 10 13 15

Sample Output 3

6

The buildings taller than the first one from the left are the sixth and seventh ones. Among them, the leftmost is the sixth one.

C - Delimiter

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150

問題文

N 個の整数 A_1,A_2,\dots,A_N が、 1 行に 1 つずつ、 N 行にわたって与えられます。但し、 N は入力では与えられません。
さらに、以下が保証されます。

  • A_i \neq 0 ( 1 \le i \le N-1 )
  • A_N = 0

A_N, A_{N-1},\dots,A_1 をこの順に出力してください。

制約

  • 入力は全て整数
  • 1 \le N \le 100
  • 1 \le A_i \le 10^9 ( 1 \le i \le N-1 )
  • A_N = 0

入力

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

A_1
A_2
\vdots
A_N

出力

A_N, A_{N-1},\dots,A_1 をこの順に、改行区切りで整数として出力せよ。


入力例 1

3
2
1
0

出力例 1

0
1
2
3

繰り返しになりますが、 N は入力では与えられないことに注意してください。
この入力においては N=4 で、 A=(3,2,1,0) です。


入力例 2

0

出力例 2

0

A=(0) です。


入力例 3

123
456
789
987
654
321
0

出力例 3

0
321
654
987
789
456
123

Score: 150 points

Problem Statement

You are given N integers A_1,A_2,\dots,A_N, one per line, over N lines. However, N is not given in the input.
Furthermore, the following is guaranteed:

  • A_i \neq 0 ( 1 \le i \le N-1 )
  • A_N = 0

Print A_N, A_{N-1},\dots,A_1 in this order.

Constraints

  • All input values are integers.
  • 1 \le N \le 100
  • 1 \le A_i \le 10^9 ( 1 \le i \le N-1 )
  • A_N = 0

Input

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

A_1
A_2
\vdots
A_N

Output

Print A_N, A_{N-1}, \dots, A_1 in this order, as integers, separated by newlines.


Sample Input 1

3
2
1
0

Sample Output 1

0
1
2
3

Note again that N is not given in the input. Here, N=4 and A=(3,2,1,0).


Sample Input 2

0

Sample Output 2

0

A=(0).


Sample Input 3

123
456
789
987
654
321
0

Sample Output 3

0
321
654
987
789
456
123
D - Greedy Draft

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

N 人の客がおり、1 から N までの番号が付けられています。また、M 本の缶ジュースがあり、1 から M までの番号が付けられています。

i (1 \leq i \leq N) は長さ L_i の希望リストを持っています。客 i の希望リストの先頭から j 番目 (1 \leq j \leq L_i) は缶ジュース X_{i,j} です。任意の客 i に対して、客 i の希望リストに載っている番号 X_{i, 1}, \dots, X_{i, L_i} は相異なります。

これから客 1, \dots, N が番号の小さいほうから順に、以下にしたがって自分が飲む飲料を選びます。

  • その時点で誰にも選ばれていない缶ジュースの番号が自分の希望リストに存在する場合、そのうち先頭に最も近い番号の缶ジュースを選ぶ。そうでない場合は水を選ぶ。

それぞれの客がどの飲料を得るかを求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq M \leq 100
  • 1 \leq L_i \leq M (1 \leq i \leq N)
  • 1 \leq X_{i,j} \leq M (1 \leq i \leq N, 1 \leq j \leq L_i)
  • X_{i, 1}, \dots, X_{i, L_i} は相異なる (1 \leq i \leq N)
  • 入力される値はすべて整数

入力

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

N M
L_1
X_{1,1} X_{1,2} \cdots X_{1,L_1}
L_2
X_{2,1} X_{2,2} \cdots X_{2,L_2}
\vdots
L_N
X_{N,1} X_{N,2} \cdots X_{N,L_N}

出力

N 行出力せよ。i 行目 (1 \leq i \leq N) には、客 i が缶ジュースを得る場合はその番号を、水を得る場合は 0 を出力せよ。


入力例 1

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

出力例 1

3
2
0
5

1 の希望リストにある番号のうち、対応する缶ジュースが誰にも選ばれていないのは 3,1,2 です。このうち先頭に最も近いのは 3 なので、客 1 は缶ジュース 3 を選びます。

2 の希望リストにある番号のうち、対応する缶ジュースが誰にも選ばれていないのは 2,1 です。このうち先頭に最も近いのは 2 なので、客 2 は缶ジュース 2 を選びます。

3 の希望リストにある番号について、対応する缶ジュースはすべてその時点で誰かに選ばれています。よって客 3 は水を選びます。

4 の希望リストにある番号のうち、対応する缶ジュースが誰にも選ばれていないのは 5,1 です。このうち先頭に最も近いのは 5 なので、客 4 は缶ジュース 5 を選びます。


入力例 2

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

出力例 2

3
5
1
4
2
0

Score : 200 points

Problem Statement

There are N customers numbered 1 to N, and M canned juices numbered 1 to M.

Customer i (1 \leq i \leq N) has a wish list of length L_i. The j-th item (1 \leq j \leq L_i) from the top of customer i's wish list is canned juice X_{i,j}. For any customer i, the numbers X_{i, 1}, \dots, X_{i, L_i} on customer i's wish list are distinct.

Customers 1, \dots, N, in this order, will now choose their beverages, following the procedure below.

  • If the customer's wish list contains a canned juice that has not yet been chosen by anyone at that point, they choose the canned juice whose number appears earliest in their wish list. Otherwise, they choose water.

Determine which beverage each customer gets.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq M \leq 100
  • 1 \leq L_i \leq M (1 \leq i \leq N)
  • 1 \leq X_{i,j} \leq M (1 \leq i \leq N, 1 \leq j \leq L_i)
  • X_{i, 1}, \dots, X_{i, L_i} are distinct. (1 \leq i \leq N)
  • All input values are integers.

Input

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

N M
L_1
X_{1,1} X_{1,2} \cdots X_{1,L_1}
L_2
X_{2,1} X_{2,2} \cdots X_{2,L_2}
\vdots
L_N
X_{N,1} X_{N,2} \cdots X_{N,L_N}

Output

Output N lines. The i-th line (1 \leq i \leq N) should contain the number of the canned juice customer i gets if they get one, or 0 if customer i gets water.


Sample Input 1

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

Sample Output 1

3
2
0
5

Among the numbers on customer 1's wish list, the canned juices not yet chosen by anyone are 3, 1, 2. The one appearing earliest in the list is 3, so customer 1 chooses canned juice 3.

Among the numbers on customer 2's wish list, the canned juices not yet chosen by anyone are 2, 1. The one appearing earliest in the list is 2, so customer 2 chooses canned juice 2.

For the numbers on customer 3's wish list, all corresponding canned juices have already been chosen by someone at that point. Thus, customer 3 chooses water.

Among the numbers on customer 4's wish list, the canned juices not yet chosen by anyone are 5, 1. The one appearing earliest in the list is 5, so customer 4 chooses canned juice 5.


Sample Input 2

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

Sample Output 2

3
5
1
4
2
0
E - Medicine

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

高橋君は医者のすぬけ君から N 種類の薬を処方されました。i 種類目の薬は(処方された日を含めて) a_i 日間、毎日 b_i 錠ずつ飲む必要があります。また、高橋君はこれ以外の薬を飲む必要がありません。

薬を処方された日を 1 日目とします。1 日目以降で、初めて高橋君がその日に飲む必要がある薬が K 錠以下になるのは何日目かを求めてください。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq K \leq 10^9
  • 1 \leq a_i,b_i \leq 10^9
  • 入力はすべて整数

入力

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

N K
a_1 b_1
\vdots
a_N b_N

出力

1 日目以降で、初めて高橋君がその日に飲む必要がある薬が K 錠以下になるのが X 日目の時、 X を出力せよ。


入力例 1

4 8
6 3
2 5
1 9
4 2

出力例 1

3

1 日目には、高橋君は 1,2,3,4 種類目の薬をそれぞれ 3,5,9,2 錠飲む必要があります。よってこの日は 19 錠飲む必要があり、K(=8) 錠以下ではありません。
2 日目には、高橋君は 1,2,4 種類目の薬をそれぞれ 3,5,2 錠飲む必要があります。よってこの日は 10 錠飲む必要があり、K(=8) 錠以下ではありません。
3 日目には、高橋君は 1,4 種類目の薬をそれぞれ 3,2 錠飲む必要があります。よってこの日は 5 錠飲む必要があり、初めて K(=8) 錠以下になります。

以上より、3 が答えです。


入力例 2

4 100
6 3
2 5
1 9
4 2

出力例 2

1

入力例 3

15 158260522
877914575 2436426
24979445 61648772
623690081 33933447
476190629 62703497
211047202 71407775
628894325 31963982
822804784 50968417
430302156 82631932
161735902 80895728
923078537 7723857
189330739 10286918
802329211 4539679
303238506 17063340
492686568 73361868
125660016 50287940

出力例 3

492686569

Score : 350 points

Problem Statement

Snuke the doctor prescribed N kinds of medicine for Takahashi. For the next a_i days (including the day of the prescription), he has to take b_i pills of the i-th medicine. He does not have to take any other medicine.

Let the day of the prescription be day 1. On or after day 1, when is the first day on which he has to take K pills or less?

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq K \leq 10^9
  • 1 \leq a_i,b_i \leq 10^9
  • All input values are integers.

Input

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

N K
a_1 b_1
\vdots
a_N b_N

Output

If Takahashi has to take K pills or less on day X for the first time on or after day 1, print X.


Sample Input 1

4 8
6 3
2 5
1 9
4 2

Sample Output 1

3

On day 1, he has to take 3,5,9, and 2 pills of the 1-st, 2-nd, 3-rd, and 4-th medicine, respectively. In total, he has to take 19 pills on this day, which is not K(=8) pills or less.
On day 2, he has to take 3,5, and 2 pills of the 1-st, 2-nd, and 4-th medicine, respectively. In total, he has to take 10 pills on this day, which is not K(=8) pills or less.
On day 3, he has to take 3 and 2 pills of the 1-st and 4-th medicine, respectively. In total, he has to take 5 pills on this day, which is K(=8) pills or less for the first time.

Thus, the answer is 3.


Sample Input 2

4 100
6 3
2 5
1 9
4 2

Sample Output 2

1

Sample Input 3

15 158260522
877914575 2436426
24979445 61648772
623690081 33933447
476190629 62703497
211047202 71407775
628894325 31963982
822804784 50968417
430302156 82631932
161735902 80895728
923078537 7723857
189330739 10286918
802329211 4539679
303238506 17063340
492686568 73361868
125660016 50287940

Sample Output 3

492686569
F - Ameba

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

あなたはアメーバの観察記録をつけました。

最初 1 匹のアメーバがおり、番号は 1 です。

観察記録は時系列順に N 個あり、i 番目の観察記録は「番号 A_i のアメーバが分裂して消滅し、新たに 2 匹のアメーバが生まれ、それらにそれぞれ 2i,2i+1 と番号をつけた」というものです。
このとき、アメーバ A_i を アメーバ 2i,2i+1 の親と呼びます。

k=1,\ldots,2N+1 について、アメーバ k から何代親を遡るとアメーバ 1 になるか求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 観察記録は矛盾していない。すなわち
    • 1\leq A_i \leq 2i-1
    • A_i は相異なる整数

入力

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

N
A_1 A_2 \ldots A_N

出力

2N+1 行出力せよ。k 行目にはアメーバ k から何代親を遡るとアメーバ 1 になるかを出力せよ。


入力例 1

2
1 2

出力例 1

0
1
1
2
2

アメーバ 1 からアメーバ 2,3 が生まれ、アメーバ 2 からアメーバ 4,5 が生まれました。

  • アメーバ 10 代遡るとアメーバ 1 になります。
  • アメーバ 21 代遡るとアメーバ 1 になります。
  • アメーバ 31 代遡るとアメーバ 1 になります。
  • アメーバ 41 代遡るとアメーバ 2 になり、2 代遡るとアメーバ 1 になります。
  • アメーバ 51 代遡るとアメーバ 2 になり、2 代遡るとアメーバ 1 になります。

入力例 2

4
1 3 5 2

出力例 2

0
1
1
2
2
3
3
2
2

Score : 300 points

Problem Statement

You observed amoebae and kept some records.

Initially, there was one amoeba, numbered 1.

You made N records. According to the i-th record, the amoeba numbered A_i disappeared by dividing itself into two new amoebae, which were then numbered 2i and 2i+1.
Here, amoeba A_i is said to be the parent of amoebae 2i and 2i+1.

For each k=1,\ldots,2N+1, how many generations away is amoeba k from amoeba 1?

Constraints

  • 1 \leq N \leq 2\times 10^5
  • The records are consistent. That is:
    • 1\leq A_i \leq 2i-1.
    • A_i are distinct integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print 2N+1 lines. The k-th line should contain the generation distance between amoeba 1 and amoeba k.


Sample Input 1

2
1 2

Sample Output 1

0
1
1
2
2

From amoeba 1, amoebae 2 and 3 were born. From amoeba 2, amoebae 4 and 5 were born.

  • Amoeba 1 is zero generations away from amoeba 1.
  • Amoeba 2 is one generation away from amoeba 1.
  • Amoeba 3 is one generation away from amoeba 1.
  • Amoeba 4 is one generation away from amoeba 2, and two generations away from amoeba 1.
  • Amoeba 5 is one generation away from amoeba 2, and two generations away from amoeba 1.

Sample Input 2

4
1 3 5 2

Sample Output 2

0
1
1
2
2
3
3
2
2
G - Rectangles

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

2 次元平面上に N 個の相異なる点があり、1,2,\ldots ,N の番号がついています。点 i\,(1 \leq i \leq N) の座標は (x_i,y_i) です。

これらの点のうち 4 つを頂点とし、全ての辺が x 軸または y 軸に平行であるような長方形はいくつありますか?

制約

  • 4 \leq N \leq 2000
  • 0 \leq x_i, y_i \leq 10^9
  • (x_i,y_i) \neq (x_j,y_j) (i \neq j)
  • 入力は全て整数である。

入力

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

N
x_1 y_1
x_2 y_2
 \vdots 
x_N y_N

出力

答えを出力せよ。


入力例 1

6
0 0
0 1
1 0
1 1
2 0
2 1

出力例 1

3

1 、点 2 、点 3 、点 4 を頂点とする長方形、

1 、点 2 、点 5 、点 6 を頂点とする長方形、

3 、点 4 、点 5 、点 6 を頂点とする長方形

の合計 3 つです。


入力例 2

4
0 1
1 2
2 3
3 4

出力例 2

0

入力例 3

7
0 1
1 0
2 0
2 1
2 2
3 0
3 2

出力例 3

1

Score : 400 points

Problem Statement

We have N distinct points on a two-dimensional plane, numbered 1,2,\ldots,N. Point i (1 \leq i \leq N) has the coordinates (x_i,y_i).

How many rectangles are there whose vertices are among the given points and whose edges are parallel to the x- or y-axis?

Constraints

  • 4 \leq N \leq 2000
  • 0 \leq x_i, y_i \leq 10^9
  • (x_i,y_i) \neq (x_j,y_j) (i \neq j)
  • All values in input are integers.

Input

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 the answer.


Sample Input 1

6
0 0
0 1
1 0
1 1
2 0
2 1

Sample Output 1

3

There are three such rectangles:

the rectangle whose vertices are Points 1, 2, 3, 4,

the rectangle whose vertices are Points 1, 2, 5, 6,

and the rectangle whose vertices are Points 3, 4, 5, 6.


Sample Input 2

4
0 1
1 2
2 3
3 4

Sample Output 2

0

Sample Input 3

7
0 1
1 0
2 0
2 1
2 2
3 0
3 2

Sample Output 3

1
H - At Least One

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

整数 M および N 個の整数の組 (A_1, B_1), (A_2, B_2), \dots, (A_N, B_N) が与えられます。
すべての i について 1 \leq A_i \lt B_i \leq M が成り立っています。

次の条件を満たす数列 S良い数列と呼びます。

  • S は数列 (1,2,3,..., M) の連続部分列である。
  • すべての i について SA_i, B_i の少なくとも一方を含んでいる。

長さ k の良い数列としてあり得るものの個数を f(k) とします。
f(1), f(2), \dots, f(M) を列挙してください。

制約

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

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

答えを以下の形式で出力せよ。

f(1) f(2) \dots f(M)

入力例 1

3 5
1 3
1 4
2 5

出力例 1

0 1 3 2 1

良い数列としてあり得るものを列挙すると次のようになります。

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

入力例 2

1 2
1 2

出力例 2

2 1

入力例 3

5 9
1 5
1 7
5 6
5 8
2 6

出力例 3

0 0 1 2 4 4 3 2 1

Score : 500 points

Problem Statement

You are given an integer M and N pairs of integers (A_1, B_1), (A_2, B_2), \dots, (A_N, B_N).
For all i, it holds that 1 \leq A_i \lt B_i \leq M.

A sequence S is said to be a good sequence if the following conditions are satisfied:

  • S is a contiguous subsequence of the sequence (1,2,3,..., M).
  • For all i, S contains at least one of A_i and B_i.

Let f(k) be the number of possible good sequences of length k.
Enumerate f(1), f(2), \dots, f(M).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 2 \times 10^5
  • 1 \leq A_i \lt B_i \leq M
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the answers in the following format:

f(1) f(2) \dots f(M)

Sample Input 1

3 5
1 3
1 4
2 5

Sample Output 1

0 1 3 2 1

Here is the list of all possible good sequences.

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

Sample Input 2

1 2
1 2

Sample Output 2

2 1

Sample Input 3

5 9
1 5
1 7
5 6
5 8
2 6

Sample Output 3

0 0 1 2 4 4 3 2 1
I - Almost Sorted 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : {500}

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) および正整数 D が与えられます。

A を並び替えることで得られる整数列 B=(B_1, B_2, \ldots, B_N) であって、次の条件を満たすものの個数を 998244353 で割ったあまりを求めてください。

  • すべての i\ (1\leq i\leq N-1) に対して B_{i+1}\geq B_i-D が成り立つ。

制約

  • 2\leq N\leq 2\times 10^5
  • 1\leq D\leq 10^6
  • 1\leq A_i\leq 10^6
  • 入力は全て整数

入力

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

N D
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4 1
5 2 1 2

出力例 1

3

条件を満たす整数列は (1,2,2,5),(2,1,2,5),(2,2,1,5)3 つです。


入力例 2

5 10
20 40 60 80 100

出力例 2

1

入力例 3

15 12345
18270 31252 27543 31406 22271 13402 12279 25697 18349 27615 39360 22790 32581 23990 36154

出力例 3

858152905

Score : 500 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N and a positive integer D.

Find the number, modulo 998244353, of integer sequences B=(B_1, B_2, \ldots, B_N) that can be obtained by rearranging A and satisfy the following condition:

  • B_{i+1}\geq B_i-D holds for all i\ (1\leq i\leq N-1).

Constraints

  • 2\leq N\leq 2\times 10^5
  • 1\leq D\leq 10^6
  • 1\leq A_i\leq 10^6
  • All input values are integers.

Input

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

N D
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

4 1
5 2 1 2

Sample Output 1

3

The integer sequences satisfying the condition are (1,2,2,5),(2,1,2,5),(2,2,1,5), which are three sequences.


Sample Input 2

5 10
20 40 60 80 100

Sample Output 2

1

Sample Input 3

15 12345
18270 31252 27543 31406 22271 13402 12279 25697 18349 27615 39360 22790 32581 23990 36154

Sample Output 3

858152905