E - Overtaking on a Straight Course Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466 点

問題文

高橋君と青木君は、一直線のランニングコースを走っています。このコースにはスタート地点と、その先に N 個の給水所が一列に並んでおり、スタート地点から近い順に給水所 1, 2, \ldots, N と番号が付けられています。給水所 N がこのコースのゴールです。

スタート地点から給水所 1 までの距離、および隣り合う給水所間の距離はすべて 1 です。高橋君は一定の速さ毎秒 V で走り、青木君は一定の速さ毎秒 W で走ります。

高橋君と青木君はそれぞれ、各給水所で立ち止まって給水することがあります。高橋君は給水所 i に到着すると R_i 秒間その場に立ち止まってから走行を再開し、青木君は給水所 i に到着すると S_i 秒間その場に立ち止まってから走行を再開します(0 秒の場合は立ち止まらずそのまま通過します)。ただし、ゴールである給水所 N では到着した時点で走行を終了し、R_N, S_N の値によらず給水のための停止は行いません。ゴールに到着した走者は、その後も観測終了時刻まで給水所 N にいるものとします。

二人は時刻 0 に同時にスタート地点を出発し、それぞれの速さでゴール方向(給水所の番号が増える方向)に向かって走ります。

時刻 t における高橋君のスタート地点からの距離を f(t)、青木君のスタート地点からの距離を g(t) とします。ある走者がもう一方の走者を 追い越す とは、f(t) - g(t) の符号が変わることを指します。厳密には、次のいずれかが発生することです:

  • ある時刻 t_0 が存在して、十分小さい正の数 \varepsilon に対し、t_0 - \varepsilon < t < t_0 のとき f(t) < g(t) であり、t_0 < t < t_0 + \varepsilon のとき f(t) > g(t) である。これは高橋君が青木君を追い越したことに相当する。
  • ある時刻 t_0 が存在して、十分小さい正の数 \varepsilon に対し、t_0 - \varepsilon < t < t_0 のとき f(t) > g(t) であり、t_0 < t < t_0 + \varepsilon のとき f(t) < g(t) である。これは青木君が高橋君を追い越したことに相当する。

f(t) = g(t) が一定時間続いた後に前後関係が入れ替わる場合も、追い越し 1 回と数えます。一方、f(t) = g(t) となった後に前後関係が入れ替わらず元に戻る場合は追い越しとは数えません。

以下の注意事項があります:

  • 時刻 0 の時点では二人は同じ位置(スタート地点)にいます。出発前には前後関係が存在しないため、出発直後に一方が前に出ることは追い越しとは数えません。
  • 観測区間の終了時刻は、二人のうち 遅い方 が給水所 N に到着する時刻 T とします。時刻 T ちょうどに前後関係が入れ替わる場合(すなわち上の定義における t_0 = T の場合)は、t_0 より後の状態を観測できないため、追い越しとは数えません。
  • 給水所で立ち止まっている走者をもう一方が走行中に通過して前後関係が入れ替わる場合も、上の定義に従い追い越しとして数えます。

二人が時刻 0 にスタート地点を出発してから時刻 T までの間に発生する追い越しの合計回数を求めてください。高橋君が青木君を追い越す場合と、青木君が高橋君を追い越す場合の両方を数えます。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq V \leq 10^9
  • 1 \leq W \leq 10^9
  • V \neq W
  • 0 \leq R_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数

入力

N V W
R_1 R_2 \ldots R_N
S_1 S_2 \ldots S_N
  • 1 行目には、給水所の数を表す整数 N、高橋君の速さを表す整数 V、青木君の速さを表す整数 W が、スペース区切りで与えられる。
  • 2 行目には、高橋君が給水所 i で立ち止まる時間(秒)を表す整数 R_i が N 個、スペース区切りで与えられる。
  • 3 行目には、青木君が給水所 i で立ち止まる時間(秒)を表す整数 S_i が N 個、スペース区切りで与えられる。

出力

二人がスタート地点を出発してから両者がともに給水所 N に到着するまでの間に発生する追い越しの合計回数を 1 行で出力せよ。


入力例 1

3 2 1
0 3 0
1 0 0

出力例 1

1

入力例 2

4 1 3
2 0 4 0
0 5 0 0

出力例 2

2

入力例 3

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

出力例 3

5

入力例 4

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

出力例 4

8

入力例 5

1 1000000000 1
1000000000
0

出力例 5

0

Score : 466 pts

Problem Statement

Takahashi and Aoki are running on a straight running course. This course has a start point, and N water stations are aligned in a straight line ahead of it, numbered 1, 2, \ldots, N in increasing order of distance from the start point. Water station N is the goal of this course.

The distance from the start point to water station 1, as well as the distance between any two adjacent water stations, is 1. Takahashi runs at a constant speed of V per second, and Aoki runs at a constant speed of W per second.

Takahashi and Aoki may stop at each water station to hydrate. Upon arriving at water station i, Takahashi stops for R_i seconds before resuming running, and Aoki stops for S_i seconds before resuming running (if the time is 0 seconds, they pass through without stopping). However, upon arriving at the goal, water station N, they finish running immediately and do not stop for hydration, regardless of the values of R_N and S_N. A runner who has reached the goal remains at water station N until the end of the observation period.

Both runners start from the start point simultaneously at time 0 and run towards the goal (the direction in which the water station numbers increase) at their respective speeds.

Let f(t) be Takahashi's distance from the start point at time t, and g(t) be Aoki's distance from the start point at time t. We say that one runner overtakes the other when the sign of f(t) - g(t) changes. Strictly speaking, it refers to the occurrence of either of the following:

  • There exists a time t_0 such that, for a sufficiently small positive number \varepsilon, f(t) < g(t) holds for t_0 - \varepsilon < t < t_0, and f(t) > g(t) holds for t_0 < t < t_0 + \varepsilon. This corresponds to Takahashi overtaking Aoki.
  • There exists a time t_0 such that, for a sufficiently small positive number \varepsilon, f(t) > g(t) holds for t_0 - \varepsilon < t < t_0, and f(t) < g(t) holds for t_0 < t < t_0 + \varepsilon. This corresponds to Aoki overtaking Takahashi.

If f(t) = g(t) persists for some duration before their relative order changes, this is also counted as 1 overtaking. On the other hand, if f(t) = g(t) occurs but their relative order does not change afterwards, it is not counted as an overtaking.

Please note the following:

  • At time 0, both runners are at the same position (the start point). Since there is no relative order before departure, one runner pulling ahead immediately after starting is not counted as an overtaking.
  • The end of the observation period is the time T when the slower of the two runners arrives at water station N. If their relative order changes exactly at time T (i.e., t_0 = T in the definition above), it is not counted as an overtaking because the state after t_0 cannot be observed.
  • If one runner passes the other who is stopped at a water station, changing their relative order, this is also counted as an overtaking according to the definition above.

Find the total number of overtakings that occur from the time they start at time 0 until time T. Count both the cases where Takahashi overtakes Aoki and where Aoki overtakes Takahashi.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq V \leq 10^9
  • 1 \leq W \leq 10^9
  • V \neq W
  • 0 \leq R_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

N V W
R_1 R_2 \ldots R_N
S_1 S_2 \ldots S_N
  • The first line contains three space-separated integers: N, the number of water stations; V, Takahashi's speed; and W, Aoki's speed.
  • The second line contains N space-separated integers R_i, representing the time (in seconds) Takahashi stops at water station i.
  • The third line contains N space-separated integers S_i, representing the time (in seconds) Aoki stops at water station i.

Output

Print the total number of overtakings that occur from the moment they start until both of them arrive at water station N in a single line.


Sample Input 1

3 2 1
0 3 0
1 0 0

Sample Output 1

1

Sample Input 2

4 1 3
2 0 4 0
0 5 0 0

Sample Output 2

2

Sample Input 3

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

Sample Output 3

5

Sample Input 4

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

Sample Output 4

8

Sample Input 5

1 1000000000 1
1000000000
0

Sample Output 5

0