A - Bomb Disposal Squad

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 266

問題文

高橋君は爆弾処理班のリーダーです。現在、N 個の建物が一列に並んでおり、左から順に 1, 2, \ldots, N の番号が付けられています。各建物 i は耐久値 H_i を持っています。

不幸なことに、この地域で M 回の爆発が発生します。j 回目の爆発は、建物 T_j を中心として威力 D_j で発生します。なお、同じ建物が複数回の爆発の中心となることもあり得ます。

j 回目の爆発が発生すると、以下の耐久値の減少が起こります。

  • 建物 T_j の耐久値が D_j 減少する。
  • T_j \geq 2 の場合、建物 T_j - 1 の耐久値が \lfloor D_j / 2 \rfloor 減少する。
  • T_j \leq N - 1 の場合、建物 T_j + 1 の耐久値が \lfloor D_j / 2 \rfloor 減少する。

ここで \lfloor D_j / 2 \rfloorD_j2 で割って小数点以下を切り捨てた値を表します。各爆発の影響を受けるのは、中心の建物とその両隣を合わせた最大 3 つの建物のみであり、それより遠くの建物には影響しません。

各爆発によるダメージは建物の現在の耐久値によらず上記の通り決まるため、M 回の爆発が発生する順序は結果に影響しません。

耐久値は 0 未満(負の値)になることもあります。耐久値が 0 以下になった建物であっても列からは取り除かれず、以降の爆発の影響を通常通り受けます(耐久値がさらに減少することがあります)。また、そのような建物が爆発の中心となった場合も、通常通り周囲の建物に影響を及ぼします。

すべての M 回の爆発が終わった後、最終的な耐久値が 1 以上である建物の数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq T_j \leq N (1 \leq j \leq M)
  • 1 \leq D_j \leq 10^9 (1 \leq j \leq M)
  • 入力はすべて整数である

入力

N M
H_1 H_2 \ldots H_N
T_1 D_1
T_2 D_2
\vdots
T_M D_M
  • 1 行目には、建物の数 N と爆発の回数 M が、スペース区切りで与えられる。
  • 2 行目には、各建物の初期耐久値 H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。
  • 3 行目から M 行にわたり、各爆発の情報が与えられる。
  • 2 + j 行目には、j 回目の爆発の中心となる建物番号 T_j と威力 D_j が、スペース区切りで与えられる。

出力

すべての爆発が終わった後、最終的な耐久値が 1 以上である建物の数を 1 行で出力せよ。


入力例 1

5 2
10 20 30 20 10
3 15
1 8

出力例 1

5

入力例 2

7 4
100 50 80 120 60 40 90
2 60
5 100
2 30
7 50

出力例 2

4

入力例 3

10 6
500 300 450 200 600 150 400 250 350 100
3 400
7 300
1 600
5 500
10 200
4 100

出力例 3

4

Score : 266 pts

Problem Statement

Takahashi is the leader of a bomb disposal squad. Currently, N buildings are lined up in a row, numbered 1, 2, \ldots, N from left to right. Each building i has a durability value H_i.

Unfortunately, M explosions will occur in this area. The j-th explosion occurs centered at building T_j with power D_j. Note that the same building may be the center of multiple explosions.

When the j-th explosion occurs, the following durability reductions take place:

  • The durability of building T_j decreases by D_j.
  • If T_j \geq 2, the durability of building T_j - 1 decreases by \lfloor D_j / 2 \rfloor.
  • If T_j \leq N - 1, the durability of building T_j + 1 decreases by \lfloor D_j / 2 \rfloor.

Here, \lfloor D_j / 2 \rfloor denotes the value obtained by dividing D_j by 2 and rounding down. Each explosion affects at most 3 buildings — the center building and its two neighbors — and does not affect buildings farther away.

The damage from each explosion is determined as described above regardless of the building's current durability, so the order in which the M explosions occur does not affect the result.

Durability values can become less than 0 (negative values). Even if a building's durability drops to 0 or below, it is not removed from the row and continues to be affected by subsequent explosions as usual (its durability may decrease further). Also, if such a building becomes the center of an explosion, it affects surrounding buildings as usual.

After all M explosions have finished, find the number of buildings whose final durability is 1 or greater.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq T_j \leq N (1 \leq j \leq M)
  • 1 \leq D_j \leq 10^9 (1 \leq j \leq M)
  • All input values are integers

Input

N M
H_1 H_2 \ldots H_N
T_1 D_1
T_2 D_2
\vdots
T_M D_M
  • The first line contains the number of buildings N and the number of explosions M, separated by a space.
  • The second line contains the initial durability values H_1, H_2, \ldots, H_N of each building, separated by spaces.
  • The following M lines contain the information for each explosion.
  • The (2 + j)-th line contains the building number T_j that is the center of the j-th explosion and its power D_j, separated by a space.

Output

Output in a single line the number of buildings whose final durability is 1 or greater after all explosions have finished.


Sample Input 1

5 2
10 20 30 20 10
3 15
1 8

Sample Output 1

5

Sample Input 2

7 4
100 50 80 120 60 40 90
2 60
5 100
2 30
7 50

Sample Output 2

4

Sample Input 3

10 6
500 300 450 200 600 150 400 250 350 100
3 400
7 300
1 600
5 500
10 200
4 100

Sample Output 3

4
B - Quality Inspection and Product Disposal

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333

問題文

高橋君は工場の品質管理責任者です。工場では製造された製品の品質を定期的に検査し、基準を満たさない製品を廃棄する制度があります。

工場には現在 N 個の製品があり、各製品には 1 から N までの番号が付けられています。製品 i の品質スコアは A_i です。

高橋君は Q 回の品質検査を順番に行います。j 回目 (1 \leq j \leq Q) の検査では基準値 T_j が定められ、その時点でまだ廃棄されていない製品のうち、品質スコアが T_j 未満であるすべての製品を廃棄します。一度廃棄された製品は、以降の検査の対象にはなりません。なお、基準値 T_j は検査ごとにそれぞれ別々に定められるため、前回の基準値より小さくなることもあります。その場合、すでに前回以前の検査で該当する製品が廃棄済みであれば、新たに廃棄される製品はありません。

各検査の直後に、その検査で新たに廃棄された製品の個数を記録します。Q 回すべての品質検査について、各検査で新たに廃棄された製品の個数をそれぞれ求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq T_j \leq 10^9 (1 \leq j \leq Q)
  • 入力はすべて整数である。

入力

N Q
A_1 A_2 \ldots A_N
T_1
T_2
\vdots
T_Q
  • 1 行目には、製品の個数を表す整数 N と、品質検査の回数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目には、各製品の品質スコアを表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 続く Q 行のうち j 行目 (1 \leq j \leq Q) には、j 回目の検査の基準値を表す整数 T_j が与えられる。

出力

Q 行出力せよ。j 行目 (1 \leq j \leq Q) には、j 回目の検査で新たに廃棄された製品の個数を出力せよ。


入力例 1

5 3
3 1 4 1 5
2
4
3

出力例 1

2
1
0

入力例 2

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

出力例 2

3
0
2
0
2

入力例 3

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

出力例 3

4
5
0
3
0
3

Score : 333 pts

Problem Statement

Takahashi is the quality control manager of a factory. The factory has a system where manufactured products are periodically inspected for quality, and products that do not meet the standards are disposed of.

The factory currently has N products, each numbered from 1 to N. The quality score of product i is A_i.

Takahashi performs Q quality inspections in order. In the j-th inspection (1 \leq j \leq Q), a threshold value T_j is set, and among the products that have not yet been disposed of at that point, all products whose quality score is strictly less than T_j are disposed of. Once a product is disposed of, it is no longer subject to subsequent inspections. Note that the threshold value T_j is determined independently for each inspection, so it may be smaller than the previous threshold. In that case, if the relevant products have already been disposed of in previous inspections, no new products are disposed of.

Immediately after each inspection, the number of products newly disposed of in that inspection is recorded. For all Q quality inspections, determine the number of products newly disposed of in each inspection.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq T_j \leq 10^9 (1 \leq j \leq Q)
  • All input values are integers.

Input

N Q
A_1 A_2 \ldots A_N
T_1
T_2
\vdots
T_Q
  • The first line contains an integer N representing the number of products and an integer Q representing the number of quality inspections, separated by a space.
  • The second line contains integers A_1, A_2, \ldots, A_N representing the quality scores of each product, separated by spaces.
  • In the following Q lines, the j-th line (1 \leq j \leq Q) contains an integer T_j representing the threshold value for the j-th inspection.

Output

Output Q lines. The j-th line (1 \leq j \leq Q) should contain the number of products newly disposed of in the j-th inspection.


Sample Input 1

5 3
3 1 4 1 5
2
4
3

Sample Output 1

2
1
0

Sample Input 2

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

Sample Output 2

3
0
2
0
2

Sample Input 3

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

Sample Output 3

4
5
0
3
0
3
C - Reward for Carrying Luggage

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

N 個の部屋が一列に並んでおり、左から順に部屋 1, 部屋 2, \ldots, 部屋 N と番号が付けられています。高橋君はこれらの部屋を部屋 1 から部屋 N まで順に 1 つずつ訪れ、それぞれの部屋で提示される仕事を「引き受ける」か「断る」かを選びます。各部屋にはちょうど 1 つの仕事があり、各部屋を訪れるのは 1 回だけです。

各部屋 i の仕事には種類 T_i と報酬 A_i が設定されています。仕事の種類は次の 2 種類です。

  • 荷物を積む仕事 (T_i = 1):引き受けると報酬 A_i を受け取り、荷物を 1 つ受け取る。持っている荷物の個数が 1 増える。
  • 荷物を降ろす仕事 (T_i = 0):引き受けると報酬 A_i を受け取る。荷物を 1 つ以上持っている場合は荷物を 1 つ降ろし、持っている荷物の個数が 1 減る。荷物を 1 つも持っていない場合は、降ろす荷物がないため、荷物の個数は 0 のまま変わらない(報酬は受け取れる)。

高橋君の持っている荷物の個数は最初 0 です。仕事を引き受ける・断るを繰り返す過程を通じて、どの時点においても荷物の個数が K を超えてはなりません。荷物が多すぎると持ちきれなくなってしまうためです。

また、すべての部屋を訪れ終わった時点で荷物の個数がちょうど 0 でなければなりません(荷物を持ったままでは帰れないためです)。

これらの条件を満たすように高橋君が引き受ける仕事を選んだとき、引き受けた仕事の報酬の合計の最大値を求めてください。なお、1 つも仕事を引き受けないことも可能であり、その場合の報酬の合計は 0 です。

制約

  • 1 \leq K \leq N \leq 10^5
  • N K \leq 10^7
  • T_i \in \{0, 1\}
  • 1 \leq A_i \leq 10^6
  • 入力はすべて整数である

入力

N K
T_1 A_1
T_2 A_2
\vdots
T_N A_N
  • 1 行目には、部屋の数 N と荷物の個数の上限 K が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各部屋の仕事の情報が与えられる。
  • 1 + i 行目には、部屋 i の仕事の種類 T_i (1 : 荷物を積む、0 : 荷物を降ろす) と報酬 A_i が、スペース区切りで与えられる。

出力

条件を満たすように引き受ける仕事を選んだとき、引き受けた仕事の報酬の合計の最大値を 1 行で出力せよ。


入力例 1

4 1
1 5
0 4
1 3
0 10

出力例 1

22

入力例 2

3 2
1 5
1 6
1 7

出力例 2

0

入力例 3

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

出力例 3

61

入力例 4

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

出力例 4

164

入力例 5

1 1
0 1000000

出力例 5

1000000

Score : 366 pts

Problem Statement

There are N rooms arranged in a row, numbered Room 1, Room 2, \ldots, Room N from left to right. Takahashi visits these rooms one by one in order from Room 1 to Room N, and for each room, he chooses to either "accept" or "decline" the job offered there. Each room has exactly one job, and he visits each room exactly once.

Each room i has a job with type T_i and reward A_i. There are two types of jobs:

  • Loading job (T_i = 1): If accepted, he receives reward A_i and picks up one piece of luggage. The number of pieces of luggage he is carrying increases by 1.
  • Unloading job (T_i = 0): If accepted, he receives reward A_i. If he is carrying one or more pieces of luggage, he puts down one piece, and the number of pieces of luggage he is carrying decreases by 1. If he is carrying no luggage, there is nothing to put down, so the number of pieces of luggage remains 0 (he still receives the reward).

The number of pieces of luggage Takahashi is carrying is initially 0. Throughout the process of accepting and declining jobs, the number of pieces of luggage must not exceed K at any point. This is because carrying too much luggage would be unmanageable.

Additionally, the number of pieces of luggage must be exactly 0 after visiting all rooms (because he cannot go home while still carrying luggage).

Determine the maximum total reward of the jobs Takahashi accepts while satisfying these conditions. Note that it is possible to accept no jobs at all, in which case the total reward is 0.

Constraints

  • 1 \leq K \leq N \leq 10^5
  • N K \leq 10^7
  • T_i \in \{0, 1\}
  • 1 \leq A_i \leq 10^6
  • All input values are integers.

Input

N K
T_1 A_1
T_2 A_2
\vdots
T_N A_N
  • The first line contains the number of rooms N and the upper limit on the number of pieces of luggage K, separated by a space.
  • From the 2nd line to the (N + 1)-th line, the job information for each room is given.
  • The (1 + i)-th line contains the type T_i of the job in room i (1: loading, 0: unloading) and the reward A_i, separated by a space.

Output

Print in one line the maximum total reward of accepted jobs when choosing which jobs to accept while satisfying the conditions.


Sample Input 1

4 1
1 5
0 4
1 3
0 10

Sample Output 1

22

Sample Input 2

3 2
1 5
1 6
1 7

Sample Output 2

0

Sample Input 3

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

Sample Output 3

61

Sample Input 4

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

Sample Output 4

164

Sample Input 5

1 1
0 1000000

Sample Output 5

1000000
D - Network Construction

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は、N 台のサーバーを持つデータセンターのネットワーク管理者です。サーバーには 1 から N までの番号が付いています。

サーバー間を接続するために利用可能なケーブルが M 本あり、ケーブルにも 1 から M までの番号が付いています。i 番目のケーブル (1 \leq i \leq M) はサーバー U_i とサーバー V_i を双方向に結び、敷設コストが W_i です。各ケーブルは異なる2台のサーバーを結びます(すなわち U_i \neq V_i)。ただし、同じ2台のサーバーを結ぶケーブルが複数存在することもあります。

高橋君は、N 台のサーバーすべてが互いに通信できるよう、ケーブルをいくつか選んでネットワークを構築したいと考えています。選ぶケーブルの集合は全域木を成す必要があります。ここで全域木とは、N 台のサーバーすべてを頂点として含み、選んだケーブルによって全サーバーが連結であり、かつ閉路が存在しないようなグラフのことです(このとき選ばれるケーブルの本数はちょうど N-1 本になります)。

ただし、セキュリティ上の要件により、特定の K 本のケーブル(番号 E_1, E_2, \dots, E_K)は暗号化通信専用の回線として既に契約済みであり、必ずネットワークに含めなければなりません。

指定された K 本のケーブルをすべて含む全域木が存在する場合は、その全域木に含まれる N-1 本のケーブルの敷設コストの合計の最小値を出力してください。そのような全域木が存在しない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N - 1
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • 1 \leq W_i \leq 10^9
  • 1 \leq E_j \leq M1 \leq j \leq K
  • E_j はすべて異なる
  • 入力はすべて整数である

入力

N M K
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
E_1 E_2 \dots E_K
  • 1 行目には、サーバーの台数 N、利用可能なケーブルの本数 M、必ず含めなければならないケーブルの本数 K が、スペース区切りで与えられる。
  • 2 行目から M + 1 行目では、各ケーブルの情報が M 行で与えられる。
  • 1 + i 行目では、i 番目のケーブルがサーバー U_i とサーバー V_i を結び、敷設コストが W_i であることを表す。
  • K \geq 1 の場合、M + 2 行目には、必ずネットワークに含めなければならないケーブルの番号 E_1, E_2, \dots, E_K がスペース区切りで与えられる。K = 0 の場合、この行は存在しない。

出力

指定された K 本のケーブルをすべて含む全域木が存在する場合、その全域木に含まれる N-1 本のケーブルの敷設コストの合計の最小値を 1 行で出力せよ。存在しない場合は -1 を出力せよ。


入力例 1

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

出力例 1

8

入力例 2

3 3 2
1 2 1
1 2 2
2 3 3
1 2

出力例 2

-1

入力例 3

8 12 2
1 2 5
2 3 3
3 4 7
4 5 2
5 6 8
6 7 4
7 8 6
1 3 9
2 5 1
4 7 10
1 8 11
3 6 12
2 6

出力例 3

29

入力例 4

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

出力例 4

77

入力例 5

2 1 0
1 2 1000000000

出力例 5

1000000000

Score : 400 pts

Problem Statement

Takahashi is the network administrator of a data center with N servers. The servers are numbered from 1 to N.

There are M available cables for connecting servers, and the cables are also numbered from 1 to M. The i-th cable (1 \leq i \leq M) bidirectionally connects server U_i and server V_i, with an installation cost of W_i. Each cable connects two distinct servers (i.e., U_i \neq V_i). However, there may be multiple cables connecting the same pair of servers.

Takahashi wants to select some cables to build a network so that all N servers can communicate with each other. The set of selected cables must form a spanning tree. A spanning tree is a graph that includes all N servers as vertices, where all servers are connected by the selected cables, and no cycles exist (in this case, exactly N-1 cables are selected).

However, due to security requirements, a specific set of K cables (numbered E_1, E_2, \dots, E_K) have already been contracted as dedicated encrypted communication lines and must be included in the network.

If a spanning tree exists that contains all K specified cables, output the minimum total installation cost of the N-1 cables included in that spanning tree. If no such spanning tree exists, output -1.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N - 1
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • 1 \leq W_i \leq 10^9
  • 1 \leq E_j \leq M (1 \leq j \leq K)
  • All E_j are distinct
  • All input values are integers

Input

N M K
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
E_1 E_2 \dots E_K
  • The first line contains the number of servers N, the number of available cables M, and the number of cables that must be included K, separated by spaces.
  • From the 2nd line to the (M + 1)-th line, the information of each cable is given over M lines.
  • The (1 + i)-th line indicates that the i-th cable connects server U_i and server V_i with an installation cost of W_i.
  • If K \geq 1, the (M + 2)-th line contains the cable numbers E_1, E_2, \dots, E_K that must be included in the network, separated by spaces. If K = 0, this line does not exist.

Output

If a spanning tree exists that contains all K specified cables, output in one line the minimum total installation cost of the N-1 cables included in that spanning tree. If no such spanning tree exists, output -1.


Sample Input 1

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

Sample Output 1

8

Sample Input 2

3 3 2
1 2 1
1 2 2
2 3 3
1 2

Sample Output 2

-1

Sample Input 3

8 12 2
1 2 5
2 3 3
3 4 7
4 5 2
5 6 8
6 7 4
7 8 6
1 3 9
2 5 1
4 7 10
1 8 11
3 6 12
2 6

Sample Output 3

29

Sample Input 4

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

Sample Output 4

77

Sample Input 5

2 1 0
1 2 1000000000

Sample Output 5

1000000000
E - Paint Drop

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466

問題文

高橋君は、縦 H マス、横 W マスのグリッド状のキャンバスでペイントゲームをしています。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。

マス (i, j) には、スコア A_{i,j} が設定されています。最初、すべてのマスはペイントされていません。

高橋君はこれから Q 回、キャンバスにペイントを落とします。

t 回目のペイントでは、マス (R_t, C_t) を中心として、マンハッタン距離が D_t 以下であるすべてのキャンバス上のマスがペイントされます。

つまり、1 \leq i \leq H かつ 1 \leq j \leq W を満たすマス (i, j) のうち、

|i - R_t| + |j - C_t| \leq D_t

を満たすすべてのマスがペイントされます。

まだペイントされていないマスが新たにペイントされたとき、高橋君はそのマスのスコアを獲得します。すでにペイントされているマスに再びペイントが届いてもスコアは獲得できません。

各ペイントについて、そのペイントで高橋君が新たに獲得したスコアの総和を求めてください。

制約

  • 1 \leq H \leq 2 \times 10^5
  • 1 \leq W \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • H \times W \leq 4 \times 10^6
  • H \times Q \leq 4 \times 10^6
  • H + Q \leq 2 \times 10^5
  • 0 \leq A_{i,j} \leq 10^9
  • 1 \leq R_t \leq H
  • 1 \leq C_t \leq W
  • 0 \leq D_t \leq H + W
  • 入力はすべて整数である

入力

H W
A_{1,1} A_{1,2} \cdots A_{1,W}
A_{2,1} A_{2,2} \cdots A_{2,W}
\vdots
A_{H,1} A_{H,2} \cdots A_{H,W}
Q
R_1 C_1 D_1
R_2 C_2 D_2
\vdots
R_Q C_Q D_Q
  • 1 行目には、キャンバスの縦のマス数 H と横のマス数 W がスペース区切りで与えられる。
  • 続く H 行のうち i 行目には、スコア A_{i,1}, A_{i,2}, \ldots, A_{i,W} がスペース区切りで与えられる。
  • 続く 1 行には、ペイントの回数 Q が与えられる。
  • 続く Q 行のうち t 行目には、t 回目のペイントの中心の行 R_t、列 C_t、届く距離 D_t がスペース区切りで与えられる。

出力

Q 行出力せよ。

t 行目には、t 回目のペイントで高橋君が新たに獲得したスコアの総和を出力せよ。


入力例 1

3 4
1 2 3 4
5 6 7 8
9 10 11 12
4
2 2 1
1 4 0
3 1 2
2 3 7

出力例 1

30
4
21
23

入力例 2

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

出力例 2

0
16
3
0
0

入力例 3

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

出力例 3

52
30
13
46
6
18
0
0

入力例 4

8 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
12
4 5 3
1 10 2
8 1 4
3 7 0
6 6 5
2 2 1
7 9 3
1 1 18
8 10 0
5 3 2
4 10 6
8 5 8

出力例 4

875
96
885
0
1121
60
80
123
0
0
0
0

入力例 5

1 1
1000000000
4
1 1 0
1 1 1
1 1 2
1 1 0

出力例 5

1000000000
0
0
0

Score : 466 pts

Problem Statement

Takahashi is playing a paint game on a grid canvas with H rows and W columns. The cell at the i-th row from the top and the j-th column from the left is called cell (i, j).

Cell (i, j) has a score A_{i,j} assigned to it. Initially, no cells are painted.

Takahashi will drop paint onto the canvas Q times.

In the t-th paint drop, all cells on the canvas whose Manhattan distance from cell (R_t, C_t) is at most D_t are painted.

In other words, among all cells (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W, all cells satisfying

|i - R_t| + |j - C_t| \leq D_t

are painted.

When a cell that has not yet been painted becomes newly painted, Takahashi earns the score of that cell. No score is earned when paint reaches a cell that is already painted.

For each paint drop, find the total score newly earned by Takahashi from that paint drop.

Constraints

  • 1 \leq H \leq 2 \times 10^5
  • 1 \leq W \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • H \times W \leq 4 \times 10^6
  • H \times Q \leq 4 \times 10^6
  • H + Q \leq 2 \times 10^5
  • 0 \leq A_{i,j} \leq 10^9
  • 1 \leq R_t \leq H
  • 1 \leq C_t \leq W
  • 0 \leq D_t \leq H + W
  • All input values are integers

Input

H W
A_{1,1} A_{1,2} \cdots A_{1,W}
A_{2,1} A_{2,2} \cdots A_{2,W}
\vdots
A_{H,1} A_{H,2} \cdots A_{H,W}
Q
R_1 C_1 D_1
R_2 C_2 D_2
\vdots
R_Q C_Q D_Q
  • The first line contains the number of rows H and the number of columns W of the canvas, separated by a space.
  • In the following H lines, the i-th line contains the scores A_{i,1}, A_{i,2}, \ldots, A_{i,W} separated by spaces.
  • The next line contains the number of paint drops Q.
  • In the following Q lines, the t-th line contains the center row R_t, column C_t, and reach distance D_t of the t-th paint drop, separated by spaces.

Output

Output Q lines.

The t-th line should contain the total score newly earned by Takahashi from the t-th paint drop.


Sample Input 1

3 4
1 2 3 4
5 6 7 8
9 10 11 12
4
2 2 1
1 4 0
3 1 2
2 3 7

Sample Output 1

30
4
21
23

Sample Input 2

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

Sample Output 2

0
16
3
0
0

Sample Input 3

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

Sample Output 3

52
30
13
46
6
18
0
0

Sample Input 4

8 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
12
4 5 3
1 10 2
8 1 4
3 7 0
6 6 5
2 2 1
7 9 3
1 1 18
8 10 0
5 3 2
4 10 6
8 5 8

Sample Output 4

875
96
885
0
1121
60
80
123
0
0
0
0

Sample Input 5

1 1
1000000000
4
1 1 0
1 1 1
1 1 2
1 1 0

Sample Output 5

1000000000
0
0
0