The sky can be seen as a one-dimensional axis. The moon is at the origin whose coordinate is 00.
There are nn clouds floating in the sky. Each cloud has the same length ll. The ii-th initially covers the range of (xi,xi+l)(xi,xi+l) (endpoints excluded). Initially, it moves at a velocity of vivi, which equals either 11 or −1−1.
Furthermore, no pair of clouds intersect initially, that is, for all 1≤i<j≤n1≤i<j≤n, |xi−xj|≥l|xi−xj|≥l.
With a wind velocity of ww, the velocity of the ii-th cloud becomes vi+wvi+w. That is, its coordinate increases by vi+wvi+w during each unit of time. Note that the wind can be strong and clouds can change their direction.
You are to help Mino count the number of pairs (i,j)(i,j) (i<ji<j), such that with a proper choice of wind velocity ww not exceeding wmaxwmax in absolute value (possibly negative and/or fractional), the ii-th and jj-th clouds both cover the moon at the same future moment. This wwdoesn't need to be the same across different pairs.
The first line contains three space-separated integers nn, ll, and wmaxwmax (1≤n≤1051≤n≤105, 1≤l,wmax≤1081≤l,wmax≤108) — the number of clouds, the length of each cloud and the maximum wind speed, respectively.
The ii-th of the following nn lines contains two space-separated integers xixi and vivi (−108≤xi≤108−108≤xi≤108, vi∈{−1,1}vi∈{−1,1}) — the initial position and the velocity of the ii-th cloud, respectively.
The input guarantees that for all 1≤i<j≤n1≤i<j≤n, |xi−xj|≥l|xi−xj|≥l.
Output one integer — the number of unordered pairs of clouds such that it's possible that clouds from each pair cover the moon at the same future moment with a proper choice of wind velocity ww.
5 1 2 -2 1 2 1 3 -1 5 -1 7 -1
4
4 10 1 -20 1 -10 -1 0 1 10 -1
1
In the first example, the initial positions and velocities of clouds are illustrated below.
The pairs are:
Below is the positions of clouds at time 2.52.5 with w=−0.4w=−0.4. At this moment, the 11-st and 33-rd clouds both cover the moon.
In the second example, the only pair is (1,4)(1,4), covering the moon at time 1515 with w=0w=0.
Note that all the times and wind velocities given above are just examples among infinitely many choices.
思路上大體上就是二分了,顯然只有初始速度方向相反的雲才能相遇,所以猜測應該就是將方向不同的雲記錄在兩個數組裏,然後枚舉其中一個,二分另一個。然而應該用什麼指標來判斷雲是否能相遇呢?到這兒騷操作就來了,我們想象沒有風在吹,月亮自己在跑,如下圖。
橙色代表月亮的軌跡,藍色代表雲的軌跡。如果兩個藍條能在橙色部分相遇,那麼就說明有一對雲是符合要求的。根據w必定大於1,可知橙色線和橫軸夾角不得大於45°。那就保證藍條相交得到的菱形,上面的頂點在橙色區域裏,因此xj−xi+l2>|xj−xi+l2+xi|/w。
從第一個滿足這個條件的雲開始,往後的雲都滿足這個條件,我們把它加在結果裏。
代碼奇醜無比,不貼了。