A - Warehouse Cargo Organization

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 266

問題文

高橋君は物流会社の倉庫で荷物の管理を担当しています。今日は N 個の荷物が入荷する予定でしたが、そのうち M 個の荷物は発送元でキャンセルされ、届かないことになりました。

倉庫では、各荷物に 1 から N までの管理番号が割り振られています。管理番号 i の荷物の重さは T_i グラムです(1 \leq i \leq N)。キャンセルされた M 個の荷物の管理番号は D_1, D_2, \ldots, D_M で与えられます。なお、同じ荷物が重複してキャンセルされることはありません。

高橋君は届いた荷物を棚に収納するため、重さを「ユニット」という単位に換算して記録することにしました。1 ユニットは K グラムに相当します。各荷物のユニット数は、その荷物の重さ(グラム)を K で割り小数点以下を切り捨てた値として定義します。すなわち、管理番号 i の荷物のユニット数は \lfloor T_i / K \rfloor です。

キャンセルされずに届いた荷物(管理番号が D_1, D_2, \ldots, D_M のいずれでもない荷物)のそれぞれについてユニット数を求め、それらの総和を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq N
  • 1 \leq K \leq 10^9
  • 1 \leq T_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq D_j \leq N (1 \leq j \leq M)
  • D_1, D_2, \ldots, D_M はすべて異なる
  • 入力はすべて整数

入力

N M K
T_1 T_2 \cdots T_N
D_1 D_2 \cdots D_M
  • 1 行目には、入荷予定の荷物の個数 N、キャンセルされた荷物の個数 M1 ユニットに相当するグラム数 K が、スペース区切りで与えられる。
  • 2 行目には、管理番号 i の荷物の重さ(グラム)を表す T_i が、N 個スペース区切りで与えられる。
  • 3 行目には、キャンセルされた荷物の管理番号 D_j が、M 個スペース区切りで与えられる。ただし M = 0 のとき、3 行目は空行ではなく与えられない(入力は 2 行からなる)。

出力

キャンセルされずに届いた各荷物について \lfloor T_i / K \rfloor を求め、それらの総和を 1 行で出力してください。


入力例 1

5 2 3
10 7 8 5 6
2 4

出力例 1

7

入力例 2

4 1 5
3 12 7 20
3

出力例 2

6

入力例 3

10 3 100
250 99 450 1000 50 333 678 812 999 10
2 5 9

出力例 3

33

入力例 4

15 5 7
100 49 35 77 64 21 88 53 42 99 15 70 28 56 91
1 5 8 12 15

出力例 4

72

入力例 5

1 0 1000000000
999999999

出力例 5

0

Score : 266 pts

Problem Statement

Takahashi is in charge of managing packages at a logistics company's warehouse. Today, N packages were scheduled to arrive, but M of them were cancelled by the sender and will not be delivered.

In the warehouse, each package is assigned a management number from 1 to N. The weight of the package with management number i is T_i grams (1 \leq i \leq N). The management numbers of the M cancelled packages are given as D_1, D_2, \ldots, D_M. Note that the same package is never cancelled more than once.

To store the delivered packages on shelves, Takahashi decided to convert the weights into a unit called "units" and record them. 1 unit corresponds to K grams. The number of units for each package is defined as the weight (in grams) of that package divided by K, rounded down to the nearest integer. That is, the number of units for the package with management number i is \lfloor T_i / K \rfloor.

For each package that was not cancelled and actually arrived (i.e., packages whose management numbers are not any of D_1, D_2, \ldots, D_M), compute the number of units and output their total sum.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq N
  • 1 \leq K \leq 10^9
  • 1 \leq T_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq D_j \leq N (1 \leq j \leq M)
  • D_1, D_2, \ldots, D_M are all distinct
  • All input values are integers

Input

N M K
T_1 T_2 \cdots T_N
D_1 D_2 \cdots D_M
  • The first line contains the number of scheduled packages N, the number of cancelled packages M, and the number of grams per unit K, separated by spaces.
  • The second line contains N space-separated values T_i, representing the weight (in grams) of the package with management number i.
  • The third line contains M space-separated values D_j, representing the management numbers of the cancelled packages. However, when M = 0, the third line is not given (the input consists of only 2 lines).

Output

For each package that was not cancelled and actually arrived, compute \lfloor T_i / K \rfloor, and output their total sum on a single line.


Sample Input 1

5 2 3
10 7 8 5 6
2 4

Sample Output 1

7

Sample Input 2

4 1 5
3 12 7 20
3

Sample Output 2

6

Sample Input 3

10 3 100
250 99 450 1000 50 333 678 812 999 10
2 5 9

Sample Output 3

33

Sample Input 4

15 5 7
100 49 35 77 64 21 88 53 42 99 15 70 28 56 91
1 5 8 12 15

Sample Output 4

72

Sample Input 5

1 0 1000000000
999999999

Sample Output 5

0
B - Team Formation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は、プログラミングコンテストに出場するチームの監督をしています。

今回の大会では、N 人の選手が候補として集まりました。各選手には 1 から N までの番号が付けられており、選手 i の実力値は A_i です。

大会のルールとして、チームはちょうど K 人で編成する必要があります。基本的には実力値の高い順にメンバーを選びたいのですが、一つ特別な事情があります。

青木君は高橋君の幼馴染であり、候補選手の一人として参加しています。青木君の選手番号は T です。高橋君は青木君を必ずチームに入れると約束しているため、以下のようなメンバー選出方式を取ることにしました:

  1. まず、青木君(選手 T)を必ずチームメンバーに含める。
  2. 残りの K - 1 人は、青木君を除いた N - 1 人の候補選手の中から、実力値の高い順に K - 1 人を選ぶ。ただし K = 1 の場合、青木君のみでチームを編成する。

このルールで選出された K 人の実力値の合計を求めてください。

なお、実力値が等しい選手が複数いて選出の境界にいる場合、どの選手を選んでも実力値の合計は同じになるため、答えは一意に定まります。

制約

  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq T \leq N
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数

入力

N K T
A_1 A_2 \ldots A_N
  • 1 行目には、候補選手の人数を表す整数 N、チームの人数を表す整数 K、青木君の選手番号を表す整数 T が、スペース区切りで与えられる。
  • 2 行目には、各選手の実力値を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

選出された K 人の実力値の合計を 1 行で出力してください。


入力例 1

5 3 2
10 5 8 3 7

出力例 1

23

入力例 2

7 4 5
100 250 180 90 120 300 75

出力例 2

850

入力例 3

10 5 7
500000000 300000000 800000000 150000000 600000000 450000000 200000000 900000000 350000000 700000000

出力例 3

3200000000

Score : 300 pts

Problem Statement

Takahashi is the coach of a team competing in a programming contest.

For this tournament, N players have gathered as candidates. Each player is assigned a number from 1 to N, and the skill value of player i is A_i.

According to the tournament rules, a team must consist of exactly K members. Basically, Takahashi wants to select members in descending order of skill value, but there is one special circumstance.

Aoki is Takahashi's childhood friend and is participating as one of the candidate players. Aoki's player number is T. Since Takahashi has promised to include Aoki on the team, he decides to use the following member selection method:

  1. First, Aoki (player T) is always included as a team member.
  2. The remaining K - 1 members are selected from the N - 1 candidate players (excluding Aoki) by choosing the K - 1 players with the highest skill values. However, if K = 1, the team consists of only Aoki.

Find the total skill value of the K members selected under this rule.

Note that if there are multiple players with equal skill values at the selection boundary, the total skill value is the same regardless of which players are chosen, so the answer is uniquely determined.

Constraints

  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq T \leq N
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers

Input

N K T
A_1 A_2 \ldots A_N
  • The first line contains three space-separated integers: N representing the number of candidate players, K representing the team size, and T representing Aoki's player number.
  • The second line contains space-separated integers A_1, A_2, \ldots, A_N representing the skill values of each player.

Output

Print the total skill value of the selected K members on a single line.


Sample Input 1

5 3 2
10 5 8 3 7

Sample Output 1

23

Sample Input 2

7 4 5
100 250 180 90 120 300 75

Sample Output 2

850

Sample Input 3

10 5 7
500000000 300000000 800000000 150000000 600000000 450000000 200000000 900000000 350000000 700000000

Sample Output 3

3200000000
C - Dissolution of the Department

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

高橋君は大企業の組織管理を担当しています。この会社には N 個の部署があり、各部署には 1 から N までの番号が付けられています。番号 1 は会社全体を統括する本社です。

会社の組織構造は根付き木として表されており、番号 1 の本社が根です。番号 i2 \leq i \leq N)の部署の親は番号 P_i の部署です。すなわち、番号 i の部署は番号 P_i の部署の直属の下部組織です。

ある日、経営陣は組織のスリム化を図るため、番号 K の部署を解体することを決定しました。解体では、番号 K の部署自身と、その子孫にあたるすべての部署が取り除かれます。ここで、番号 K の部署の子孫とは、根付き木において番号 K の部署の子、子の子、…と再帰的にたどれるすべての部署を指します(番号 K の部署自身は子孫には含みません)。

解体後に会社に残る部署の数を求めてください。K = 1 の場合は会社の全部署が解体の対象となるため、答えが 0 になることに注意してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq P_i < i2 \leq i \leq N
  • 入力はすべて整数である

入力

N K
P_2 P_3 \cdots P_N
  • 1 行目には、部署の総数 N と、解体の起点となる部署の番号 K が、スペース区切りで与えられる。
  • 2 行目には、番号 2 から N までの各部署の親の番号 P_2, P_3, \ldots, P_N が、スペース区切りで与えられる。ただし N = 1 のときは 2 行目は存在せず、入力は 1 行のみである。

出力

番号 K の部署とその子孫にあたるすべての部署を解体した後に残る部署の数を 1 行で出力してください。


入力例 1

5 3
1 1 3 3

出力例 1

2

入力例 2

8 2
1 1 2 2 3 5 5

出力例 2

3

入力例 3

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

出力例 3

9

Score : 366 pts

Problem Statement

Takahashi is in charge of organizational management at a large company. The company has N departments, each numbered from 1 to N. Department 1 is the headquarters that oversees the entire company.

The company's organizational structure is represented as a rooted tree, with department 1 (headquarters) as the root. The parent of department i (2 \leq i \leq N) is department P_i. In other words, department i is a direct subdivision of department P_i.

One day, the management decided to dismantle department K in order to streamline the organization. The dismantling removes department K itself and all departments that are its descendants. Here, the descendants of department K refer to all departments that can be reached by recursively following children of department K in the rooted tree — that is, children, children of children, and so on (department K itself is not included in its descendants).

Determine the number of departments remaining in the company after the dismantling. Note that if K = 1, all departments in the company are subject to dismantling, so the answer is 0.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq P_i < i (2 \leq i \leq N)
  • All input values are integers.

Input

N K
P_2 P_3 \cdots P_N
  • The first line contains the total number of departments N and the number K of the department to be dismantled, separated by a space.
  • The second line contains the parent numbers P_2, P_3, \ldots, P_N for departments 2 through N, separated by spaces. However, when N = 1, the second line does not exist and the input consists of only one line.

Output

Print on one line the number of departments remaining after dismantling department K and all of its descendant departments.


Sample Input 1

5 3
1 1 3 3

Sample Output 1

2

Sample Input 2

8 2
1 1 2 2 3 5 5

Sample Output 2

3

Sample Input 3

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

Sample Output 3

9
D - Meeting Room Reservation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君はある会社の会議室管理を担当しています。この会議室では、1 日を T 個の時間枠(時間枠 1, 2, \ldots, T)に分けて運用しており、各時間枠には同時に 1 件の会議しか割り当てることができません。

ある日、N 件の会議室利用の申請が届きました。

i 番目(1 \leq i \leq N)の申請は、時間枠 L_i から時間枠 R_i まで(両端を含む)の連続した時間枠に会議室を使用したいというものです。i 番目の申請を承認した場合、会社は V_i 円の利益を得ることができます。

高橋君は、これらの N 件の申請の中から 0 件以上を選んで承認します。ただし、同じ申請を複数回承認することはできず、承認した申請のうちどの 2 件をとっても、使用する時間枠が重なってはいけません。ここで、申請 i と申請 ji \neq j)の時間枠が重なっているとは、閉区間 [L_i, R_i][L_j, R_j] に共通する整数が存在すること、すなわち L_i \leq R_j かつ L_j \leq R_i が成り立つことを指します。

承認する申請の選び方を最適にしたときの、承認した申請の利益の合計(すなわち、承認した申請の V_i の総和)の最大値を求めてください。1 件も承認しない場合の利益の合計は 0 円とします。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq T1 \leq i \leq N
  • 1 \leq V_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数である。

入力

N T
L_1 R_1 V_1
L_2 R_2 V_2
\vdots
L_N R_N V_N
  • 1 行目には、申請の件数 N と時間枠の総数 T がスペース区切りで与えられる。
  • 続く N 行のうち i 行目(1 \leq i \leq N)には、i 番目の申請の開始時間枠 L_i、終了時間枠 R_i、および利益 V_i がスペース区切りで与えられる。

出力

利益の合計の最大値を 1 行で出力せよ。


入力例 1

3 5
1 3 5
2 4 6
4 5 3

出力例 1

8

入力例 2

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

出力例 2

7

入力例 3

10 20
1 5 10
3 7 15
6 10 12
8 12 8
11 15 20
1 3 7
4 6 9
7 9 11
10 14 14
15 20 18

出力例 3

59

入力例 4

20 50
1 5 100
3 8 150
6 10 120
9 15 200
12 18 180
16 20 90
19 25 160
22 28 140
26 30 110
29 35 170
32 38 130
36 40 190
1 3 80
4 7 95
8 11 105
14 17 115
21 24 85
30 34 145
37 42 175
43 50 250

出力例 4

1230

入力例 5

1 1
1 1 1000000000

出力例 5

1000000000

Score : 400 pts

Problem Statement

Takahashi is in charge of managing a meeting room at a company. This meeting room operates by dividing each day into T time slots (time slots 1, 2, \ldots, T), and only 1 meeting can be assigned to each time slot at a time.

One day, N requests for meeting room usage were received.

The i-th request (1 \leq i \leq N) is to use the meeting room for consecutive time slots from time slot L_i to time slot R_i (inclusive). If the i-th request is approved, the company earns a profit of V_i yen.

Takahashi will select and approve 0 or more of these N requests. However, the same request cannot be approved more than once, and for any 2 approved requests, their time slots must not overlap. Here, the time slots of request i and request j (i \neq j) overlap means that there exists a common integer in the closed intervals [L_i, R_i] and [L_j, R_j], that is, L_i \leq R_j and L_j \leq R_i both hold.

Find the maximum total profit of the approved requests (i.e., the maximum sum of V_i over all approved requests) when the selection of requests to approve is optimized. If no requests are approved, the total profit is 0 yen.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq T (1 \leq i \leq N)
  • 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

N T
L_1 R_1 V_1
L_2 R_2 V_2
\vdots
L_N R_N V_N
  • The first line contains the number of requests N and the total number of time slots T, separated by a space.
  • The following N lines, where the i-th line (1 \leq i \leq N), contains the starting time slot L_i, ending time slot R_i, and profit V_i of the i-th request, separated by spaces.

Output

Output the maximum total profit in a single line.


Sample Input 1

3 5
1 3 5
2 4 6
4 5 3

Sample Output 1

8

Sample Input 2

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

Sample Output 2

7

Sample Input 3

10 20
1 5 10
3 7 15
6 10 12
8 12 8
11 15 20
1 3 7
4 6 9
7 9 11
10 14 14
15 20 18

Sample Output 3

59

Sample Input 4

20 50
1 5 100
3 8 150
6 10 120
9 15 200
12 18 180
16 20 90
19 25 160
22 28 140
26 30 110
29 35 170
32 38 130
36 40 190
1 3 80
4 7 95
8 11 105
14 17 115
21 24 85
30 34 145
37 42 175
43 50 250

Sample Output 4

1230

Sample Input 5

1 1
1 1 1000000000

Sample Output 5

1000000000
E - Monitoring Temperature Fluctuations

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466

問題文

高橋君は気象データの分析を担当しています。彼の仕事は、複数の観測地点の気温データを調べ、気温変動が激しい地点を報告することです。

観測地点は N 箇所あり、それぞれの観測地点 i1 \leq i \leq N )では、過去 M 日間の日平均気温 S_{i,1}, S_{i,2}, \ldots, S_{i,M} が記録されています。

高橋君は、各観測地点について「気温変動スコア」を算出することにしました。観測地点 i の気温変動スコアは、以下のように定義されます:

  • 過去 M 日間の気温データの中から、連続する K 日間を選ぶすべての方法を考える。それぞれの連続 K 日間について、その区間内の気温の最大値と最小値の差を求める。
  • それらの差のうち、最大のものを観測地点 i の気温変動スコアとする。

高橋君は、気温変動スコアが閾値 T 以上である観測地点の数を報告する必要があります。

気温変動スコアが T 以上である観測地点の数を求めてください。

制約

  • 1 \leq N \leq 500
  • 1 \leq K \leq M \leq 10^5
  • N \times M \leq 2 \times 10^6
  • -10^9 \leq S_{i,j} \leq 10^9
  • 0 \leq T \leq 2 \times 10^9
  • 入力はすべて整数である。

入力

N M K T
S_{1,1} S_{1,2} \ldots S_{1,M}
S_{2,1} S_{2,2} \ldots S_{2,M}
\vdots
S_{N,1} S_{N,2} \ldots S_{N,M}
  • 1 行目には、観測地点の数を表す N 、日数を表す M 、連続日数を表す K 、閾値を表す T が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目には、各観測地点の気温データが与えられる。
  • 1 + i 行目では、観測地点 iM 日間の気温 S_{i,1}, S_{i,2}, \ldots, S_{i,M} がスペース区切りで与えられる。

出力

気温変動スコアが T 以上である観測地点の数を 1 行で出力せよ。


入力例 1

3 5 3 5
1 3 4 8 2
10 11 12 13 14
0 -5 -1 -2 1

出力例 1

2

入力例 2

2 4 2 10
1 2 3 4
-5 -4 -6 -5

出力例 2

0

入力例 3

5 10 4 12
1 2 3 4 5 6 7 8 9 10
10 -5 8 7 6 5 4 3 2 1
-10 -8 -6 -4 -2 0 2 4 6 8
100 90 80 70 60 50 40 30 20 10
5 5 20 5 5 5 -10 5 5 5

出力例 3

3

入力例 4

8 15 5 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
30 25 20 15 10 5 0 -5 -10 -15 -20 -25 -30 -35 -40
-5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
100 80 100 80 100 80 100 80 100 80 100 80 100 80 100
1 50 2 49 3 48 4 47 5 46 6 45 7 44 8
-100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40
7 8 6 9 5 10 4 11 3 12 2 13 1 14 0
1000000000 999999990 999999980 999999970 999999960 -1000000000 -999999990 -999999980 -999999970 -999999960 0 10 20 30 40

出力例 4

5

入力例 5

1 1 1 0
-1000000000

出力例 5

1

Score : 466 pts

Problem Statement

Takahashi is in charge of analyzing weather data. His job is to examine temperature data from multiple observation stations and report stations with severe temperature fluctuations.

There are N observation stations. For each observation station i (1 \leq i \leq N), the daily average temperatures for the past M days, S_{i,1}, S_{i,2}, \ldots, S_{i,M}, are recorded.

Takahashi has decided to calculate a "temperature fluctuation score" for each observation station. The temperature fluctuation score for station i is defined as follows:

  • Consider all possible ways to choose K consecutive days from the past M days of temperature data. For each interval of K consecutive days, find the difference between the maximum and minimum temperatures within that interval.
  • The maximum of these differences is defined as the temperature fluctuation score of station i.

Takahashi needs to report the number of observation stations whose temperature fluctuation score is at least the threshold T.

Find the number of observation stations with a temperature fluctuation score of T or higher.

Constraints

  • 1 \leq N \leq 500
  • 1 \leq K \leq M \leq 10^5
  • N \times M \leq 2 \times 10^6
  • -10^9 \leq S_{i,j} \leq 10^9
  • 0 \leq T \leq 2 \times 10^9
  • All input values are integers.

Input

N M K T
S_{1,1} S_{1,2} \ldots S_{1,M}
S_{2,1} S_{2,2} \ldots S_{2,M}
\vdots
S_{N,1} S_{N,2} \ldots S_{N,M}
  • The first line contains N (the number of observation stations), M (the number of days), K (the number of consecutive days), and T (the threshold), separated by spaces.
  • The 2-nd to (N + 1)-th lines contain the temperature data for each observation station.
  • The (1 + i)-th line contains the temperatures of station i for M days, S_{i,1}, S_{i,2}, \ldots, S_{i,M}, separated by spaces.

Output

Print the number of observation stations with a temperature fluctuation score of at least T in a single line.


Sample Input 1

3 5 3 5
1 3 4 8 2
10 11 12 13 14
0 -5 -1 -2 1

Sample Output 1

2

Sample Input 2

2 4 2 10
1 2 3 4
-5 -4 -6 -5

Sample Output 2

0

Sample Input 3

5 10 4 12
1 2 3 4 5 6 7 8 9 10
10 -5 8 7 6 5 4 3 2 1
-10 -8 -6 -4 -2 0 2 4 6 8
100 90 80 70 60 50 40 30 20 10
5 5 20 5 5 5 -10 5 5 5

Sample Output 3

3

Sample Input 4

8 15 5 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
30 25 20 15 10 5 0 -5 -10 -15 -20 -25 -30 -35 -40
-5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
100 80 100 80 100 80 100 80 100 80 100 80 100 80 100
1 50 2 49 3 48 4 47 5 46 6 45 7 44 8
-100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40
7 8 6 9 5 10 4 11 3 12 2 13 1 14 0
1000000000 999999990 999999980 999999970 999999960 -1000000000 -999999990 -999999980 -999999970 -999999960 0 10 20 30 40

Sample Output 4

5

Sample Input 5

1 1 1 0
-1000000000

Sample Output 5

1