実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 266 点
問題文
高橋君は N 個の都市からなる地域の電力網を管理しています。各都市には 1 から N までの番号が付けられており、都市 i には正常に機能するために必要な電力量(需要量) S_i が定められています。
この地域には M 本の送電線があり、各送電線は 2 つの都市を双方向に結んでいます。 j 番目の送電線は都市 U_j と都市 V_j を結び、送電容量 W_j を持ちます。各送電線は、結んでいる 2 つの都市のそれぞれに対して、送電容量の分だけ電力を供給することができます。
都市 i に供給可能な電力量 T_i を、都市 i を端点とするすべての送電線の送電容量の合計として定めます。すなわち、都市 i を端点とする送電線の送電容量が W_{j_1}, W_{j_2}, \ldots, W_{j_k} であるとき、 T_i = W_{j_1} + W_{j_2} + \cdots + W_{j_k} です。都市 i を端点とする送電線が 1 本も存在しない場合は T_i = 0 とします。
都市 i が「電力不足の都市」であるとは、 T_i < S_i が成り立つことを意味します。
電力不足の都市の数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
- 1 \leq W_j \leq 10^9 (1 \leq j \leq M)
- 同じ都市の組を結ぶ送電線は高々 1 本である(すなわち、(U_j, V_j) の組はすべて異なる)
- 入力はすべて整数である
入力
N M S_1 S_2 \ldots S_N U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M
- 1 行目には、都市の数を表す N と送電線の数を表す M が、スペース区切りで与えられる。
- 2 行目には、各都市の需要量を表す S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。
- 3 行目から M 行にわたり、送電線の情報が与えられる。
- 2 + j 行目では、 j 番目の送電線が結ぶ都市 U_j と V_j 、および送電容量 W_j が、スペース区切りで与えられる。
出力
電力不足の都市の数を 1 行で出力せよ。
入力例 1
4 3 10 3 5 8 1 2 4 2 3 6 3 4 2
出力例 1
2
入力例 2
3 3 5 5 5 1 2 3 1 3 3 2 3 3
出力例 2
0
入力例 3
6 5 10 20 15 8 30 5 1 2 7 2 3 10 3 4 5 4 5 3 1 6 6
出力例 3
2
入力例 4
10 8 100 50 200 30 80 60 150 40 90 10 1 2 25 2 3 30 3 4 50 4 5 20 5 6 35 6 7 40 7 8 60 9 10 5
出力例 4
6
入力例 5
1 0 1
出力例 5
1
Score : 266 pts
Problem Statement
Takahashi manages the power grid of a region consisting of N cities. Each city is numbered from 1 to N, and city i has a required power amount (demand) S_i needed to function properly.
There are M power transmission lines in this region, each connecting two cities bidirectionally. The j-th transmission line connects city U_j and city V_j and has a transmission capacity of W_j. Each transmission line can supply power equal to its transmission capacity to each of the two cities it connects.
The power amount T_i that can be supplied to city i is defined as the sum of the transmission capacities of all transmission lines that have city i as an endpoint. That is, if the transmission capacities of the transmission lines with city i as an endpoint are W_{j_1}, W_{j_2}, \ldots, W_{j_k}, then T_i = W_{j_1} + W_{j_2} + \cdots + W_{j_k}. If there are no transmission lines with city i as an endpoint, then T_i = 0.
City i is called a "power-deficient city" if T_i < S_i holds.
Find the number of power-deficient cities.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
- 1 \leq W_j \leq 10^9 (1 \leq j \leq M)
- There is at most one transmission line connecting the same pair of cities (i.e., all pairs (U_j, V_j) are distinct)
- All input values are integers
Input
N M S_1 S_2 \ldots S_N U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M
- The first line contains N, the number of cities, and M, the number of transmission lines, separated by a space.
- The second line contains the demand of each city S_1, S_2, \ldots, S_N, separated by spaces.
- The following M lines provide information about the transmission lines.
- The (2 + j)-th line contains the cities U_j and V_j connected by the j-th transmission line, and the transmission capacity W_j, separated by spaces.
Output
Output the number of power-deficient cities in one line.
Sample Input 1
4 3 10 3 5 8 1 2 4 2 3 6 3 4 2
Sample Output 1
2
Sample Input 2
3 3 5 5 5 1 2 3 1 3 3 2 3 3
Sample Output 2
0
Sample Input 3
6 5 10 20 15 8 30 5 1 2 7 2 3 10 3 4 5 4 5 3 1 6 6
Sample Output 3
2
Sample Input 4
10 8 100 50 200 30 80 60 150 40 90 10 1 2 25 2 3 30 3 4 50 4 5 20 5 6 35 6 7 40 7 8 60 9 10 5
Sample Output 4
6
Sample Input 5
1 0 1
Sample Output 5
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君の前に N 本の柱が一列に並んでいます。左から i 番目の柱の耐久値は A_i です。
高橋君はすべての柱に対して同時に同じ力 X で衝撃を与えます。衝撃を受けた各柱は、耐久値が X だけ減少します。
その後、左から右へ順に連鎖が伝わります。具体的には、柱が倒壊(耐久値が 0 以下になること)すると、すぐ右隣の柱の耐久値がさらに 1 減少します。これにより右隣の柱も倒壊した場合、同様にそのさらに右隣の柱の耐久値が 1 減少します。この連鎖は右方向に、柱が倒壊しなくなるまで続きます。なお、最も右の柱が倒壊しても、それ以上の連鎖は発生しません。
まとめると、各柱が倒壊するかどうかは、直接受ける衝撃 X に加え、すぐ左隣の柱が倒壊していればさらに 1 のダメージを受ける、という規則で左から順に決まります。
すべての柱を倒壊させるために必要な X の最小値を求めてください。X は正の整数とします。
制約
- 1 \leq N \leq 5 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で与えられます。
N A_1 A_2 \ldots A_N
- 1 行目には、柱の本数 N が与えられます。
- 2 行目には、各柱の耐久値を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられます。
出力
すべての柱を倒壊させるために必要な力 X の最小値を 1 行で出力してください。
入力例 1
4 2 3 2 4
出力例 1
3
入力例 2
5 5 2 2 2 2
出力例 2
5
入力例 3
12 4 2 5 3 6 7 3 8 4 9 5 10
出力例 3
9
入力例 4
30 8 12 9 13 10 14 11 15 12 16 13 17 14 18 15 19 16 20 17 21 18 22 19 23 20 24 21 25 22 26
出力例 4
25
入力例 5
1 1000000000
出力例 5
1000000000
Score : 300 pts
Problem Statement
There are N pillars lined up in a row in front of Takahashi. The durability of the i-th pillar from the left is A_i.
Takahashi simultaneously applies the same force X to all pillars. Each pillar that receives the impact has its durability decreased by X.
After that, a chain reaction propagates from left to right. Specifically, when a pillar collapses (its durability becomes 0 or less), the durability of the pillar immediately to its right decreases by an additional 1. If this causes the right neighbor to also collapse, the durability of the pillar immediately to its right decreases by 1 as well. This chain continues to the right until a pillar does not collapse. Note that if the rightmost pillar collapses, no further chain reaction occurs.
In summary, whether each pillar collapses is determined from left to right by the following rule: in addition to the direct impact X, a pillar receives an additional 1 damage if the pillar immediately to its left has collapsed.
Find the minimum value of X required to collapse all pillars. X must be a positive integer.
Constraints
- 1 \leq N \leq 5 \times 10^5
- 1 \leq A_i \leq 10^9
- All inputs are integers
Input
The input is given in the following format.
N A_1 A_2 \ldots A_N
- The first line gives the number of pillars N.
- The second line gives N integers A_1, A_2, \ldots, A_N representing the durability of each pillar, separated by spaces.
Output
Print the minimum value of the force X required to collapse all pillars in a single line.
Sample Input 1
4 2 3 2 4
Sample Output 1
3
Sample Input 2
5 5 2 2 2 2
Sample Output 2
5
Sample Input 3
12 4 2 5 3 6 7 3 8 4 9 5 10
Sample Output 3
9
Sample Input 4
30 8 12 9 13 10 14 11 15 12 16 13 17 14 18 15 19 16 20 17 21 18 22 19 23 20 24 21 25 22 26
Sample Output 4
25
Sample Input 5
1 1000000000
Sample Output 5
1000000000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は N 台のコンピュータと 1 台の中央サーバからなるネットワークの管理者です。N 台のコンピュータには 1 から N までの番号が、中央サーバにはコンピュータ 0 という番号が付けられています。すなわち、ネットワーク全体はコンピュータ 0, 1, \ldots, N の合計 N + 1 台で構成されます。
各コンピュータ i(1 \leq i \leq N)は、親コンピュータ P_i(0 \leq P_i \leq N, P_i \neq i)に直接接続されています。これらの接続関係は、中央サーバ(コンピュータ 0)を根とする N + 1 頂点の木構造を成しています。この木構造において、コンピュータ v の子コンピュータとは、P_j = v を満たすコンピュータ j のことを指します。
ある日、ネットワークにウイルスが侵入しました。各コンピュータ i(1 \leq i \leq N)には脆弱性値 D_i が設定されています。
ウイルスの感染は、コンピュータ 1 から N のみを対象とします。中央サーバ(コンピュータ 0)は感染の対象外であり、感染することはありません。感染は以下のルールで連鎖的に広がります。
- 初期感染: 脆弱性値が正(D_i > 0)であるコンピュータは全て感染します。脆弱性値が 0 以下(D_i \leq 0)であるコンピュータは、この段階では感染しません。ただし、以下のルール 2 によって後から感染する場合があります。
- 感染の伝播(子→親方向): まだ感染していないコンピュータ v(1 \leq v \leq N)について、v の子コンピュータのうち感染しているものの台数を a、感染していないものの台数を b とします。a > b を満たすならば、v を新たに感染させます。この判定はコンピュータ v 自身の脆弱性値 D_v の値によらず行われます。すなわち、D_v \leq 0 であっても、子コンピュータの感染状態の条件を満たせば v は感染します。なお、子コンピュータを 1 台も持たないコンピュータでは a = b = 0 となり、a > b を満たさないため、このルールでは感染しません。感染は子の感染状態に基づいて親が感染する方向にのみ起こります。親コンピュータが感染しても、そのことによって子コンピュータが新たに感染することはありません。
- 繰り返し: ルール 2 の判定と感染を、新たに感染するコンピュータが 1 台も発生しなくなるまで繰り返します。各回の判定は、その回の開始時点での感染状態に基づいて全コンピュータについて同時に行います。すなわち、ある回で新たに感染するコンピュータが複数ある場合、それらは全て同時に感染し、同じ回の途中で感染状態が変化することはありません。次の回の判定は、更新後の感染状態に基づいて行われます。
最終的に感染したコンピュータの台数を求めてください。中央サーバ(コンピュータ 0)は感染の対象外であるため、カウントに含めません。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq P_i \leq N、P_i \neq i(1 \leq i \leq N)
- 与えられる接続関係はコンピュータ 0 を根とする N + 1 頂点の木構造を成す
- -10^9 \leq D_i \leq 10^9(1 \leq i \leq N)
- 入力はすべて整数である
入力
N P_1 D_1 P_2 D_2 \vdots P_N D_N
- 1 行目には、コンピュータの台数を表す整数 N が与えられる。
- 2 行目から N + 1 行目では、各コンピュータの情報が与えられる。
- 1 + i 行目では、コンピュータ i の親コンピュータの番号 P_i と、コンピュータ i の脆弱性値 D_i がスペース区切りで与えられる。
出力
最終的に感染したコンピュータの台数を 1 行で出力せよ。
入力例 1
6 0 -1 1 1 1 2 1 0 2 -5 3 0
出力例 1
3
入力例 2
4 0 0 0 -3 0 -1 0 0
出力例 2
0
入力例 3
15 0 -5 1 -1 1 -1 1 0 2 1 2 1 2 0 3 -1 3 2 3 0 4 1 4 0 8 1 8 1 8 0
出力例 3
10
入力例 4
40 0 -100 1 0 1 -1 1 -1 2 -1 2 -1 2 3 3 -2 3 -2 3 -2 4 1 4 0 4 1 5 0 5 1 5 1 6 -1 6 2 7 0 7 -5 8 1 8 0 8 1 9 -1 9 -1 10 1 10 1 10 0 17 1 17 0 17 1 19 1 19 0 20 -1 20 -1 24 1 24 1 25 0 25 1 34 1
出力例 4
28
入力例 5
1 0 -1000000000
出力例 5
0
Score : 366 pts
Problem Statement
Takahashi is the administrator of a network consisting of N computers and 1 central server. The N computers are numbered from 1 to N, and the central server is numbered as computer 0. That is, the entire network consists of a total of N + 1 computers: computers 0, 1, \ldots, N.
Each computer i (1 \leq i \leq N) is directly connected to a parent computer P_i (0 \leq P_i \leq N, P_i \neq i). These connections form a tree structure with N + 1 vertices rooted at the central server (computer 0). In this tree structure, a child computer of computer v refers to a computer j satisfying P_j = v.
One day, a virus infiltrated the network. Each computer i (1 \leq i \leq N) has a vulnerability value D_i assigned to it.
Virus infection targets only computers 1 through N. The central server (computer 0) is not subject to infection and will never become infected. Infection spreads in a chain reaction according to the following rules:
- Initial infection: All computers with a positive vulnerability value (D_i > 0) become infected. Computers with a vulnerability value of 0 or less (D_i \leq 0) are not infected at this stage. However, they may become infected later through Rule 2 below.
- Propagation of infection (child → parent direction): For a computer v (1 \leq v \leq N) that is not yet infected, let a be the number of infected child computers of v, and b be the number of non-infected child computers of v. If a > b is satisfied, then v becomes newly infected. This determination is made regardless of the vulnerability value D_v of computer v itself. That is, even if D_v \leq 0, v becomes infected if the condition on the infection status of its child computers is met. Note that for a computer with no child computers, a = b = 0, which does not satisfy a > b, so such a computer will not become infected by this rule. Infection only occurs in the direction where a parent becomes infected based on the infection status of its children. Even if a parent computer becomes infected, this does not cause its child computers to become newly infected.
- Repetition: The determination and infection in Rule 2 are repeated until no new computers become infected in a round. The determination in each round is performed simultaneously for all computers based on the infection status at the beginning of that round. That is, if multiple computers become newly infected in a given round, they all become infected simultaneously, and the infection status does not change partway through the same round. The determination in the next round is based on the updated infection status.
Determine the total number of computers that are ultimately infected. The central server (computer 0) is not subject to infection and should not be included in the count.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq P_i \leq N, P_i \neq i (1 \leq i \leq N)
- The given connections form a tree structure with N + 1 vertices rooted at computer 0
- -10^9 \leq D_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers
Input
N P_1 D_1 P_2 D_2 \vdots P_N D_N
- The first line contains an integer N representing the number of computers.
- Lines 2 through N + 1 give the information for each computer.
- Line 1 + i contains the parent computer number P_i of computer i and the vulnerability value D_i of computer i, separated by a space.
Output
Output the total number of computers that are ultimately infected, in a single line.
Sample Input 1
6 0 -1 1 1 1 2 1 0 2 -5 3 0
Sample Output 1
3
Sample Input 2
4 0 0 0 -3 0 -1 0 0
Sample Output 2
0
Sample Input 3
15 0 -5 1 -1 1 -1 1 0 2 1 2 1 2 0 3 -1 3 2 3 0 4 1 4 0 8 1 8 1 8 0
Sample Output 3
10
Sample Input 4
40 0 -100 1 0 1 -1 1 -1 2 -1 2 -1 2 3 3 -2 3 -2 3 -2 4 1 4 0 4 1 5 0 5 1 5 1 6 -1 6 2 7 0 7 -5 8 1 8 0 8 1 9 -1 9 -1 10 1 10 1 10 0 17 1 17 0 17 1 19 1 19 0 20 -1 20 -1 24 1 24 1 25 0 25 1 34 1
Sample Output 4
28
Sample Input 5
1 0 -1000000000
Sample Output 5
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君はリフォーム会社で働いており、正方形のタイルを使って部屋の床を敷き詰める仕事をしています。
高橋君は N 個の長方形の部屋の床にタイルを張る依頼を受けました。i 番目の部屋の床の縦の長さは H_i、横の長さは W_i です。
高橋君は、ちょうど1種類の正方形タイルを選び、そのタイルを使ってすべての部屋の床を敷き詰めたいと考えています。各部屋において、タイルは部屋の辺に平行に隙間なく・重なりなく並べます。正方形タイルの一辺の長さは正の整数でなければなりません。
一辺の長さ d の正方形タイルで部屋 i の床を敷き詰められるための必要十分条件は、H_i と W_i がともに d の倍数であることです。このとき、部屋 i に使うタイルの枚数は C_i = \frac{H_i}{d} \times \frac{W_i}{d} 枚です。
さらに、各部屋にはデザイン上の都合により「デザイン係数」S_i が指定されています。部屋 i に使うタイルの枚数 C_i は S_i の倍数でなければなりません。
高橋君は、できるだけ大きなタイルを使いたいと考えています。すべての部屋について上記の条件を同時に満たす正方形タイルの一辺の長さ d のうち、最大のものを求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq H_i \leq 10^9
- 1 \leq W_i \leq 10^9
- 1 \leq S_i \leq 10^9
- すべての i について H_i \times W_i は S_i の倍数である(これにより d = 1 は常に条件を満たすため、答えが存在することが保証される)
- 入力はすべて整数である
入力
N H_1 W_1 S_1 H_2 W_2 S_2 \vdots H_N W_N S_N
- 1 行目には、部屋の数を表す整数 N が与えられる。
- 2 行目から N + 1 行目では、各部屋の情報が与えられる。
- 1 + i 行目には、i 番目の部屋の縦の長さ H_i、横の長さ W_i、デザイン係数 S_i がスペース区切りで与えられる。
出力
条件を満たす正方形タイルの一辺の長さ d の最大値を 1 行で出力せよ。
入力例 1
2 6 8 6 10 12 15
出力例 1
2
入力例 2
1 12 12 8
出力例 2
3
入力例 3
6 48 72 8 90 60 25 84 126 14 108 144 12 150 210 35 66 132 11
出力例 3
6
入力例 4
15 123456000 789000000 96 250000000 400000000 125000000 999999000 888888000 27 314159000 271828000 4 500001000 700002000 9 655360000 131072000 1024 100003000 300009000 3 777777000 222222000 6 12345000 67890000 15 987654000 321000000 18 400000000 600000000 100000000 864000000 972000000 7776 135790000 246800000 10000 999000000 1000000000 999 720720000 840840000 1001
出力例 4
200
入力例 5
1 1000000000 1000000000 1000000000
出力例 5
10000
Score : 400 pts
Problem Statement
Takahashi works at a renovation company and is responsible for covering room floors with square tiles.
Takahashi has received requests to tile the floors of N rectangular rooms. The floor of the i-th room has height H_i and width W_i.
Takahashi wants to choose exactly one type of square tile and use it to cover all room floors. In each room, tiles are placed parallel to the room's edges without gaps or overlaps. The side length of the square tile must be a positive integer.
The necessary and sufficient condition for a square tile with side length d to be able to tile the floor of room i is that both H_i and W_i are multiples of d. In this case, the number of tiles used in room i is C_i = \frac{H_i}{d} \times \frac{W_i}{d}.
Furthermore, due to design requirements, each room has a specified "design coefficient" S_i. The number of tiles C_i used in room i must be a multiple of S_i.
Takahashi wants to use tiles that are as large as possible. Find the maximum side length d of a square tile that simultaneously satisfies the above conditions for all rooms.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq H_i \leq 10^9
- 1 \leq W_i \leq 10^9
- 1 \leq S_i \leq 10^9
- For all i, H_i \times W_i is a multiple of S_i (this guarantees that d = 1 always satisfies the conditions, so an answer always exists)
- All inputs are integers
Input
N H_1 W_1 S_1 H_2 W_2 S_2 \vdots H_N W_N S_N
- The first line contains an integer N representing the number of rooms.
- From the 2nd line to the (N + 1)-th line, information about each room is given.
- The (1 + i)-th line contains the height H_i, width W_i, and design coefficient S_i of the i-th room, separated by spaces.
Output
Output in one line the maximum value of the side length d of a square tile that satisfies the conditions.
Sample Input 1
2 6 8 6 10 12 15
Sample Output 1
2
Sample Input 2
1 12 12 8
Sample Output 2
3
Sample Input 3
6 48 72 8 90 60 25 84 126 14 108 144 12 150 210 35 66 132 11
Sample Output 3
6
Sample Input 4
15 123456000 789000000 96 250000000 400000000 125000000 999999000 888888000 27 314159000 271828000 4 500001000 700002000 9 655360000 131072000 1024 100003000 300009000 3 777777000 222222000 6 12345000 67890000 15 987654000 321000000 18 400000000 600000000 100000000 864000000 972000000 7776 135790000 246800000 10000 999000000 1000000000 999 720720000 840840000 1001
Sample Output 4
200
Sample Input 5
1 1000000000 1000000000 1000000000
Sample Output 5
10000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 433 点
問題文
高橋君は花壇の管理を担当しています。花壇には N 本の花が一列に植えられており、左から順に 1, 2, \ldots, N と番号が付けられています。
各花 i (1 \le i \le N) には「品種番号」 A_i と「高さ」 B_i が記録されています。高橋君は、連続する花の区間を選んで展示コーナーを作りたいと考えています。展示コーナーとして選ぶ区間 [l, r] (1 \le l \le r \le N) は、花 l から花 r までの連続した花の並びです(l = r、すなわち花が 1 本だけの場合も含みます)。
展示コーナーの見栄えをよくするため、高橋君は以下の 2 つの条件を同時に満たす 区間を選びたいと考えています。
条件 1(品種の多様さに関する条件):
区間 [l, r] に含まれる花の品種番号 A_l, A_{l+1}, \ldots, A_r の中に含まれる 異なる値の個数 を D とします。D と区間の長さ (r - l + 1) の積が K 以下でなければなりません。すなわち、
D \times (r - l + 1) \le K
を満たす必要があります。
条件 2(高さのバランスに関する条件):
区間 [l, r] に含まれる花の高さの最大値と最小値の差が M 以下でなければなりません。すなわち、
\max_{l \le i \le r} B_i - \min_{l \le i \le r} B_i \le M
を満たす必要があります。
上記の 2 条件を同時に満たす区間 [l, r] (1 \le l \le r \le N) の個数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^{18}
- 0 \leq M \leq 10^9
- 1 \leq A_i \leq N
- 1 \leq B_i \leq 10^9
- 入力はすべて整数である。
入力
N K M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
- 1 行目には、花の本数を表す整数 N、条件 1 における積の上限を表す整数 K、条件 2 における高さの差の上限を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各花の品種番号を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
- 3 行目には、各花の高さを表す整数 B_1, B_2, \ldots, B_N が、スペース区切りで与えられる。
出力
条件 1 と条件 2 を同時に満たす区間 [l, r] (1 \le l \le r \le N) の個数を 1 行で出力せよ。
入力例 1
5 6 3 1 2 1 3 2 4 5 7 6 8
出力例 1
10
入力例 2
4 2 0 1 1 2 2 5 5 6 6
出力例 2
6
入力例 3
12 20 10 1 2 3 2 1 4 4 5 3 2 6 1 10 12 15 18 14 20 21 19 13 11 16 17
出力例 3
48
入力例 4
40 100 50 1 2 3 4 5 1 2 6 7 8 3 4 9 10 1 11 12 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 100 120 130 160 170 150 140 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 470 480 490 500
出力例 4
216
入力例 5
1 1 0 1 1000000000
出力例 5
1
Score : 433 pts
Problem Statement
Takahashi is in charge of managing a flowerbed. The flowerbed has N flowers planted in a row, numbered 1, 2, \ldots, N from left to right.
Each flower i (1 \le i \le N) has a recorded "variety number" A_i and "height" B_i. Takahashi wants to select a contiguous interval of flowers to create an exhibition section. An interval [l, r] (1 \le l \le r \le N) chosen as the exhibition section is the contiguous sequence of flowers from flower l to flower r (including the case where l = r, i.e., only a single flower).
To make the exhibition section look attractive, Takahashi wants to choose an interval that satisfies both of the following 2 conditions simultaneously.
Condition 1 (Variety diversity condition):
Let D be the number of distinct values among the variety numbers A_l, A_{l+1}, \ldots, A_r of flowers in the interval [l, r]. The product of D and the length of the interval (r - l + 1) must be at most K. That is,
D \times (r - l + 1) \le K
must be satisfied.
Condition 2 (Height balance condition):
The difference between the maximum and minimum heights of flowers in the interval [l, r] must be at most M. That is,
\max_{l \le i \le r} B_i - \min_{l \le i \le r} B_i \le M
must be satisfied.
Find the number of intervals [l, r] (1 \le l \le r \le N) that satisfy both conditions above simultaneously.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^{18}
- 0 \leq M \leq 10^9
- 1 \leq A_i \leq N
- 1 \leq B_i \leq 10^9
- All inputs are integers.
Input
N K M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
- The first line contains the integer N representing the number of flowers, the integer K representing the upper bound of the product in Condition 1, and the integer M representing the upper bound of the height difference in Condition 2, separated by spaces.
- The second line contains the integers A_1, A_2, \ldots, A_N representing the variety number of each flower, separated by spaces.
- The third line contains the integers B_1, B_2, \ldots, B_N representing the height of each flower, separated by spaces.
Output
Output in one line the number of intervals [l, r] (1 \le l \le r \le N) that satisfy both Condition 1 and Condition 2 simultaneously.
Sample Input 1
5 6 3 1 2 1 3 2 4 5 7 6 8
Sample Output 1
10
Sample Input 2
4 2 0 1 1 2 2 5 5 6 6
Sample Output 2
6
Sample Input 3
12 20 10 1 2 3 2 1 4 4 5 3 2 6 1 10 12 15 18 14 20 21 19 13 11 16 17
Sample Output 3
48
Sample Input 4
40 100 50 1 2 3 4 5 1 2 6 7 8 3 4 9 10 1 11 12 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 100 120 130 160 170 150 140 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 470 480 490 500
Sample Output 4
216
Sample Input 5
1 1 0 1 1000000000
Sample Output 5
1