A - Water Management and Harvest Value Aggregation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 266 点

問題文

高橋君は農園の管理者です。農園には N 区画の畑があり、それぞれに 1 から N までの番号が付けられています。

各区画 i(1 \leq i \leq N)には、その区画で育てている作物の「収穫価値」 S_i が定められています。収穫価値は操作によって変化しません。また、各区画 i には初期の土壌の「水分量」 C_i が与えられています。水分量は操作によって変化します。

ある時点において、水分量が 0 以下である区画を「乾燥状態」であるといいます。

高橋君は農園の天候や設備の状況に応じて Q 回の操作を順番に行います。各操作は次の 3 種類のいずれかです。

  • 操作 1:1 l r v — 番号 l から r までの区画のうち、閉鎖されていない各区画の水分量に v を加算する。v は負の値もあり得ます。これは降雨や日照りによる水分量の変動を表します。閉鎖された区画はこの操作の影響を受けません。
  • 操作 2:2 x — 区画 x を閉鎖する。閉鎖された区画は、以降のすべての操作および問い合わせにおいて存在しないものとして扱われます。具体的には、操作 1 による水分量の加算の対象にならず、操作 3 の集計対象にもなりません。一度閉鎖された区画が再び開放されることはありません。同じ区画に対して操作 2 が複数回行われることはないことが保証されます。
  • 操作 3:3 l r — 番号 l から r までの区画のうち、閉鎖されておらず、かつその時点で乾燥状態(水分量が 0 以下)であるすべての区画について、それらの収穫価値の合計を求め、出力します。該当する区画が存在しない場合は 0 を出力します。

すべての操作 3 に対して、現れた順に答えを出力してください。

制約

  • 1 \leq N \leq 3000
  • 1 \leq Q \leq 3000
  • 1 \leq S_i \leq 10^4(1 \leq i \leq N)
  • -10^4 \leq C_i \leq 10^4(1 \leq i \leq N)
  • 操作 1 において、1 \leq l \leq r \leq N、-10^4 \leq v \leq 10^4
  • 操作 2 において、1 \leq x \leq N
  • 操作 3 において、1 \leq l \leq r \leq N
  • 操作 2 で指定される区画 x は、その時点でまだ閉鎖されていない。同じ区画に対して操作 2 が複数回行われることはない
  • 入力はすべて整数である
  • 操作 3 は少なくとも 1 回与えられる

入力

N Q
S_1 S_2 \ldots S_N
C_1 C_2 \ldots C_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
  • 1 行目には、区画の数を表す整数 N と、操作の回数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目には、各区画の収穫価値を表す整数 S_1, S_2, \ldots, S_N がスペース区切りで与えられる。
  • 3 行目には、各区画の初期水分量を表す整数 C_1, C_2, \ldots, C_N がスペース区切りで与えられる。
  • 4 行目から Q 行にわたって、各操作が 1 行ずつ与えられる。各行の先頭の整数が操作の種類を表す。
  • 操作 1 の場合:1 l r v の 4 つの整数がスペース区切りで与えられる。l, r は対象範囲の左端・右端の区画番号、v は加算する水分量の変化値を表す。
  • 操作 2 の場合:2 x の 2 つの整数がスペース区切りで与えられる。x は閉鎖する区画の番号を表す。
  • 操作 3 の場合:3 l r の 3 つの整数がスペース区切りで与えられる。l, r は問い合わせ範囲の左端・右端の区画番号を表す。

出力

操作 3 が与えられるたびに、該当する範囲内で閉鎖されておらず、かつ乾燥状態(水分量が 0 以下)である区画の収穫価値の合計を 1 行に出力せよ。該当する区画が存在しない場合は 0 を出力せよ。操作 3 が複数回ある場合は、与えられた順にそれぞれの結果を出力せよ。


入力例 1

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

出力例 1

100
140
110

入力例 2

8 8
5 15 25 35 10 20 30 40
1 -3 2 0 -1 4 -5 3
3 1 8
2 5
1 1 4 -2
3 1 6
1 6 8 -10
3 5 8
2 7
3 1 8

出力例 2

90
80
90
140

入力例 3

10 10
100 200 300 400 500 600 700 800 900 1000
5 -10 0 3 -7 8 1 -2 6 -4
3 1 10
1 1 5 -5
3 1 5
2 3
2 8
3 1 10
1 4 7 10
3 3 9
1 1 2 1
3 1 10

出力例 3

2800
1500
2200
500
1700

Score : 266 pts

Problem Statement

Takahashi is the manager of a farm. The farm has N plots of land, each numbered from 1 to N.

Each plot i (1 \leq i \leq N) has a designated "harvest value" S_i for the crop grown in that plot. The harvest value does not change through operations. Additionally, each plot i is given an initial soil "moisture level" C_i. The moisture level changes through operations.

At any given point in time, a plot whose moisture level is 0 or less is said to be in a "dry state".

Takahashi performs Q operations in order, depending on the weather and equipment conditions of the farm. Each operation is one of the following three types:

  • Operation 1: 1 l r v — For each non-closed plot numbered from l to r, add v to its moisture level. v can be negative. This represents changes in moisture due to rainfall or drought. Closed plots are not affected by this operation.
  • Operation 2: 2 x — Close plot x. A closed plot is treated as non-existent in all subsequent operations and queries. Specifically, it will not be subject to moisture addition in Operation 1, nor will it be included in the aggregation of Operation 3. Once a plot is closed, it is never reopened. It is guaranteed that Operation 2 is never performed more than once on the same plot.
  • Operation 3: 3 l r — Among the plots numbered from l to r that are not closed and are currently in a dry state (moisture level 0 or less), compute and output the sum of their harvest values. If no such plots exist, output 0.

For all Operation 3 queries, output the answers in the order they appear.

Constraints

  • 1 \leq N \leq 3000
  • 1 \leq Q \leq 3000
  • 1 \leq S_i \leq 10^4 (1 \leq i \leq N)
  • -10^4 \leq C_i \leq 10^4 (1 \leq i \leq N)
  • For Operation 1: 1 \leq l \leq r \leq N, -10^4 \leq v \leq 10^4
  • For Operation 2: 1 \leq x \leq N
  • For Operation 3: 1 \leq l \leq r \leq N
  • The plot x specified in Operation 2 has not yet been closed at that point. Operation 2 is never performed more than once on the same plot.
  • All input values are integers.
  • Operation 3 is given at least once.

Input

N Q
S_1 S_2 \ldots S_N
C_1 C_2 \ldots C_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
  • The first line contains two space-separated integers: N, the number of plots, and Q, the number of operations.
  • The second line contains N space-separated integers S_1, S_2, \ldots, S_N, representing the harvest value of each plot.
  • The third line contains N space-separated integers C_1, C_2, \ldots, C_N, representing the initial moisture level of each plot.
  • The following Q lines each describe one operation. The first integer on each line indicates the type of operation.
  • For Operation 1: Four space-separated integers 1 l r v are given. l and r are the left and right endpoints of the target range, and v is the moisture change value to add.
  • For Operation 2: Two space-separated integers 2 x are given. x is the number of the plot to close.
  • For Operation 3: Three space-separated integers 3 l r are given. l and r are the left and right endpoints of the query range.

Output

Each time Operation 3 is given, output on a single line the sum of harvest values of plots within the specified range that are not closed and are in a dry state (moisture level 0 or less). If no such plots exist, output 0. If Operation 3 occurs multiple times, output the results in the order they are given.


Sample Input 1

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

Sample Output 1

100
140
110

Sample Input 2

8 8
5 15 25 35 10 20 30 40
1 -3 2 0 -1 4 -5 3
3 1 8
2 5
1 1 4 -2
3 1 6
1 6 8 -10
3 5 8
2 7
3 1 8

Sample Output 2

90
80
90
140

Sample Input 3

10 10
100 200 300 400 500 600 700 800 900 1000
5 -10 0 3 -7 8 1 -2 6 -4
3 1 10
1 1 5 -5
3 1 5
2 3
2 8
3 1 10
1 4 7 10
3 3 9
1 1 2 1
3 1 10

Sample Output 3

2800
1500
2200
500
1700
B - Robot Going Back and Forth in a Hallway

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

高橋君は、長さ L の真っ直ぐな廊下でロボットの動作実験を行っています。この廊下は数直線上の区間 [0, L] に対応しており、両端(座標 0 と座標 L)には壁があります。

廊下には N 台のロボットがあり、i 番目のロボット(1 \leq i \leq N)は時刻 0 において座標 X_i に置かれています。各ロボットには時刻 0 における速度 V_i が設定されています。V_i > 0 のロボットは座標が増加する方向(正の方向)に、V_i < 0 のロボットは座標が減少する方向(負の方向)に、それぞれ速さ |V_i| で等速直線運動を行います。V_i = 0 のロボットは移動せず、その場に留まり続けます。ここで速度の単位は、座標の単位を時間の単位で割ったものです。

ロボットが廊下の端(座標 0 または座標 L)に到達すると、即座に速度の符号が反転し、同じ速さのまま逆方向に移動を続けます。この反射は何度でも繰り返され、ロボットは常に区間 [0, L] 内に留まります。

この反射ルールは時刻 0 においても適用されます。具体的には、初期位置が壁上であるロボットの挙動は以下のとおりです。

  • 座標 0 にいるロボット:V_i < 0 の場合、時刻 0 で即座に速度が反転し、正の方向に速さ |V_i| で移動を開始します。V_i \geq 0 の場合は反転せず、速度 V_i のまま移動します(V_i = 0 ならその場に留まります)。
  • 座標 L にいるロボット:V_i > 0 の場合、時刻 0 で即座に速度が反転し、負の方向に速さ |V_i| で移動を開始します。V_i \leq 0 の場合は反転せず、速度 V_i のまま移動します(V_i = 0 ならその場に留まります)。

各ロボットは独立に運動します。2台以上のロボットが同じ座標に到達しても、互いをすり抜け、それぞれの運動に影響を与えません。

時刻 T における N 台のロボットそれぞれの座標を求めてください。

なお、与えられる入力では、時刻 T における各ロボットの座標は必ず整数となることが保証されます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L \leq 10^9
  • 0 \leq T \leq 10^9
  • 0 \leq X_i \leq L(1 \leq i \leq N)
  • -10^9 \leq V_i \leq 10^9(1 \leq i \leq N)
  • 入力はすべて整数である
  • 時刻 T における各ロボットの座標は必ず整数となる

入力

N L T
X_1 V_1
X_2 V_2
\vdots
X_N V_N
  • 1 行目には、ロボットの台数を表す整数 N、廊下の長さを表す整数 L、求める時刻を表す整数 T が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各ロボットの初期位置と時刻 0 における速度が与えられる。
  • 1 + i 行目(1 \leq i \leq N)では、i 番目のロボットの初期位置 X_i と速度 V_i が、スペース区切りで与えられる。

出力

N 行出力せよ。i 行目(1 \leq i \leq N)には、時刻 T における i 番目のロボットの座標を出力せよ。


入力例 1

3 10 3
2 1
8 3
5 -2

出力例 1

5
3
1

入力例 2

4 6 5
0 -3
6 4
3 0
1 2

出力例 2

3
2
3
1

入力例 3

5 100 7
10 15
50 -30
0 20
99 -1
100 -10

出力例 3

85
40
60
92
30

入力例 4

6 1000000000 1000000000
500000000 0
0 1
1 -1
0 2
500000000 3
999999999 1

出力例 4

500000000
1000000000
999999999
0
500000000
1

入力例 5

1 1 0
0 0

出力例 5

0

Score : 300 pts

Problem Statement

Takahashi is conducting a robot motion experiment in a straight corridor of length L. This corridor corresponds to the interval [0, L] on a number line, and there are walls at both ends (coordinate 0 and coordinate L).

There are N robots in the corridor, and the i-th robot (1 \leq i \leq N) is placed at coordinate X_i at time 0. Each robot has a velocity V_i set at time 0. A robot with V_i > 0 moves at constant speed |V_i| in the direction of increasing coordinates (positive direction), a robot with V_i < 0 moves at constant speed |V_i| in the direction of decreasing coordinates (negative direction), and a robot with V_i = 0 does not move and stays in place. Here, the unit of velocity is the unit of coordinate divided by the unit of time.

When a robot reaches the end of the corridor (coordinate 0 or coordinate L), the sign of its velocity is immediately reversed, and it continues moving in the opposite direction at the same speed. This reflection is repeated any number of times, and the robot always remains within the interval [0, L].

This reflection rule also applies at time 0. Specifically, the behavior of robots whose initial position is on a wall is as follows:

  • A robot at coordinate 0: If V_i < 0, its velocity is immediately reversed at time 0, and it begins moving in the positive direction at speed |V_i|. If V_i \geq 0, it does not reverse and moves with velocity V_i (if V_i = 0, it stays in place).
  • A robot at coordinate L: If V_i > 0, its velocity is immediately reversed at time 0, and it begins moving in the negative direction at speed |V_i|. If V_i \leq 0, it does not reverse and moves with velocity V_i (if V_i = 0, it stays in place).

Each robot moves independently. Even if two or more robots reach the same coordinate, they pass through each other and do not affect each other's motion.

Find the coordinate of each of the N robots at time T.

It is guaranteed that in the given input, the coordinate of each robot at time T is always an integer.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L \leq 10^9
  • 0 \leq T \leq 10^9
  • 0 \leq X_i \leq L (1 \leq i \leq N)
  • -10^9 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers
  • The coordinate of each robot at time T is always an integer

Input

N L T
X_1 V_1
X_2 V_2
\vdots
X_N V_N
  • The first line contains three space-separated integers: N representing the number of robots, L representing the length of the corridor, and T representing the time to query.
  • From the 2nd line to the (N + 1)-th line, the initial position and velocity at time 0 of each robot are given.
  • The (1 + i)-th line (1 \leq i \leq N) contains the initial position X_i and velocity V_i of the i-th robot, separated by a space.

Output

Output N lines. The i-th line (1 \leq i \leq N) should contain the coordinate of the i-th robot at time T.


Sample Input 1

3 10 3
2 1
8 3
5 -2

Sample Output 1

5
3
1

Sample Input 2

4 6 5
0 -3
6 4
3 0
1 2

Sample Output 2

3
2
3
1

Sample Input 3

5 100 7
10 15
50 -30
0 20
99 -1
100 -10

Sample Output 3

85
40
60
92
30

Sample Input 4

6 1000000000 1000000000
500000000 0
0 1
1 -1
0 2
500000000 3
999999999 1

Sample Output 4

500000000
1000000000
999999999
0
500000000
1

Sample Input 5

1 1 0
0 0

Sample Output 5

0
C - Spread of Rumors

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366 点

問題文

高橋君は、とある学校の生徒会長です。文化祭の準備期間中に、ある重要な連絡事項を生徒たちに伝える必要があります。

この学校には N 人の生徒がおり、各生徒には 1 から N までの番号が付けられています。

生徒同士の友人関係が M 組与えられます。友人関係は双方向であり、自己ループや多重辺はありません。

高橋君は最初に K 人の生徒を選び、彼らに直接情報を伝えました。最初に情報を伝えた K 人の生徒の番号を S_1, S_2, \ldots, S_K とします。

情報は友人関係を通じて次々と伝わっていきます。すなわち、情報を知っている生徒の友人もその情報を知り、さらにその友人にも伝わる、ということが繰り返されます。より正確には、S_1, S_2, \ldots, S_K のいずれかの生徒から、友人関係の辺を 0 回以上たどって到達できる生徒はすべて最終的に情報を知ることになります。

最終的に情報を知っている生徒の人数を求めてください。なお、最初に情報を伝えられた K 人の生徒自身も、情報を知っている生徒に含みます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq S_i \leq N (1 \leq i \leq K)
  • S_i \neq S_j (i \neq j)
  • 1 \leq u_i < v_i \leq N (1 \leq i \leq M)
  • (u_i, v_i) \neq (u_j, v_j) (i \neq j)
  • 入力はすべて整数

入力

N M K
S_1 S_2 \ldots S_K
u_1 v_1
u_2 v_2
\vdots
u_M v_M
  • 1 行目には、生徒の人数を表す N 、友人関係の数を表す M 、最初に情報を伝えた生徒の人数を表す K が、スペース区切りで与えられる。
  • 2 行目には、最初に情報を伝えた生徒の番号 S_1, S_2, \ldots, S_K が、スペース区切りで与えられる。
  • 3 行目から M + 2 行目には、友人関係が M 行で与えられる。
  • 2 + i 行目 (1 \leq i \leq M) には、 i 番目の友人関係における 2 人の生徒の番号 u_i と v_i がスペース区切りで与えられる。この友人関係は双方向である。各辺は u_i < v_i を満たす形で与えられる。

出力

最終的に情報を知っている生徒の人数を 1 行で出力せよ。


入力例 1

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

出力例 1

5

入力例 2

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

出力例 2

7

入力例 3

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

出力例 3

10

Score : 366 pts

Problem Statement

Takahashi is the student council president of a certain school. During the preparation period for the cultural festival, he needs to convey an important announcement to the students.

There are N students in this school, and each student is assigned a number from 1 to N.

M pairs of friendship relations between students are given. Friendships are bidirectional, and there are no self-loops or multiple edges.

Takahashi initially chose K students and directly informed them. Let the numbers of the K students who were initially informed be S_1, S_2, \ldots, S_K.

Information spreads successively through friendship relations. That is, friends of students who know the information also learn it, and it further spreads to their friends, and so on. More precisely, all students who can be reached from any of the students S_1, S_2, \ldots, S_K by following zero or more friendship edges will eventually learn the information.

Find the number of students who ultimately know the information. Note that the K students who were initially informed are also counted as students who know the information.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq S_i \leq N (1 \leq i \leq K)
  • S_i \neq S_j (i \neq j)
  • 1 \leq u_i < v_i \leq N (1 \leq i \leq M)
  • (u_i, v_i) \neq (u_j, v_j) (i \neq j)
  • All input values are integers

Input

N M K
S_1 S_2 \ldots S_K
u_1 v_1
u_2 v_2
\vdots
u_M v_M
  • The first line contains N representing the number of students, M representing the number of friendships, and K representing the number of students initially informed, separated by spaces.
  • The second line contains the numbers S_1, S_2, \ldots, S_K of the students initially informed, separated by spaces.
  • From the 3rd line to the (M + 2)-th line, M friendships are given over M lines.
  • The (2 + i)-th line (1 \leq i \leq M) contains the numbers u_i and v_i of the two students in the i-th friendship, separated by a space. This friendship is bidirectional. Each edge is given in a form satisfying u_i < v_i.

Output

Output the number of students who ultimately know the information in a single line.


Sample Input 1

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

Sample Output 1

5

Sample Input 2

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

Sample Output 2

7

Sample Input 3

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

Sample Output 3

10
D - Presentation Order

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋君は社内プレゼンテーション大会の司会進行を担当しています。この大会では N 人の社員が発表を行い、それぞれの社員には 1 から N までの番号が付けられています。

各社員 i (1 \leq i \leq N) には「プレゼン力」を表す正の整数 A_i が定められています。

大会では、N 人の社員それぞれに 1 番目から N 番目までの発表順を重複なく割り当てます。社員 i が何番目に発表するかを P_i とすると、(P_1, P_2, \ldots, P_N) は (1, 2, \ldots, N) の順列となります。発表は 1 番目、2 番目、\ldots、N 番目の順に行われます。

社員 i の「貢献度」は A_i \times P_i と定義されます。大会全体の「総合スコア」は、全社員の貢献度の合計

\displaystyle\sum_{i=1}^{N} A_i \times P_i

として定義されます。

高橋君は総合スコアをできるだけ大きくしたいと考えています。しかし、青木君から M 個の制約条件が課されました。各制約条件 k (1 \leq k \leq M) は「社員 U_k は社員 V_k よりも先に発表しなければならない(すなわち P_{U_k} < P_{V_k})」というものです。

すべての制約条件を満たす発表順のうち、総合スコアが最大となるものを求め、その最大の総合スコアを出力してください。

なお、制約条件を満たす発表順が少なくとも 1 つ存在することが保証されます。

制約

  • 1 \leq N \leq 8
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i \leq 100 (1 \leq i \leq N)
  • 1 \leq U_k, V_k \leq N (1 \leq k \leq M)
  • U_k \neq V_k (1 \leq k \leq M)
  • 同じ (U_k, V_k) の組が複数回与えられることはない。
  • 制約条件を満たす発表順が少なくとも 1 つ存在する(すなわち、社員を頂点、制約条件を有向辺とする有向グラフは DAG である)。
  • 入力はすべて整数である。

入力

N M
A_1 A_2 \ldots A_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • 1 行目には、社員の人数を表す N と、制約条件の数を表す M が、スペース区切りで与えられる。
  • 2 行目には、各社員のプレゼン力を表す A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 3 行目から M 行にわたり、制約条件が与えられる。M = 0 のときこれらの行は存在しない。
  • 2 + k 行目 (1 \leq k \leq M) には、社員 U_k が社員 V_k よりも先に発表しなければならないことを表す U_k と V_k が、スペース区切りで与えられる。

出力

すべての制約条件を満たす発表順における総合スコアの最大値を 1 行で出力せよ。


入力例 1

3 1
1 2 3
1 2

出力例 1

14

入力例 2

3 1
3 1 2
1 3

出力例 2

13

入力例 3

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

出力例 3

84

入力例 4

8 4
10 20 30 40 50 60 70 80
1 2
3 4
5 6
7 8

出力例 4

2040

入力例 5

1 0
42

出力例 5

42

Score : 400 pts

Problem Statement

Takahashi is in charge of hosting a company presentation competition. In this competition, N employees will give presentations, and each employee is assigned a number from 1 to N.

Each employee i (1 \leq i \leq N) has a positive integer A_i representing their "presentation skill".

In the competition, each of the N employees is assigned a unique presentation order from 1st to Nth. Let P_i denote the position in which employee i presents. Then (P_1, P_2, \ldots, P_N) is a permutation of (1, 2, \ldots, N). Presentations are given in order: 1st, 2nd, \ldots, Nth.

The "contribution" of employee i is defined as A_i \times P_i. The "total score" of the competition is defined as the sum of contributions of all employees:

\displaystyle\sum_{i=1}^{N} A_i \times P_i

Takahashi wants to maximize the total score. However, Aoki has imposed M constraints. Each constraint k (1 \leq k \leq M) states that "employee U_k must present before employee V_k (i.e., P_{U_k} < P_{V_k})".

Among all presentation orders that satisfy all constraints, find the one that maximizes the total score, and output that maximum total score.

It is guaranteed that at least one presentation order satisfying all constraints exists.

Constraints

  • 1 \leq N \leq 8
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i \leq 100 (1 \leq i \leq N)
  • 1 \leq U_k, V_k \leq N (1 \leq k \leq M)
  • U_k \neq V_k (1 \leq k \leq M)
  • The same pair (U_k, V_k) is not given more than once.
  • At least one presentation order satisfying all constraints exists (i.e., the directed graph with employees as vertices and constraints as directed edges is a DAG).
  • All input values are integers.

Input

N M
A_1 A_2 \ldots A_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • The first line contains N, the number of employees, and M, the number of constraints, separated by a space.
  • The second line contains A_1, A_2, \ldots, A_N, the presentation skills of each employee, separated by spaces.
  • The following M lines contain the constraints. These lines do not exist when M = 0.
  • The (2 + k)-th line (1 \leq k \leq M) contains U_k and V_k, separated by a space, indicating that employee U_k must present before employee V_k.

Output

Output in a single line the maximum total score among all presentation orders that satisfy all constraints.


Sample Input 1

3 1
1 2 3
1 2

Sample Output 1

14

Sample Input 2

3 1
3 1 2
1 3

Sample Output 2

13

Sample Input 3

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

Sample Output 3

84

Sample Input 4

8 4
10 20 30 40 50 60 70 80
1 2
3 4
5 6
7 8

Sample Output 4

2040

Sample Input 5

1 0
42

Sample Output 5

42
E - Adventurer and a Row of Monsters

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466 点

問題文

高橋君は N 体のモンスターが一列に並んだダンジョンに挑みます。i 番目 (1 \leq i \leq N) のモンスターの強さは A_i です。モンスターの強さおよび高橋君の体力はいずれも 0 以上 C 以下の整数で表されます。

高橋君はこのダンジョンに対して Q 回の操作を順に行います。操作は次の 2 種類です。

  • 1 p x : p 番目のモンスターの強さを x に変更する。この変更は以降の操作すべてに反映される。
  • 2 l r d : 体力の初期値が d の状態で、l 番目から r 番目までのモンスターを順に相手にしたとき、倒せるモンスターの数を求める。この操作はモンスターの強さを変更しない。

操作 2 l r d で倒せるモンスターの数は、次の手順で決まります。

現在の体力を h とし、最初 h = d とします。

i = l, l+1, \dots, r の順に、i 番目のモンスター(現在の強さ A_i)について以下を行います:

  • h \geq A_i ならば、そのモンスターを倒し、h を h - A_i に更新する。
  • h < A_i ならば、そのモンスターは倒せず、h は変化しない。

いずれの場合も、次のモンスターに進みます。

すべてのモンスターを見終わったとき、倒したモンスターの合計数がこの操作の答えです。

各操作 2 について、答えを出力してください。

制約

  • 1 \leq N \leq 50000
  • 1 \leq C \leq 50
  • 1 \leq Q \leq 20000
  • 0 \leq A_i \leq C (1 \leq i \leq N)
  • 操作 1 p x について:1 \leq p \leq N, 0 \leq x \leq C
  • 操作 2 l r d について:1 \leq l \leq r \leq N, 0 \leq d \leq C
  • 入力はすべて整数である。

入力

N C Q
A_1 A_2 \dots A_N
query_1
query_2
\vdots
query_Q
  • 1 行目には、モンスターの数 N、強さと体力の上限 C、操作の回数 Q がスペース区切りで与えられる。
  • 2 行目には、初期状態での各モンスターの強さ A_1, A_2, \dots, A_N がスペース区切りで与えられる。
  • 続く Q 行には、各操作が 1 p x または 2 l r d の形式で 1 行ずつ与えられる。ここで query_j は j 番目の操作を表す。

出力

操作 2 が現れるたびに、その答えを 1 行に出力してください。


入力例 1

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

出力例 1

2
4
1
2

入力例 2

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

出力例 2

1
1
2
0

入力例 3

12 20 14
4 0 15 3 8 6 2 20 1 7 5 10
2 1 12 20
2 3 8 10
1 8 4
2 3 8 10
1 2 9
2 1 5 12
1 10 0
2 9 12 7
2 1 1 3
1 3 0
2 1 4 4
1 12 20
2 6 12 20
2 5 5 8

出力例 3

4
2
2
2
3
0
2
6
1

入力例 4

40 50 25
0 50 1 25 13 37 8 8 49 2 17 33 5 41 12 29 0 6 44 19 23 7 31 15 4 48 10 21 36 3 27 14 39 11 30 16 45 9 22 34
2 1 40 50
2 1 10 0
1 2 0
2 1 5 50
1 9 10
2 6 15 30
1 20 50
2 18 22 49
2 25 40 50
1 17 50
2 16 18 50
1 40 0
2 35 40 20
1 1 50
2 1 3 50
2 10 30 45
1 26 1
1 37 2
2 24 38 25
2 30 30 3
1 30 50
2 29 31 50
1 14 0
2 11 15 41
2 1 40 50

出力例 4

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

入力例 5

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

出力例 5

1
1
0
1
1

Score : 466 pts

Problem Statement

Takahashi challenges a dungeon where N monsters are lined up in a row. The strength of the i-th (1 \leq i \leq N) monster is A_i. Both the monsters' strengths and Takahashi's health are represented as integers between 0 and C, inclusive.

Takahashi performs Q operations on this dungeon in order. There are two types of operations:

  • 1 p x : Change the strength of the p-th monster to x. This change is reflected in all subsequent operations.
  • 2 l r d : Determine the number of monsters that can be defeated when facing monsters from the l-th to the r-th in order, starting with an initial health of d. This operation does not change the monsters' strengths.

The number of monsters defeated in operation 2 l r d is determined by the following procedure:

Let the current health be h, initially h = d.

For i = l, l+1, \dots, r in order, do the following for the i-th monster (with current strength A_i):

  • If h \geq A_i, defeat that monster and update h to h - A_i.
  • If h < A_i, that monster cannot be defeated, and h remains unchanged.

In either case, proceed to the next monster.

After all monsters have been processed, the total number of defeated monsters is the answer for this operation.

For each operation 2, output the answer.

Constraints

  • 1 \leq N \leq 50000
  • 1 \leq C \leq 50
  • 1 \leq Q \leq 20000
  • 0 \leq A_i \leq C (1 \leq i \leq N)
  • For operation 1 p x: 1 \leq p \leq N, 0 \leq x \leq C
  • For operation 2 l r d: 1 \leq l \leq r \leq N, 0 \leq d \leq C
  • All input values are integers.

Input

N C Q
A_1 A_2 \dots A_N
query_1
query_2
\vdots
query_Q
  • The first line contains the number of monsters N, the upper limit of strength and health C, and the number of operations Q, separated by spaces.
  • The second line contains the initial strengths of each monster A_1, A_2, \dots, A_N, separated by spaces.
  • The following Q lines each contain an operation in the format 1 p x or 2 l r d, one per line. Here query_j represents the j-th operation.

Output

Each time operation 2 appears, output the answer on a single line.


Sample Input 1

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

Sample Output 1

2
4
1
2

Sample Input 2

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

Sample Output 2

1
1
2
0

Sample Input 3

12 20 14
4 0 15 3 8 6 2 20 1 7 5 10
2 1 12 20
2 3 8 10
1 8 4
2 3 8 10
1 2 9
2 1 5 12
1 10 0
2 9 12 7
2 1 1 3
1 3 0
2 1 4 4
1 12 20
2 6 12 20
2 5 5 8

Sample Output 3

4
2
2
2
3
0
2
6
1

Sample Input 4

40 50 25
0 50 1 25 13 37 8 8 49 2 17 33 5 41 12 29 0 6 44 19 23 7 31 15 4 48 10 21 36 3 27 14 39 11 30 16 45 9 22 34
2 1 40 50
2 1 10 0
1 2 0
2 1 5 50
1 9 10
2 6 15 30
1 20 50
2 18 22 49
2 25 40 50
1 17 50
2 16 18 50
1 40 0
2 35 40 20
1 1 50
2 1 3 50
2 10 30 45
1 26 1
1 37 2
2 24 38 25
2 30 30 3
1 30 50
2 29 31 50
1 14 0
2 11 15 41
2 1 40 50

Sample Output 4

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

Sample Input 5

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

Sample Output 5

1
1
0
1
1