A - お店の売上計算

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

配点 : 233 点

問題文

高橋君はお店を経営しています。このお店では N 種類の商品を販売しており、それぞれ 1 から N の番号で区別されています。商品 i の定価は 1 個あたり A_i 円です。

このお店では会員制度を導入しており、会員の客は 1 個あたり K 円の割引を受けられます。具体的には、会員でない客が商品 i を購入する場合、1 個あたりの価格は定価どおりの A_i 円です。会員の客が商品 i を購入する場合、1 個あたりの価格は定価から K 円を引いた \max(A_i - K, 0) 円になります(定価以上の割引にはならず、価格は 0 円未満にはなりません)。

今、M 件の購入記録が与えられます。j 番目の記録には、会員かどうかを表す値 S_j(1 なら会員、0 なら非会員)、購入した商品の番号 P_j、および購入した個数 D_j が含まれています。

各購入記録における購入金額は、1 個あたりの価格に購入個数 D_j を掛けた値です。すべての購入記録における購入金額の合計を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 10^5
  • 0 \leq K \leq 10^5
  • 1 \leq A_i \leq 10^5 (1 \leq i \leq N)
  • S_j \in \{0, 1\} (1 \leq j \leq M)
  • 1 \leq P_j \leq N (1 \leq j \leq M)
  • 1 \leq D_j \leq 10^5 (1 \leq j \leq M)
  • 入力はすべて整数である。
  • 答えは 10^{15} 以下であることが保証される。

入力

N M K
A_1 A_2 \cdots A_N
S_1 P_1 D_1
S_2 P_2 D_2
\vdots
S_M P_M D_M
  • 1 行目には、商品の種類数 N、購入記録の件数 M、会員の割引額 K がスペース区切りで与えられる。
  • 2 行目には、N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
  • 続く M 行のうち j 行目 (1 \leq j \leq M) には、j 番目の購入記録の会員かどうかを表す値 S_j、商品番号 P_j、購入個数 D_j がスペース区切りで与えられる。

出力

すべての購入記録における購入金額の合計を 1 行で出力してください。


入力例 1

3 4 20
100 50 10
0 1 2
1 2 3
1 3 5
0 2 1

出力例 1

340

入力例 2

2 3 0
30 70
1 1 4
0 2 1
1 2 2

出力例 2

330

入力例 3

6 8 15
40 120 15 200 80 5
1 1 10
0 2 3
1 3 7
0 4 1
1 5 4
0 6 20
1 4 2
0 1 5

出力例 3

1740

入力例 4

15 20 35
20 55 90 35 120 10 200 75 60 150 5 300 45 95 130
1 1 8
0 2 6
1 3 5
0 4 10
1 5 2
0 6 30
1 7 1
0 8 7
1 9 9
0 10 3
1 11 12
0 12 1
1 4 4
0 3 2
1 8 6
0 13 5
1 14 3
0 15 2
1 2 11
0 7 4

出力例 4

5195

入力例 5

1 1 100000
1
1 1 100000

出力例 5

0

Score : 233 pts

Problem Statement

Takahashi runs a store. This store sells N types of products, each distinguished by a number from 1 to N. The regular price of product i is A_i yen per item.

This store has a membership system, where member customers receive a discount of K yen per item. Specifically, when a non-member customer purchases product i, the price per item is the regular price of A_i yen. When a member customer purchases product i, the price per item is \max(A_i - K, 0) yen, which is the regular price minus K yen (the discount does not exceed the regular price, and the price does not go below 0 yen).

Now, M purchase records are given. The j-th record contains a value S_j indicating whether the customer is a member (1 for member, 0 for non-member), the product number P_j purchased, and the quantity D_j purchased.

The purchase amount for each record is the price per item multiplied by the quantity D_j. Find the total purchase amount across all purchase records.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 10^5
  • 0 \leq K \leq 10^5
  • 1 \leq A_i \leq 10^5 (1 \leq i \leq N)
  • S_j \in \{0, 1\} (1 \leq j \leq M)
  • 1 \leq P_j \leq N (1 \leq j \leq M)
  • 1 \leq D_j \leq 10^5 (1 \leq j \leq M)
  • All inputs are integers.
  • It is guaranteed that the answer is at most 10^{15}.

Input

N M K
A_1 A_2 \cdots A_N
S_1 P_1 D_1
S_2 P_2 D_2
\vdots
S_M P_M D_M
  • The first line contains the number of product types N, the number of purchase records M, and the member discount amount K, separated by spaces.
  • The second line contains N integers A_1, A_2, \ldots, A_N, separated by spaces.
  • The j-th line (1 \leq j \leq M) of the following M lines contains the membership status S_j, the product number P_j, and the quantity purchased D_j for the j-th purchase record, separated by spaces.

Output

Output the total purchase amount across all purchase records in a single line.


Sample Input 1

3 4 20
100 50 10
0 1 2
1 2 3
1 3 5
0 2 1

Sample Output 1

340

Sample Input 2

2 3 0
30 70
1 1 4
0 2 1
1 2 2

Sample Output 2

330

Sample Input 3

6 8 15
40 120 15 200 80 5
1 1 10
0 2 3
1 3 7
0 4 1
1 5 4
0 6 20
1 4 2
0 1 5

Sample Output 3

1740

Sample Input 4

15 20 35
20 55 90 35 120 10 200 75 60 150 5 300 45 95 130
1 1 8
0 2 6
1 3 5
0 4 10
1 5 2
0 6 30
1 7 1
0 8 7
1 9 9
0 10 3
1 11 12
0 12 1
1 4 4
0 3 2
1 8 6
0 13 5
1 14 3
0 15 2
1 2 11
0 7 4

Sample Output 4

5195

Sample Input 5

1 1 100000
1
1 1 100000

Sample Output 5

0
B - ランプ列の分割スコア最大化

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

配点 : 333 点

問題文

高橋君は、N 個のランプが一列に並んだ装置を持っています。各ランプは点灯(1)または消灯(0)のいずれかの状態です。

ランプを左から順に b_1, b_2, \ldots, b_N と表すと、この装置の状態は整数

X = \sum_{i=1}^{N} b_i \cdot 2^{i-1}

で表されます。すなわち、左から i 番目のランプ b_i が X の 2 進表現における下から i ビット目に対応します(左端が最下位ビットです)。

高橋君の装置に対して、以下の操作が繰り返し行われます。操作は最大 K 回行われますが、途中で停止することがあります。

1回の操作: 現在の列の左端のランプを確認する。


- それが点灯(1)であれば、何もせずに全操作を終了する。

- それが消灯(0)であれば、そのランプを列から取り除き、右端に点灯(1)状態のランプを 1 つ追加する(列の長さは N のまま保たれる)。まだ操作回数が K 回に達していなければ、再び操作を行う。

すなわち、「左端が消灯であるかぎり、それを取り除いて右端に点灯ランプを追加する」という操作を、最大 K 回まで行います。左端が点灯であることを確認した時点、または操作を K 回行った時点で処理は終了します。

この処理の後、青木君が M 個のランプ列を持ってきます。各ランプ列も長さ N であり、j 番目のランプ列の状態は整数 Y_j で、高橋君の装置と同様のビット対応で表されます。

高橋君は、処理後の自分のランプ列と青木君の M 個のランプ列、合計 M + 1 個のランプ列すべてに対して、共通の分割位置 p(1 \le p \le N - 1)を 1 つ選びます。各ランプ列は、左側 p 個と右側 N - p 個に分けられます。

各ランプ列について、左側部分のランプを左から順に c_1, c_2, \ldots, c_p としたとき、その整数値を

\sum_{i=1}^{p} c_i \cdot 2^{i-1}

で定めます。M+1 個のランプ列の左側部分の整数値の総和を A とします。

同様に、各ランプ列について、右側部分のランプを左から順に d_1, d_2, \ldots, d_{N-p} としたとき、その整数値を

\sum_{i=1}^{N-p} d_i \cdot 2^{i-1}

で定めます(右側部分内で改めて 2^0 から数え直すことに注意してください)。M+1 個のランプ列の右側部分の整数値の総和を B とします。

高橋君は A + B を最大化するように分割位置 p を選びます。A + B の最大値を求めてください。

制約

  • 2 \leq N \leq 46
  • 0 \leq K \leq 10^9
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq X < 2^N
  • 0 \leq Y_j < 2^N(1 \leq j \leq M)
  • 入力はすべて整数である
  • この制約のもとで、答えは signed 64-bit integer に収まる

入力

N K M X
Y_1 Y_2 \ldots Y_M

1 行目には、ランプ列の長さ N、操作回数の上限 K、追加ランプ列の個数 M、高橋君の装置の初期状態 X が、スペース区切りで与えられる。

M \geq 1 のとき、2 行目には、M 個の追加ランプ列の状態を表す整数 Y_1, Y_2, \ldots, Y_M が、スペース区切りで与えられる。

M = 0 のとき、2 行目は存在しない。

出力

A + B の最大値を 1 行で出力せよ。


入力例 1

5 2 2 4
3 16

出力例 1

23

入力例 2

4 0 1 9
6

出力例 2

8

入力例 3

12 7 6 1384
0 4095 2730 1365 2048 1234

出力例 3

7616

入力例 4

46 1000000000 10 35184372088832
0 1 70368744177663 70368744177662 35184372088831 35184372088832 1234567890123 45678901234567 60000000000000 42

出力例 4

194194222917695

入力例 5

2 1000000000 0 0

出力例 5

2

Score : 333 pts

Problem Statement

Takahashi has a device with N lamps arranged in a row. Each lamp is either on (1) or off (0).

Denoting the lamps from left to right as b_1, b_2, \ldots, b_N, the state of the device is represented by the integer

X = \sum_{i=1}^{N} b_i \cdot 2^{i-1}

That is, the i-th lamp from the left, b_i, corresponds to the i-th bit from the bottom in the binary representation of X (the leftmost lamp is the least significant bit).

The following operation is repeatedly performed on Takahashi's device. The operation is performed at most K times, but may stop midway.

One operation: Check the leftmost lamp of the current sequence.


- If it is on (1), do nothing and terminate all operations.

- If it is off (0), remove that lamp from the sequence and append one on (1) lamp to the right end (the length of the sequence remains N). If the number of operations has not yet reached K, perform the operation again.

In other words, "as long as the leftmost lamp is off, remove it and append an on lamp to the right end" is performed up to K times. The process terminates when the leftmost lamp is confirmed to be on, or when K operations have been performed.

After this process, Aoki brings M lamp sequences. Each lamp sequence also has length N, and the state of the j-th lamp sequence is represented by the integer Y_j, using the same bit correspondence as Takahashi's device.

Takahashi chooses a single common split position p (1 \le p \le N - 1) for all M + 1 lamp sequences — his own processed lamp sequence and Aoki's M lamp sequences. Each lamp sequence is divided into the left p lamps and the right N - p lamps.

For each lamp sequence, letting the lamps of the left part from left to right be c_1, c_2, \ldots, c_p, its integer value is defined as

\sum_{i=1}^{p} c_i \cdot 2^{i-1}

Let A be the sum of the integer values of the left parts of all M+1 lamp sequences.

Similarly, for each lamp sequence, letting the lamps of the right part from left to right be d_1, d_2, \ldots, d_{N-p}, its integer value is defined as

\sum_{i=1}^{N-p} d_i \cdot 2^{i-1}

(Note that the powers of 2 restart from 2^0 within the right part.) Let B be the sum of the integer values of the right parts of all M+1 lamp sequences.

Takahashi chooses the split position p to maximize A + B. Find the maximum value of A + B.

Constraints

  • 2 \leq N \leq 46
  • 0 \leq K \leq 10^9
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq X < 2^N
  • 0 \leq Y_j < 2^N (1 \leq j \leq M)
  • All inputs are integers
  • Under these constraints, the answer fits in a signed 64-bit integer

Input

N K M X
Y_1 Y_2 \ldots Y_M

The first line contains the length of the lamp sequence N, the maximum number of operations K, the number of additional lamp sequences M, and the initial state of Takahashi's device X, separated by spaces.

When M \geq 1, the second line contains M integers Y_1, Y_2, \ldots, Y_M representing the states of the additional lamp sequences, separated by spaces.

When M = 0, the second line does not exist.

Output

Output the maximum value of A + B in one line.


Sample Input 1

5 2 2 4
3 16

Sample Output 1

23

Sample Input 2

4 0 1 9
6

Sample Output 2

8

Sample Input 3

12 7 6 1384
0 4095 2730 1365 2048 1234

Sample Output 3

7616

Sample Input 4

46 1000000000 10 35184372088832
0 1 70368744177663 70368744177662 35184372088831 35184372088832 1234567890123 45678901234567 60000000000000 42

Sample Output 4

194194222917695

Sample Input 5

2 1000000000 0 0

Sample Output 5

2
C - 列車の組み換え

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

配点 : 366 点

問題文

高橋君は列車の組み換え作業を行っています。

最初、列車は先頭から順に赤色(R)の車両が A 両、その後ろに青色(B)の車両が B 両連結された状態になっています。すなわち、初期状態を文字列で表すと R が A 個並んだ後に B が B 個並んだ長さ N = A + B の文字列です。

高橋君は次の操作を 0 回以上繰り返し行うことができます。

  • 整数 k(1 \leq k \leq N - 1)を選ぶ。現在の列車の後ろから k 両を切り離す。すなわち、列車を先頭側の N - k 両と末尾側の k 両に分割する。
  • 切り離した末尾側の k 両を、車両の並び順を保ったまま一つのブロックとして、先頭側の N - k 両の中の任意の位置に挿入して再連結する。挿入できる位置は、先頭の前、末尾の後ろ、または隣り合う 2 両の間の、合計 N - k + 1 箇所のうちいずれかである。

操作は毎回、その時点での列車の状態に対して行います。

目標となる車両の並びを表す長さ N の文字列 T が与えられます。初期状態から T の並びにするために必要な操作回数の最小値を求めてください。

ただし、T に含まれる R の個数が A でない場合(すなわち車両の構成が初期状態と異なる場合)、目標の並びにすることは不可能です。不可能な場合は -1 を出力してください。

制約

  • 1 \leq A
  • 1 \leq B
  • A + B \leq 10^7
  • T は R と B のみからなる長さ A + B の文字列である
  • T に含まれる R の個数が A 個であるとは限らない

入力

A B
T
  • 1 行目には、赤色の車両数を表す整数 A と青色の車両数を表す整数 B が、スペース区切りで与えられる。
  • 2 行目には、目標となる車両の並びを表す文字列 T が与えられる。

出力

初期状態から T にするために必要な操作回数の最小値を出力せよ。不可能な場合は -1 を出力せよ。


入力例 1

2 2
RBBR

出力例 1

1

入力例 2

3 2
RRBBB

出力例 2

-1

入力例 3

7 6
RBRBBRRBRRBBR

出力例 3

4

入力例 4

25 20
BBBBBBBBBBBBBBBBBBBBRRRRRRRRRRRRRRRRRRRRRRRRR

出力例 4

1

入力例 5

1 1
RB

出力例 5

0

Score : 366 pts

Problem Statement

Takahashi is performing train rearrangement work.

Initially, the train consists of A red (R) cars from the front, followed by B blue (B) cars connected behind them. In other words, representing the initial state as a string, it is a string of length N = A + B where A characters R are followed by B characters B.

Takahashi can perform the following operation zero or more times:

  • Choose an integer k (1 \leq k \leq N - 1). Detach the last k cars from the current train. That is, split the train into the front N - k cars and the rear k cars.
  • Insert the detached rear k cars as a single block, maintaining their order, into any position within the front N - k cars and reconnect them. The possible insertion positions are: before the front, after the end, or between any two adjacent cars, for a total of N - k + 1 possible positions.

Each operation is performed on the current state of the train at that point.

You are given a string T of length N representing the target arrangement of cars. Find the minimum number of operations needed to transform the initial state into the arrangement T.

However, if the number of R characters in T is not A (i.e., the composition of cars differs from the initial state), it is impossible to achieve the target arrangement. In that case, output -1.

Constraints

  • 1 \leq A
  • 1 \leq B
  • A + B \leq 10^7
  • T is a string of length A + B consisting only of R and B
  • The number of R characters in T is not necessarily A

Input

A B
T
  • The first line contains integers A and B representing the number of red cars and blue cars, separated by a space.
  • The second line contains the string T representing the target arrangement of cars.

Output

Output the minimum number of operations needed to transform the initial state into T. If it is impossible, output -1.


Sample Input 1

2 2
RBBR

Sample Output 1

1

Sample Input 2

3 2
RRBBB

Sample Output 2

-1

Sample Input 3

7 6
RBRBBRRBRRBBR

Sample Output 3

4

Sample Input 4

25 20
BBBBBBBBBBBBBBBBBBBBRRRRRRRRRRRRRRRRRRRRRRRRR

Sample Output 4

1

Sample Input 5

1 1
RB

Sample Output 5

0
D - 通路の扉と体力

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

配点 : 400 点

問題文

横一列に並んだ L 個の部屋からなる通路があります。部屋には左から順に 0 から L - 1 までの番号が付いています。

隣り合う部屋の間には扉があります。具体的には、1 \leq x \leq L - 1 を満たす各整数 x に対して、部屋 x - 1 と部屋 x の間に扉 x があります。通路の両端(部屋 0 の左側および部屋 L-1 の右側)は外壁であり、通り抜けることはできません。

この通路には N 種類の警備パターンが設定されています。警備パターン i は法 A_i 、初期値 R_i 、時刻係数 V_i を持ちます。整数時刻 t において、扉 x (1 \leq x \leq L - 1)が

x \equiv R_i + V_i \cdot t \pmod{A_i}

を満たすとき、警備パターン i はその扉に錠を 1 つかけます。1 つの扉に対して複数の警備パターンが同時に錠をかけることもあります。

時刻 t における扉 x の 重さ を、時刻 t にその扉にかけられている錠の総数と定めます。

Q 個の質問が与えられます。質問 j では、時刻 T_j に高橋君が部屋 S_j にいます。時刻 T_j における錠の状態を固定して(時間経過による変化を考えずに)以下を考えます。

高橋君の体力は P_j です。高橋君が部屋 S_j から部屋 c に到達するには、S_j と c の間にあるすべての扉を順に通過する必要があります。このとき通過する扉の重さの合計が P_j 以下であれば、高橋君は部屋 c に到達できます。

各質問について、高橋君が到達できる部屋の個数を求めてください。ただし、高橋君が最初にいる部屋 S_j 自身も到達可能な部屋として数えます。

制約

  • 1 \leq N \leq 10^5
  • 2 \leq L \leq 10^9
  • 1 \leq Q \leq 10^5
  • N \times Q \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 0 \leq R_i \leq A_i - 1
  • 0 \leq V_i \leq A_i - 1
  • 0 \leq T_j \leq 10^9
  • 0 \leq S_j \leq L - 1
  • 0 \leq P_j \leq 10^{14}
  • 入力はすべて整数である。

入力

N L Q
A_1 R_1 V_1
A_2 R_2 V_2
:
A_N R_N V_N
T_1 S_1 P_1
T_2 S_2 P_2
:
T_Q S_Q P_Q
  • 1 行目には、警備パターンの数を表す N 、部屋の個数を表す L 、質問の個数を表す Q がスペース区切りで与えられる。
  • 続く N 行では、警備パターンの情報が与えられる。
  • 1 + i 行目には、パターン i の法 A_i 、初期値 R_i 、時刻係数 V_i がスペース区切りで与えられる。
  • 続く Q 行では、質問の情報が与えられる。
  • 1 + N + j 行目には、質問 j の時刻 T_j 、高橋君のいる部屋番号 S_j 、体力 P_j がスペース区切りで与えられる。

出力

Q 行出力してください。

j 行目には、質問 j に対して高橋君が到達できる部屋の個数を出力してください。


入力例 1

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

出力例 1

1
6
4
6

入力例 2

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

出力例 2

7
2
5
1
8

入力例 3

8 50 10
7 0 3
11 5 2
13 12 0
6 4 5
17 8 9
10 1 7
9 3 3
25 24 1
0 0 5
1 10 12
2 20 0
5 25 30
10 49 100
12345 17 50
999999 33 7
42 4 20
7 48 1
100 12 1000

出力例 3

8
27
5
50
50
50
19
30
8
50

入力例 4

20 100000 15
1000 0 1
99991 12345 67890
2 1 1
3 2 0
97 96 13
1000000000 999999999 0
50000 25000 24999
65536 32768 12345
12345 6789 1111
88888 77777 66666
100 0 99
999 998 997
100000 54321 1234
271828 1828 271
314159 26535 89793
42 17 23
73 0 72
1000000000 0 999999999
1234567 765432 123456
987654321 123456789 98765432
0 0 0
1 99999 10
2 50000 1000
1000 12345 50000
999999999 54321 100000000000000
314159265 999 123456789
271828182 88888 9876543210
42 4242 42
73 7300 7300
1000000000 1 1
555555555 77777 333333
123456789 23456 1000000
987654321 65432 999999999999
80808080 90909 12345678901234
60606060 10101 0

出力例 4

1
11
2238
68321
100000
100000
100000
96
15474
2
100000
100000
100000
100000
2

入力例 5

1 2 5
1 0 0
0 0 0
0 0 1
1000000000 1 0
1000000000 1 100000000000000
123 0 100000000000000

出力例 5

1
2
1
2
2

Score : 400 pts

Problem Statement

There is a corridor consisting of L rooms arranged in a horizontal row. The rooms are numbered from 0 to L - 1 from left to right.

There is a door between each pair of adjacent rooms. Specifically, for each integer x satisfying 1 \leq x \leq L - 1, there is door x between room x - 1 and room x. Both ends of the corridor (the left side of room 0 and the right side of room L-1) are outer walls and cannot be passed through.

This corridor has N types of security patterns configured. Security pattern i has modulus A_i, initial value R_i, and time coefficient V_i. At integer time t, if door x (1 \leq x \leq L - 1) satisfies

x \equiv R_i + V_i \cdot t \pmod{A_i}

then security pattern i places 1 lock on that door. Multiple security patterns may place locks on the same door at the same time.

The weight of door x at time t is defined as the total number of locks placed on that door at time t.

You are given Q queries. In query j, Takahashi is in room S_j at time T_j. Fix the state of the locks at time T_j (without considering changes over time) and consider the following.

Takahashi's stamina is P_j. For Takahashi to reach room c from room S_j, he must pass through all doors between S_j and c in order. If the total weight of the doors he passes through is at most P_j, then Takahashi can reach room c.

For each query, find the number of rooms Takahashi can reach. Note that room S_j where Takahashi starts is also counted as a reachable room.

Constraints

  • 1 \leq N \leq 10^5
  • 2 \leq L \leq 10^9
  • 1 \leq Q \leq 10^5
  • N \times Q \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 0 \leq R_i \leq A_i - 1
  • 0 \leq V_i \leq A_i - 1
  • 0 \leq T_j \leq 10^9
  • 0 \leq S_j \leq L - 1
  • 0 \leq P_j \leq 10^{14}
  • All inputs are integers.

Input

N L Q
A_1 R_1 V_1
A_2 R_2 V_2
:
A_N R_N V_N
T_1 S_1 P_1
T_2 S_2 P_2
:
T_Q S_Q P_Q
  • The first line contains N representing the number of security patterns, L representing the number of rooms, and Q representing the number of queries, separated by spaces.
  • The following N lines give the security pattern information.
  • The (1 + i)-th line contains the modulus A_i, initial value R_i, and time coefficient V_i of pattern i, separated by spaces.
  • The following Q lines give the query information.
  • The (1 + N + j)-th line contains the time T_j, the room number S_j where Takahashi is, and the stamina P_j for query j, separated by spaces.

Output

Output Q lines.

On the j-th line, output the number of rooms Takahashi can reach for query j.


Sample Input 1

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

Sample Output 1

1
6
4
6

Sample Input 2

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

Sample Output 2

7
2
5
1
8

Sample Input 3

8 50 10
7 0 3
11 5 2
13 12 0
6 4 5
17 8 9
10 1 7
9 3 3
25 24 1
0 0 5
1 10 12
2 20 0
5 25 30
10 49 100
12345 17 50
999999 33 7
42 4 20
7 48 1
100 12 1000

Sample Output 3

8
27
5
50
50
50
19
30
8
50

Sample Input 4

20 100000 15
1000 0 1
99991 12345 67890
2 1 1
3 2 0
97 96 13
1000000000 999999999 0
50000 25000 24999
65536 32768 12345
12345 6789 1111
88888 77777 66666
100 0 99
999 998 997
100000 54321 1234
271828 1828 271
314159 26535 89793
42 17 23
73 0 72
1000000000 0 999999999
1234567 765432 123456
987654321 123456789 98765432
0 0 0
1 99999 10
2 50000 1000
1000 12345 50000
999999999 54321 100000000000000
314159265 999 123456789
271828182 88888 9876543210
42 4242 42
73 7300 7300
1000000000 1 1
555555555 77777 333333
123456789 23456 1000000
987654321 65432 999999999999
80808080 90909 12345678901234
60606060 10101 0

Sample Output 4

1
11
2238
68321
100000
100000
100000
96
15474
2
100000
100000
100000
100000
2

Sample Input 5

1 2 5
1 0 0
0 0 0
0 0 1
1000000000 1 0
1000000000 1 100000000000000
123 0 100000000000000

Sample Output 5

1
2
1
2
2
E - 会社の組織と給与

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

配点 : 466 点

問題文

高橋君は、ある会社の人事部門で働くデータアナリストです。この会社には N 人の社員が所属しており、それぞれ 1 から N までの社員番号が付けられています。

この会社では、社員の間に上司・部下の関係が存在します。各社員 i ( 2 \leq i \leq N )にはちょうど 1 人の直属の上司 P_i がおり、 P_i < i が保証されています。社員 1 は社長であり、直属の上司を持ちません。この関係は、社員 1 を根とする根付き木を形成します。

社員 u が社員 v の部下であるとは、根付き木において u が v の子孫であること、すなわち v から根に向かう経路上に u は含まれないが、u から根に向かう経路上に v が含まれることを指します。言い換えると、社員 u から直属の上司の関係を 1 回以上たどることで社員 v に到達できるとき、かつそのときに限り、u は v の部下です。なお、社員 v 自身は社員 v の部下には含みません。

各社員 i には給与を表す正の整数値 A_i が定められています。

高橋君は、組織の分析のために、以下の 2 種類のクエリを合計 Q 回処理する必要があります。各クエリは与えられた順に逐次処理され、更新クエリによる変更はそれ以降のすべてのクエリに反映されます。

クエリ1(更新): 1 v x — 社員 v の給与を x に変更する。

クエリ2(調査): 2 v — 社員 v のすべての部下のうち、そのクエリが処理される時点での社員 v の給与よりも厳密に大きい給与を持つ者の人数を求めて出力する。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq A_i \leq 10^9 ( 1 \leq i \leq N )
  • 1 \leq P_i < i ( 2 \leq i \leq N )
  • クエリ1において、 1 \leq v \leq N かつ 1 \leq x \leq 10^9
  • クエリ2において、 1 \leq v \leq N
  • クエリ2は 1 つ以上存在する
  • 入力はすべて整数である

入力

N Q
A_1 A_2 \cdots A_N
P_2 P_3 \cdots P_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
  • 1 行目には、社員の人数を表す整数 N とクエリの個数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目には、各社員の初期給与 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
  • 3 行目には、社員 2 から社員 N までの直属の上司を表す整数 P_2, P_3, \ldots, P_N がスペース区切りで与えられる。ただし N = 1 のとき、この行には何も書かれていない(空行が与えられる)。
  • 続く Q 行にわたり、各クエリが 1 行ずつ与えられる。
  • クエリ1の場合: 1 v x の形式で、社員番号 v と新しい給与 x がスペース区切りで与えられる。
  • クエリ2の場合: 2 v の形式で、社員番号 v が与えられる。

出力

クエリ2が与えられるたびに、社員 v の部下のうち社員 v の給与よりも厳密に大きい給与を持つ者の人数を 1 行に出力せよ。部下が 1 人もいない場合は 0 を出力せよ。


入力例 1

5 5
10 5 15 8 3
1 1 2 2
2 1
2 2
1 5 20
2 2
2 1

出力例 1

1
1
2
2

入力例 2

4 4
5 10 3 7
1 2 2
2 3
2 4
1 3 100
2 2

出力例 2

0
0
1

入力例 3

10 8
50 30 70 20 60 10 80 40 90 55
1 1 2 2 3 3 4 4 5
2 1
2 2
2 3
1 4 100
2 2
1 1 85
2 1
2 5

出力例 3

5
4
1
5
2
0

入力例 4

20 15
100 90 80 70 60 50 40 30 20 10 110 120 130 140 150 55 65 75 85 95
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
2 1
2 2
2 3
1 1 200
2 1
1 11 500
2 5
1 5 1
2 5
2 4
1 10 999
2 9
2 1
1 3 1000
2 3

出力例 4

5
2
4
0
2
3
2
2
2
0

入力例 5

1 3
42

1 1 100
2 1
1 1 1

出力例 5

0

Score : 466 pts

Problem Statement

Takahashi is a data analyst working in the human resources department of a company. The company has N employees, each assigned an employee number from 1 to N.

In this company, there exist boss-subordinate relationships among employees. Each employee i (2 \leq i \leq N) has exactly one direct boss P_i, and it is guaranteed that P_i < i. Employee 1 is the president and has no direct boss. This relationship forms a rooted tree with employee 1 as the root.

Employee u is a subordinate of employee v if u is a descendant of v in the rooted tree. In other words, v is not on the path from v to the root that includes u, but v is on the path from u to the root. More precisely, u is a subordinate of v if and only if employee v can be reached from employee u by following the direct boss relationship one or more times. Note that employee v itself is not considered a subordinate of employee v.

Each employee i has a positive integer value A_i representing their salary.

For organizational analysis, Takahashi needs to process a total of Q queries of the following two types. Each query is processed sequentially in the given order, and changes made by update queries are reflected in all subsequent queries.

Query 1 (Update): 1 v x — Change the salary of employee v to x.

Query 2 (Investigation): 2 v — Among all subordinates of employee v, find and output the number of those whose salary is strictly greater than the salary of employee v at the time the query is processed.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq P_i < i (2 \leq i \leq N)
  • For Query 1, 1 \leq v \leq N and 1 \leq x \leq 10^9
  • For Query 2, 1 \leq v \leq N
  • There is at least one Query 2
  • All input values are integers

Input

N Q
A_1 A_2 \cdots A_N
P_2 P_3 \cdots P_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
  • The first line contains the integer N representing the number of employees and the integer Q representing the number of queries, separated by a space.
  • The second line contains the initial salaries A_1, A_2, \ldots, A_N of each employee, separated by spaces.
  • The third line contains the integers P_2, P_3, \ldots, P_N representing the direct bosses of employees 2 through N, separated by spaces. When N = 1, this line is empty (an empty line is given).
  • The following Q lines each contain one query.
  • For Query 1: Given in the format 1 v x, where the employee number v and the new salary x are separated by spaces.
  • For Query 2: Given in the format 2 v, where the employee number v is given.

Output

For each Query 2, output on a single line the number of subordinates of employee v whose salary is strictly greater than the salary of employee v. If employee v has no subordinates, output 0.


Sample Input 1

5 5
10 5 15 8 3
1 1 2 2
2 1
2 2
1 5 20
2 2
2 1

Sample Output 1

1
1
2
2

Sample Input 2

4 4
5 10 3 7
1 2 2
2 3
2 4
1 3 100
2 2

Sample Output 2

0
0
1

Sample Input 3

10 8
50 30 70 20 60 10 80 40 90 55
1 1 2 2 3 3 4 4 5
2 1
2 2
2 3
1 4 100
2 2
1 1 85
2 1
2 5

Sample Output 3

5
4
1
5
2
0

Sample Input 4

20 15
100 90 80 70 60 50 40 30 20 10 110 120 130 140 150 55 65 75 85 95
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
2 1
2 2
2 3
1 1 200
2 1
1 11 500
2 5
1 5 1
2 5
2 4
1 10 999
2 9
2 1
1 3 1000
2 3

Sample Output 4

5
2
4
0
2
3
2
2
2
0

Sample Input 5

1 3
42

1 1 100
2 1
1 1 1

Sample Output 5

0