A - Save the Monsters

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1 から N までの番号がついた N 体のモンスターをあなたは飼っています.

あなたのモンスターを討伐するために勇者がやってきました. 勇者はこれから M ターンかけてモンスターに攻撃を仕掛けます. i ターン目には,勇者は以下のいずれかの行動を行います.

  • MP を 1 消費してモンスター X_i を攻撃する. この行動は,モンスター X_i がまだ生きており,かつ勇者の MP が 1 以上のときにのみ行える.

  • 何もしない.

勇者が攻撃を行った場合,あなたはそれに対して以下のいずれかの行動を行います.

  • MP を 1 消費してモンスター X_i を守る. この行動はあなたの MP が 1 以上のときにのみ行える.

  • 何もしない.このとき,モンスター X_i は死んでしまう.

最初のターンが始まる前の段階で,勇者の MP は A,あなたの MP は B です. また,勇者もあなたも N,M,A,B,X_i の値をすべて把握しています. このとき,以下の条件をみたす最大の整数 k を求めてください.

  • あなたが適切な戦略を取ることで,勇者がどのように行動したとしても,k 体以上のモンスターを最後まで生存させることができる.

制約

  • 1 \leq N,M \leq 250000
  • 1 \leq B \leq A \leq M
  • 1 \leq X_i \leq N
  • 入力される値はすべて整数.

入力

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

N M A B
X_1 X_2 \cdots X_M

出力

答えを出力せよ.


入力例 1

2 3 2 1
1 2 1

出力例 1

1

あなたは 1 体以上のモンスターを必ず生存させることができます.

以下にありうる進行の一例を示します.

  • 1 ターン目: 勇者がモンスター 1 を攻撃する.
    • あなたは何もせず,モンスター 1 が死ぬ.
  • 2 ターン目: 勇者がモンスター 2 を攻撃する.
    • あなたはモンスター 2 を守る.
  • 3 ターン目: 勇者はなにもしない.

入力例 2

2 6 3 2
1 1 1 2 2 2

出力例 2

1

入力例 3

100 1 1 1
100

出力例 3

100

入力例 4

6 20 16 5
5 6 1 3 2 1 4 3 2 4 1 4 4 6 3 3 5 2 2 2

出力例 4

2

Score : 500 points

Problem Statement

You keep N monsters numbered 1 to N.

A hero has come to slay your monsters. He will take M turns to attack the monsters. In the i-th turn, he performs one of the following actions.

  • Consume 1 MP to attack monster X_i. This action can only be performed when monster X_i is still alive and the hero has at least 1 MP.

  • Do nothing.

If the hero attacks, you will respond by performing one of the following actions.

  • Consume 1 MP to defend monster X_i. This action can only be performed when you have at least 1 MP.

  • Do nothing. The monster will die.

Before the first turn begins, the hero has A MP, and you have B MP. Both you and the hero know all values of N, M, A, B, and X_i. Find the greatest integer k that satisfies the following condition.

  • By using an appropriate strategy, you can keep at least k monsters alive no matter how the hero acts.

Constraints

  • 1 \leq N,M \leq 250000
  • 1 \leq B \leq A \leq M
  • 1 \leq X_i \leq N
  • All input values are integers.

Input

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

N M A B
X_1 X_2 \cdots X_M

Output

Print the answer.


Sample Input 1

2 3 2 1
1 2 1

Sample Output 1

1

You can always keep at least one monster alive.

Here is one possible progression.

  • The first turn: The hero attacks monster 1.
    • You do nothing, and monster 1 dies.
  • The second turn: The hero attacks monster 2.
    • You defend monster 2.
  • The third turn: The hero does nothing.

Sample Input 2

2 6 3 2
1 1 1 2 2 2

Sample Output 2

1

Sample Input 3

100 1 1 1
100

Sample Output 3

100

Sample Input 4

6 20 16 5
5 6 1 3 2 1 4 3 2 4 1 4 4 6 3 3 5 2 2 2

Sample Output 4

2
B - Non-Overlapping Swaps

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

(1,2,\cdots,N) の順列 P=(P_1,P_2,\cdots,P_N) が与えられます.

整数の組の列 ((l_1,r_1),(l_2,r_2),\cdots,(l_k,r_k)) であって,以下の条件をすべて満たすものを一つ求めてください.

  • 列の長さ k0 \leq k \leq N-1 を満たす.
  • 1 \leq l_i \leq r_i \leq N (1 \leq i \leq k)
  • 1 \leq i \leq k-1 について,r_{i+1} \leq l_i または r_i \leq l_{i+1} が成立する.
  • 次の操作を k 回行うと,P が昇順にソートされる.
    • i 回目の操作: P_{l_i}P_{r_i} の値を入れ替える. ただし,l_i=r_i のときは何もしない.

この問題の制約下で,条件を満たす列が必ず存在することが証明できます.

1 つの入力ファイルにつき,T 個のテストケースに答えて下さい.

制約

  • 1 \leq T \leq 250000
  • 1 \leq N \leq 250000
  • P=(P_1,P_2,\cdots,P_N)(1,2,\cdots,N) の順列
  • 1 つの入力ファイルにつき,N の総和は 250000 を超えない.
  • 入力される値はすべて整数.

入力

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

T
case_1
case_2
\vdots
case_T

各テストケース case_i は以下の形式である.

N
P_1 P_2 \cdots P_N

出力

各テストケースについて,以下の形式で答えを出力せよ.

k
l_1 r_1
l_2 r_2
\vdots
l_k r_k

解が複数存在する場合,どれを出力しても正解とみなされる.


入力例 1

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

出力例 1

2
2 3
1 2
3
1 4
1 1
2 3
0

1 つめのテストケースについて説明します.

この出力例はすべての条件を満たしています. 例えば 4 番目の条件を満たしていることは,次のように確認できます. P=(2,3,1)\to(P_2,P_3 をスワップ)\to(2,1,3)\to(P_1,P_2をスワップ)\to(1,2,3)

逆に,以下のような出力は正しくありません.

2
1 2
1 3

これは,3 番目の条件を満たしていないためです.

Score : 1000 points

Problem Statement

You are given a permutation P=(P_1,P_2,\cdots,P_N) of (1,2,\cdots,N).

Find one sequence of integer pairs ((l_1,r_1),(l_2,r_2),\cdots,(l_k,r_k)) that satisfies all of the following conditions.

  • The length k of the sequence satisfies 0 \leq k \leq N-1.
  • 1 \leq l_i \leq r_i \leq N (1 \leq i \leq k).
  • For each 1 \leq i \leq k-1, it holds that r_{i+1} \leq l_i or r_i \leq l_{i+1}.
  • Performing the following k operations sorts P in ascending order.
    • The i-th operation: swap the values of P_{l_i} and P_{r_i}. If l_i=r_i, do nothing.

It can be proved that such a sequence always exists under the constraints of this problem.

Process T test cases for each input file.

Constraints

  • 1 \leq T \leq 250000
  • 1 \leq N \leq 250000
  • P=(P_1,P_2,\cdots,P_N) is a permutation of (1,2,\cdots,N).
  • The sum of N for each input file is at most 250000.
  • All input values are integers.

Input

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

T
case_1
case_2
\vdots
case_T

Each test case case_i is in the following format:

N
P_1 P_2 \cdots P_N

Output

For each test case, print a solution in the following format:

k
l_1 r_1
l_2 r_2
\vdots
l_k r_k

If multiple solutions exist, any of them will be accepted.


Sample Input 1

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

Sample Output 1

2
2 3
1 2
3
1 4
1 1
2 3
0

Let us explain the first test case.

The sample output satisfies all of the conditions. For example, the fourth condition can be verified as follows: P=(2,3,1)\to(swap P_2,P_3)\to(2,1,3)\to(swap P_1,P_2)\to(1,2,3)

The following output, on the other hand, is not correct:

2
1 2
1 3

This is because the third condition is not satisfied.

C - Shrink the Tree

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 1500

問題文

1 から N までの番号のついた N 頂点からなる木 T が与えられます. i 番目の辺は頂点 A_iB_i を結んでいます.

あなたは以下の操作を好きな回数 (0 回でもよい) 行うことができます.

  • T2 つの葉 u,v であって,その距離が奇数であるものを選ぶ. そして,u,v を(それらに接続する辺とともに) T から削除する.

ここで,ある頂点が葉であるとは,操作を行おうとする時点でその次数がちょうど 1 であることを意味します. また,2 つの頂点の距離とは,その頂点間を結ぶパスに含まれる辺の個数です.

操作後の T に含まれる頂点集合としてありうるものが何通りあるかを 998244353 で割ったあまりを求めてください.

制約

  • 2 \leq N \leq 150
  • 1 \leq A_i,B_i \leq N
  • 入力で与えられるグラフは木である.
  • 入力される値はすべて整数.

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}

出力

答えを出力せよ.


入力例 1

4
1 2
2 3
3 4

出力例 1

3

1 度も操作を行わないことで,頂点集合 \{1,2,3,4\} を得ることができます.

T の葉 1,4 を消すことで,頂点集合 \{2,3\} を得ることができます.

ここから更に葉 2,3 を消すことで,頂点集合 \{\} を得ることができます.


入力例 2

5
2 1
3 1
4 1
5 3

出力例 2

3

入力例 3

8
2 1
3 2
4 1
5 2
6 5
7 3
8 7

出力例 3

11

入力例 4

24
12 7
14 4
8 13
24 13
1 3
4 9
17 2
1 21
24 22
11 1
15 17
22 5
23 10
24 12
13 6
12 16
10 21
19 22
20 17
4 20
20 6
10 18
21 6

出力例 4

5359

Score : 1500 points

Problem Statement

You are given a tree T with N vertices numbered 1 to N. The i-th edge connects vertices A_i and B_i.

You can perform the following operation any number of times, possibly zero.

  • Choose two leaves u and v of T such that the distance between them is odd. Then, remove u and v from T (along with the incident edges).

Here, a vertex is said to be a leaf when its degree is exactly one at the time of the operation. The distance between two vertices is the number of edges in the path connecting those vertices.

Find the number, modulo 998244353, of sets that can be the vertex set of T after your operations.

Constraints

  • 2 \leq N \leq 150
  • 1 \leq A_i,B_i \leq N
  • The input graph is a tree.
  • All input values are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}

Output

Print the answer.


Sample Input 1

4
1 2
2 3
3 4

Sample Output 1

3

Performing zero operations yields the vertex set \{1,2,3,4\}.

Erasing the leaves 1 and 4 from T yields the vertex set \{2,3\}.

From this state, erasing the leaves 2 and 3 yields the vertex set \{\}.


Sample Input 2

5
2 1
3 1
4 1
5 3

Sample Output 2

3

Sample Input 3

8
2 1
3 2
4 1
5 2
6 5
7 3
8 7

Sample Output 3

11

Sample Input 4

24
12 7
14 4
8 13
24 13
1 3
4 9
17 2
1 21
24 22
11 1
15 17
22 5
23 10
24 12
13 6
12 16
10 21
19 22
20 17
4 20
20 6
10 18
21 6

Sample Output 4

5359
D - Welcome to Tokyo!

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 2000

問題文

1 から M までの番号のついた M 人の競技プログラマーがおり,今日から N 日間の間に東京を訪れます. 競技プログラマー i は,L_i 日目から R_i 日目まで (1 \leq L_i \leq R_i \leq N) 東京に滞在します.

maroon 君は彼らとの食事会を計画しています. x 日目 (1 \leq x \leq N) に食事会を開催すると,L_i \leq x \leq R_i を満たすすべての競技プログラマー i と友達になることができます.

k=1,2,\cdots,N について,以下の問題を解いてください.

  • ちょうど k 回の食事会を開催する場合,友達になることができる競技プログラマーの人数の最大値を求めよ.

制約

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq 10^6
  • 1 \leq L_i \leq R_i \leq N
  • 入力される値はすべて整数.

入力

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

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

出力

N 行出力せよ. i 行目には k=i に対する答えを出力せよ.


入力例 1

3 3
1 1
1 2
3 3

出力例 1

2
3
3
  • k=1: 1 日目に食事会を開催すると,競技プログラマー 1,2 と友達になることができます.
  • k=2: 1,3 日目に食事会を開催すると,競技プログラマー 1,2,3 と友達になることができます.
  • k=3: 1,2,3 日目に食事会を開催すると,競技プログラマー 1,2,3 と友達になることができます.

入力例 2

4 4
1 1
2 2
3 3
4 4

出力例 2

1
2
3
4

入力例 3

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

出力例 3

4
6
6

入力例 4

20 15
15 19
1 8
6 11
3 11
11 17
6 6
16 20
7 11
11 14
2 19
1 3
7 7
6 19
14 15
15 15

出力例 4

7
11
12
13
14
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15

Score : 2000 points

Problem Statement

There are M competitive programmers (kyopro-ers) numbered 1 to M, who will visit Tokyo during the N days starting today. Kyopro-er i will stay in Tokyo from the L_i-th day through the R_i-th day (1 \leq L_i \leq R_i \leq N).

Maroon is planning dinner parties with them. If a dinner party is held on the x-th day (1 \leq x \leq N), he can make friends with all kyopro-ers i with L_i \leq x \leq R_i.

For each k=1,2,\cdots,N, solve the following problem.

  • Find the maximum number of kyopro-ers Maroon can make friends with if he holds exactly k dinner parties.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq 10^6
  • 1 \leq L_i \leq R_i \leq N
  • All input values are integers.

Input

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

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print N lines. The i-th line should contain the answer for k=i.


Sample Input 1

3 3
1 1
1 2
3 3

Sample Output 1

2
3
3
  • k=1: If he holds a party on the 1-st day, he can make friends with kyopro-ers 1 and 2.
  • k=2: If he holds a party on the 1-st and 3-rd days, he can make friends with kyopro-ers 1, 2, and 3.
  • k=3: If he holds a party on the 1-st, 2-nd, and 3-rd days, he can make friends with kyopro-ers 1, 2, and 3.

Sample Input 2

4 4
1 1
2 2
3 3
4 4

Sample Output 2

1
2
3
4

Sample Input 3

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

Sample Output 3

4
6
6

Sample Input 4

20 15
15 19
1 8
6 11
3 11
11 17
6 6
16 20
7 11
11 14
2 19
1 3
7 7
6 19
14 15
15 15

Sample Output 4

7
11
12
13
14
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
E - Sort A[i]-i

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 2500

問題文

正整数 N,M が与えられます.ここで,N<M です.

長さ N の非負整数列 a=(a_1,a_2,\cdots,a_N) であって,以下の条件を満たすものをよい数列と呼ぶことにします.

  • 0 \leq a_1 \leq a_2 \leq \cdots \leq a_N \leq M

よい数列 a について,(a_1-1,a_2-2,\cdots,a_N-N) を昇順にソートして得られる数列を f(a) と表すことにします.

k=1,2,\cdots,N について,次の問題を解いてください.

  • あり得るすべてのよい数列 a に対して f(a)k 番目の要素を求めたとする. これらの値の総和を 998244353 で割ったあまりを求めよ.

※ 負の数を割ったあまりも 0 以上 998244353 未満の範囲で求めてください.

制約

  • 1 \leq N < M \leq 10^6
  • 入力される値はすべて整数.

入力

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

N M

出力

N 行出力せよ. i 行目には k=i に対する答えを出力せよ.


入力例 1

2 3

出力例 1

998244349
4

あり得る a とそれに対応する f(a) は以下の通りです.

  • a=(0,0): f(a)=(-2,-1)
  • a=(0,1): f(a)=(-1,-1)
  • a=(0,2): f(a)=(-1,0)
  • a=(0,3): f(a)=(-1,1)
  • a=(1,1): f(a)=(-1,0)
  • a=(1,2): f(a)=(0,0)
  • a=(1,3): f(a)=(0,1)
  • a=(2,2): f(a)=(0,1)
  • a=(2,3): f(a)=(1,1)
  • a=(3,3): f(a)=(1,2)

よって,f(a)1 番目の要素の総和は -4 になり,f(a)2 番目の要素の総和は 4 になります.


入力例 2

3 4

出力例 2

998244329
0
24

入力例 3

4 6

出力例 3

998244233
35
175
330

入力例 4

10 1000000

出力例 4

297189103
747015740
88545731
123651717
920498165
977169022
775771117
810877103
152407094
602233731

Score : 2500 points

Problem Statement

You are given positive integers N and M. Here, N<M.

A sequence of N non-negative integers a=(a_1,a_2,\cdots,a_N) is said to be a good sequence when it satisfies the following condition:

  • 0 \leq a_1 \leq a_2 \leq \cdots \leq a_N \leq M.

For a good sequence a, let f(a) be the sequence resulting from sorting (a_1-1,a_2-2,\cdots,a_N-N) in ascending order.

For each k=1,2,\cdots,N, solve the following problem.

  • Assume that we have found the k-th element of f(a) for every possible good sequence a. Find the sum, modulo 998244353, of these values.

※ The answer should be between 0 and 998244352 even if the sum is negative.

Constraints

  • 1 \leq N < M \leq 10^6
  • All input values are integers.

Input

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

N M

Output

Print N lines. The i-th line should contain the answer for k=i.


Sample Input 1

2 3

Sample Output 1

998244349
4

Here are all possible a and the corresponding f(a).

  • a=(0,0): f(a)=(-2,-1)
  • a=(0,1): f(a)=(-1,-1)
  • a=(0,2): f(a)=(-1,0)
  • a=(0,3): f(a)=(-1,1)
  • a=(1,1): f(a)=(-1,0)
  • a=(1,2): f(a)=(0,0)
  • a=(1,3): f(a)=(0,1)
  • a=(2,2): f(a)=(0,1)
  • a=(2,3): f(a)=(1,1)
  • a=(3,3): f(a)=(1,2)

Thus, the sum of the first elements is -4, and the sum of the second elements is 4.


Sample Input 2

3 4

Sample Output 2

998244329
0
24

Sample Input 3

4 6

Sample Output 3

998244233
35
175
330

Sample Input 4

10 1000000

Sample Output 4

297189103
747015740
88545731
123651717
920498165
977169022
775771117
810877103
152407094
602233731