A - 快適な気温

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

配点 : 200

問題文

高橋君は N 日間の休暇を取る予定です。i 日目 (1 \leq i \leq N) の予想気温は T_i ℃ です。

高橋君は、気温が L ℃ 以上 R ℃ 以下の日を「お出かけ日和」と考えています。

N 日間のうち、お出かけ日和は何日あるか求めてください。

制約

  • 1 \leq N \leq 10^5
  • -100 \leq L \leq R \leq 100
  • -100 \leq T_i \leq 100
  • 入力はすべて整数である

入力

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

N L R
T_1
T_2
\vdots
T_N

1 行目には、休暇の日数 N、お出かけ日和とみなす気温の下限 L、上限 R がスペース区切りで与えられる。

続く N 行の i 行目 (1 \leq i \leq N) には、i 日目の予想気温 T_i が与えられる。

出力

お出かけ日和の日数を 1 行で出力してください。


入力例 1

5 20 25
18
20
22
25
30

出力例 1

3

入力例 2

4 10 12
9
13
0
100

出力例 2

0

入力例 3

10 -5 5
-10
-5
-3
0
4
5
6
10
-1
-6

出力例 3

6

入力例 4

20 15 30
12
15
18
21
24
27
30
33
14
16
19
22
25
28
31
29
17
13
30
20

出力例 4

15

入力例 5

1 -100 -100
-100

出力例 5

1

Score : 200 pts

Problem Statement

Takahashi plans to take an N-day vacation. The forecasted temperature on day i (1 \leq i \leq N) is T_i ℃.

Takahashi considers a day to be a "nice day for an outing" if the temperature is at least L ℃ and at most R ℃.

Out of the N days, determine how many are nice days for an outing.

Constraints

  • 1 \leq N \leq 10^5
  • -100 \leq L \leq R \leq 100
  • -100 \leq T_i \leq 100
  • All inputs are integers

Input

Input is given from standard input in the following format:

N L R
T_1
T_2
\vdots
T_N

The first line contains the number of vacation days N, the lower bound L, and the upper bound R of the temperature range considered nice for an outing, separated by spaces.

The i-th of the following N lines (1 \leq i \leq N) contains the forecasted temperature T_i on day i.

Output

Print the number of nice days for an outing in a single line.


Sample Input 1

5 20 25
18
20
22
25
30

Sample Output 1

3

Sample Input 2

4 10 12
9
13
0
100

Sample Output 2

0

Sample Input 3

10 -5 5
-10
-5
-3
0
4
5
6
10
-1
-6

Sample Output 3

6

Sample Input 4

20 15 30
12
15
18
21
24
27
30
33
14
16
19
22
25
28
31
29
17
13
30
20

Sample Output 4

15

Sample Input 5

1 -100 -100
-100

Sample Output 5

1
B - 農場の区画分け

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

配点 : 300

問題文

高橋君は広大な農場を経営しています。この農場には N 個の畑が東西に一列に並んでおり、各畑には 1 から N までの番号が西から順に割り振られています。畑 i (1 \leq i \leq N) の収穫量は A_i です。

高橋君は農場の管理を効率化するため、これらの畑を M 個の区画に分けることにしました。各区画は連続する番号の畑で構成されます。具体的には、j 番目の区画 (1 \leq j \leq M) は畑 L_j から畑 R_j までの連続した畑で構成されます(L_j \leq R_j)。すべての畑はちょうど1つの区画に属し、区画同士で畑が重複することはありません。なお、区画は入力中で必ずしも西から順に与えられるとは限りません。

各区画の 生産力 を、その区画に含まれる畑の収穫量の合計として定めます。すなわち、j 番目の区画の生産力は A_{L_j} + A_{L_j+1} + \cdots + A_{R_j} です。

高橋君は、区画ごとの生産力のばらつきを把握したいと考えています。生産力の差が大きすぎると、作業員の配置や資材の分配が難しくなるためです。

与えられた区画分けに対して、M 個の区画の生産力の最大値から最小値を引いた値を求めてください。

制約

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq M)
  • M 個の区画は畑 1 から畑 N までを重複なく過不足なく分割する
  • 区画は入力中で必ずしも西から順に与えられるとは限らない
  • 入力はすべて整数

入力

N M
A_1 A_2 \ldots A_N
L_1 R_1
L_2 R_2
\vdots
L_M R_M
  • 1 行目には、畑の数 N と区画の数 M がスペース区切りで与えられる。
  • 2 行目には、各畑の収穫量 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
  • 続く M 行にわたって、各区画の構成が与えられる。
  • 2 + j 行目 (1 \leq j \leq M) には、j 番目の区画を構成する畑の番号の範囲 L_jR_j がスペース区切りで与えられる。この区画は畑 L_j から畑 R_j までで構成される。

出力

M 個の区画の生産力の最大値から最小値を引いた値を 1 行で出力せよ。


入力例 1

5 3
10 20 30 40 50
1 2
3 3
4 5

出力例 1

60

入力例 2

8 4
5 15 10 20 25 5 30 10
1 3
4 4
5 6
7 8

出力例 2

20

入力例 3

12 5
100 200 300 150 250 50 400 100 200 300 150 100
1 2
3 5
6 8
9 10
11 12

出力例 3

450

Score : 300 pts

Problem Statement

Takahashi manages a vast farm. The farm has N fields arranged in a row from east to west, and each field is numbered from 1 to N in order from west to east. The harvest yield of field i (1 \leq i \leq N) is A_i.

To improve the efficiency of farm management, Takahashi has decided to divide these fields into M sections. Each section consists of consecutively numbered fields. Specifically, the j-th section (1 \leq j \leq M) consists of consecutive fields from field L_j to field R_j (L_j \leq R_j). Every field belongs to exactly one section, and no field is shared between sections. Note that the sections are not necessarily given in order from west to east in the input.

The productivity of each section is defined as the sum of the harvest yields of all fields contained in that section. That is, the productivity of the j-th section is A_{L_j} + A_{L_j+1} + \cdots + A_{R_j}.

Takahashi wants to understand the variation in productivity among the sections. This is because if the difference in productivity is too large, it becomes difficult to allocate workers and distribute resources.

For the given partitioning, find the value obtained by subtracting the minimum productivity from the maximum productivity among the M sections.

Constraints

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq M)
  • The M sections partition fields 1 through N completely without overlap or omission
  • The sections are not necessarily given in order from west to east in the input
  • All input values are integers

Input

N M
A_1 A_2 \ldots A_N
L_1 R_1
L_2 R_2
\vdots
L_M R_M
  • The first line contains the number of fields N and the number of sections M, separated by a space.
  • The second line contains the harvest yields A_1, A_2, \ldots, A_N of each field, separated by spaces.
  • The following M lines describe the composition of each section.
  • The (2 + j)-th line (1 \leq j \leq M) contains the range of field numbers L_j and R_j that make up the j-th section, separated by a space. This section consists of fields from field L_j to field R_j.

Output

Output in one line the value obtained by subtracting the minimum productivity from the maximum productivity among the M sections.


Sample Input 1

5 3
10 20 30 40 50
1 2
3 3
4 5

Sample Output 1

60

Sample Input 2

8 4
5 15 10 20 25 5 30 10
1 3
4 4
5 6
7 8

Sample Output 2

20

Sample Input 3

12 5
100 200 300 150 250 50 400 100 200 300 150 100
1 2
3 5
6 8
9 10
11 12

Sample Output 3

450
C - チームの旗の色

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

配点 : 366

問題文

高橋君は N 人の選手が所属するスポーツクラブの監督です。最初、各選手はそれぞれ 1 人だけからなる別々のチームに所属しています。各チームは旗の色を 1 つ持つことができますが、初期状態ではどのチームにも旗の色は設定されていません。

高橋君は M 回の操作を順に行います。

i 回目の操作では、次のことを行います。

  1. 選手 U_i が所属するチームと選手 V_i が所属するチームを合併して 1 つのチームにします。ただし、2 人が既に同じチームに所属している場合は、合併は行わずそのチームをそのまま対象とします。
  2. 合併後のチームの旗の色を C_i に設定します。以前の旗の色は上書きされます。

すべての操作が終わった後、旗の色が設定されているチームに注目します。それらのチームの旗の色として現れる、異なる色の種類数を求めてください。一度も操作の対象にならなかったチーム(旗の色が未設定のチーム)は無視します。

制約

  • 1 \leq N \leq 200000
  • 1 \leq M \leq 150000
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • 1 \leq C_i \leq 10^9
  • 入力はすべて整数である。

入力

N M
U_1 V_1 C_1
U_2 V_2 C_2
:
U_M V_M C_M
  • 1 行目には、選手の人数を表す整数 N と、操作の回数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目から M 行には、i 番目の操作の情報として、選手番号 U_i 、選手番号 V_i 、旗の色 C_i がスペース区切りで与えられる。
  • 選手の番号は 1 から N までの整数である。
  • C_i はその操作で設定される旗の色を表す整数である。

出力

すべての操作終了後、旗の色が設定されているチームの旗の色として現れる、異なる色の種類数を 1 行で出力してください。


入力例 1

4 3
1 2 10
3 4 20
2 3 10

出力例 1

1

入力例 2

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

出力例 2

2

入力例 3

10 9
1 2 10
2 3 20
4 5 10
6 7 30
8 9 30
1 3 40
5 6 20
7 4 50
9 10 50

出力例 3

2

入力例 4

15 16
1 2 1
3 4 2
5 6 3
7 8 4
9 10 5
11 12 6
13 14 7
2 3 8
6 7 9
10 11 5
1 4 1
8 5 3
12 9 6
4 5 8
14 15 7
1 8 2

出力例 4

3

入力例 5

2 1
1 2 1000000000

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi is the coach of a sports club with N players. Initially, each player belongs to a separate team consisting of only themselves. Each team can have one flag color, but in the initial state, no team has a flag color assigned.

Takahashi performs M operations in order.

In the i-th operation, the following is done:

  1. The team to which player U_i belongs and the team to which player V_i belongs are merged into a single team. However, if the two players already belong to the same team, no merge is performed and that team is used as the target.
  2. The flag color of the resulting team is set to C_i. Any previous flag color is overwritten.

After all operations are completed, consider the teams that have a flag color assigned. Determine the number of distinct colors that appear as flag colors among those teams. Teams that were never the target of any operation (teams with no flag color assigned) are ignored.

Constraints

  • 1 \leq N \leq 200000
  • 1 \leq M \leq 150000
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • 1 \leq C_i \leq 10^9
  • All input values are integers.

Input

N M
U_1 V_1 C_1
U_2 V_2 C_2
:
U_M V_M C_M
  • The first line contains an integer N representing the number of players and an integer M representing the number of operations, separated by a space.
  • The following M lines each contain the information for the i-th operation: player number U_i, player number V_i, and flag color C_i, separated by spaces.
  • Player numbers are integers from 1 to N.
  • C_i is an integer representing the flag color set by that operation.

Output

Print on a single line the number of distinct colors that appear as flag colors among the teams that have a flag color assigned after all operations are completed.


Sample Input 1

4 3
1 2 10
3 4 20
2 3 10

Sample Output 1

1

Sample Input 2

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

Sample Output 2

2

Sample Input 3

10 9
1 2 10
2 3 20
4 5 10
6 7 30
8 9 30
1 3 40
5 6 20
7 4 50
9 10 50

Sample Output 3

2

Sample Input 4

15 16
1 2 1
3 4 2
5 6 3
7 8 4
9 10 5
11 12 6
13 14 7
2 3 8
6 7 9
10 11 5
1 4 1
8 5 3
12 9 6
4 5 8
14 15 7
1 8 2

Sample Output 4

3

Sample Input 5

2 1
1 2 1000000000

Sample Output 5

1
D - 混雑する交差点

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

配点 : 400

問題文

高橋君は、N 個の交差点と M 本の双方向の道路からなる街に住んでいます。交差点には 1 から N までの番号が付けられており、i 番目の道路は交差点 u_i と交差点 v_i を結んでいます。なお、自分自身を結ぶ道路(自己ループ)は存在せず、同じ 2 つの交差点を結ぶ道路も高々 1 本です。

各交差点について、その交差点に接続する道路の本数を、その交差点の混雑度と呼びます。

高橋君は交差点 1 から交差点 N へ向かいたいのですが、混雑した交差点が苦手です。

高橋君の経路は、交差点の列 p_0, p_1, p_2, \ldots, p_LL \geq 1)として表されます。ただし、p_0 = 1p_L = N であり、各 j = 0, 1, \ldots, L-1 について交差点 p_j と交差点 p_{j+1} を結ぶ道路が存在しなければなりません。同じ交差点や同じ道路を複数回通ることも許されます。

この経路でかかる時間(分)は、以下の 2 つの合計です。

  1. 道路の移動時間: 経路中で道路を通る回数、すなわち L 分。各道路を通るのにちょうど 1 分かかり、同じ道路を複数回通る場合はその回数分だけ数えます。
  2. 交差点の通過時間: 経路の最初の要素 p_0 と最後の要素 p_L を除いた、p_1, p_2, \ldots, p_{L-1} のそれぞれについて、その交差点の混雑度が K 以上であれば追加で 1 分かかります。これは信号待ちや人混みによるものです。同じ交差点が p_1, p_2, \ldots, p_{L-1} の中に複数回現れる場合は、現れるたびにそれぞれ判定し、該当すればその回数分だけ加算します。

通過時間の判定対象外となるのは、あくまで経路の最初の要素 p_0 と最後の要素 p_L としての出現のみです。たとえば交差点 1 や交差点 N であっても、p_1, p_2, \ldots, p_{L-1} のいずれかとして現れた場合は通過時間の対象となります。

高橋君が交差点 1 から交差点 N へ移動するとき、かかる時間の最小値を求めてください。交差点 1 から交差点 N へたどり着ける経路が存在しない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • 同じ 2 つの交差点を結ぶ道路は高々 1 本である
  • 入力はすべて整数である

入力

N M K
u_1 v_1
u_2 v_2
\vdots
u_M v_M
  • 1 行目には、交差点の数 N、道路の数 M、混雑度の閾値 K が、スペース区切りで与えられる。
  • 続く M 行のうち i 行目には、i 番目の道路が結ぶ 2 つの交差点の番号 u_i, v_i が、スペース区切りで与えられる。

出力

高橋君が交差点 1 から交差点 N へ移動するのにかかる最小の時間(分)を 1 行で出力せよ。たどり着ける経路が存在しない場合は -1 を出力せよ。


入力例 1

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

出力例 1

3

入力例 2

4 2 2
1 2
3 4

出力例 2

-1

入力例 3

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

出力例 3

5

Score : 400 pts

Problem Statement

Takahashi lives in a city consisting of N intersections and M bidirectional roads. The intersections are numbered from 1 to N, and the i-th road connects intersection u_i and intersection v_i. There are no self-loops (roads connecting an intersection to itself), and there is at most one road connecting any pair of intersections.

For each intersection, the number of roads connected to that intersection is called the congestion level of that intersection.

Takahashi wants to travel from intersection 1 to intersection N, but he dislikes congested intersections.

Takahashi's route is represented as a sequence of intersections p_0, p_1, p_2, \ldots, p_L (L \geq 1), where p_0 = 1, p_L = N, and for each j = 0, 1, \ldots, L-1, there must exist a road connecting intersection p_j and intersection p_{j+1}. It is allowed to pass through the same intersection or the same road multiple times.

The time (in minutes) required for this route is the sum of the following two components:

  1. Road travel time: The number of times roads are traversed along the route, namely L minutes. Each road traversal takes exactly 1 minute, and if the same road is traversed multiple times, each traversal is counted.
  2. Intersection passing time: For each of p_1, p_2, \ldots, p_{L-1} (excluding the first element p_0 and the last element p_L of the route), if the congestion level of that intersection is K or more, an additional 1 minute is incurred. This is due to waiting at traffic lights or navigating through crowds. If the same intersection appears multiple times among p_1, p_2, \ldots, p_{L-1}, the check is performed for each occurrence, and the additional time is added for each qualifying occurrence.

Only the occurrences as the first element p_0 and the last element p_L of the route are exempt from the passing time check. For example, even intersections 1 or N, if they appear as any of p_1, p_2, \ldots, p_{L-1}, are subject to the passing time check.

Find the minimum time required for Takahashi to travel from intersection 1 to intersection N. If no route from intersection 1 to intersection N exists, output -1.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • There is at most one road connecting any pair of intersections
  • All input values are integers

Input

N M K
u_1 v_1
u_2 v_2
\vdots
u_M v_M
  • The first line contains the number of intersections N, the number of roads M, and the congestion level threshold K, separated by spaces.
  • The i-th of the following M lines contains the numbers u_i and v_i of the two intersections connected by the i-th road, separated by spaces.

Output

Output in one line the minimum time (in minutes) required for Takahashi to travel from intersection 1 to intersection N. If no such route exists, output -1.


Sample Input 1

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

Sample Output 1

3

Sample Input 2

4 2 2
1 2
3 4

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

5
E - 写真撮影スポットの選定

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

配点 : 466

問題文

高橋君は N \times N のマス目で表される観光エリアの中から、K \times K の正方形の区域を一つ選んで写真撮影ツアーを企画しようとしています。上から i 行目、左から j 列目のマスには景観スコア A_{i,j} が定められています。

ここで K \times K の区域とは、整数 r, c1 \leq r \leq N-K+1, 1 \leq c \leq N-K+1)を選んだとき、上から r 行目〜 r+K-1 行目かつ左から c 列目〜 c+K-1 列目に含まれる K \times K 個のマス全体を指します。

ライバルのツアー会社を経営する青木君は、高橋君が選んだ区域を知った上で、その区域内からちょうど一つのマスを選び、そのマスの景観スコアを 0 に変更します(他のマスの景観スコアは変化しません)。青木君はこの操作を必ず行わなければならず、スコアが既に 0 のマスを選ぶこともできます。青木君は、操作後の区域内の景観スコアの合計を最小化するようにマスを選びます。

青木君の操作後における区域内の K \times K 個のマスの景観スコアの合計を 満足度 と呼びます。高橋君は青木君の妨害を考慮した上で、満足度が最大となるように区域を選びます。このときの満足度の最大値を求めてください。

制約

  • 1 \leq K \leq N \leq 1000
  • 0 \leq A_{i,j} \leq 10^9
  • 入力はすべて整数である。

入力

入力は以下の形式で与えられます。

N K
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
  • 1 行目には、エリアの一辺の大きさを表す整数 N と、選ぶ区域の一辺の大きさを表す整数 K が、スペース区切りで与えられる。
  • 続く N 行のうち i 行目(1 \leq i \leq N)には N 個の整数 A_{i,1}, A_{i,2}, \ldots, A_{i,N} がスペース区切りで与えられる。A_{i,j} は上から i 行目、左から j 列目のマスの景観スコアを表す。

出力

青木君が最適に妨害を行った場合における、高橋君が得られる満足度の最大値を整数で一行に出力してください。


入力例 1

3 2
1 2 3
4 5 6
7 8 9

出力例 1

19

入力例 2

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

出力例 2

43

入力例 3

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

出力例 3

37

入力例 4

8 4
12 7 3 15 8 6 14 2
9 11 5 4 13 10 1 16
6 14 2 12 7 15 3 8
10 1 16 9 5 11 4 13
8 13 7 3 14 2 12 6
4 15 10 1 16 9 5 11
14 2 12 6 8 13 7 3
5 11 4 16 10 1 15 9

出力例 4

132

入力例 5

1 1
1000000000

出力例 5

0

Score : 466 pts

Problem Statement

Takahashi is planning a photo tour by selecting a single K \times K square area from a sightseeing region represented as an N \times N grid. The cell in the i-th row from the top and the j-th column from the left has a scenery score of A_{i,j}.

Here, a K \times K area refers to the set of all K \times K cells from row r to row r+K-1 and from column c to column c+K-1, where integers r, c are chosen such that 1 \leq r \leq N-K+1 and 1 \leq c \leq N-K+1.

Aoki, who runs a rival tour company, learns which area Takahashi has chosen, and then selects exactly one cell within that area to change its scenery score to 0 (the scenery scores of all other cells remain unchanged). Aoki must perform this operation, and may choose a cell whose score is already 0. Aoki selects the cell so as to minimize the total scenery score within the area after the operation.

The total scenery score of the K \times K cells in the area after Aoki's operation is called the satisfaction. Takahashi, taking Aoki's sabotage into account, chooses the area to maximize the satisfaction. Find the maximum value of the satisfaction.

Constraints

  • 1 \leq K \leq N \leq 1000
  • 0 \leq A_{i,j} \leq 10^9
  • All input values are integers.

Input

The input is given in the following format:

N K
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
  • The first line contains two integers separated by a space: N, the side length of the area, and K, the side length of the chosen square.
  • Each of the following N lines, where the i-th line (1 \leq i \leq N), contains N integers A_{i,1}, A_{i,2}, \ldots, A_{i,N} separated by spaces. A_{i,j} represents the scenery score of the cell in the i-th row from the top and the j-th column from the left.

Output

Print, as a single integer on one line, the maximum satisfaction Takahashi can achieve when Aoki sabotages optimally.


Sample Input 1

3 2
1 2 3
4 5 6
7 8 9

Sample Output 1

19

Sample Input 2

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

Sample Output 2

43

Sample Input 3

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

Sample Output 3

37

Sample Input 4

8 4
12 7 3 15 8 6 14 2
9 11 5 4 13 10 1 16
6 14 2 12 7 15 3 8
10 1 16 9 5 11 4 13
8 13 7 3 14 2 12 6
4 15 10 1 16 9 5 11
14 2 12 6 8 13 7 3
5 11 4 16 10 1 15 9

Sample Output 4

132

Sample Input 5

1 1
1000000000

Sample Output 5

0