#P3136. [USACO16JAN] Mowing the Field P

[USACO16JAN] Mowing the Field P

题目描述

Farmer John is quite reliable in all aspects of managing his farm, except one: he is terrible at mowing the grass in a timely fashion. He only manages to move the mowing machine once per day, in fact. On day 1, he starts at position (x1,y1)(x_1, y_1) and on day dd he mows along a straight segment to the position (xd,yd)(x_d, y_d), moving either horizontally or vertically on the 2D map of his farm; that is, either xd=xd1x_d = x_{d-1}, or yd=yd1y_d = y_{d-1}. FJ alternates between horizontal and vertical moves on successive days.

So slow is FJ's progress that some of the grass he mows might grow back before he is finished with all his mowing. Any section of grass that is cut in day dd will reappear on day d+Td + T, so if FJ's mowing path crosses a path he cut at least TT days earlier, he will end up cutting grass at the same point again. In an effort to try and reform his poor mowing strategy, FJ would like to count the number of times this happens.

Please count the number of times FJ's mowing path crosses over an earlier segment on which grass has already grown back. You should only count "perpendicular" crossings, defined as a point in common between a horizontal and a vertical segment that is an endpoint of neither.

输入格式

The first line of input contains NN (2N100,0002 \leq N \leq 100,000) and TT

(1TN1 \leq T \leq N, TT even).

The next NN lines describe the position of the mower on days 1N1 \ldots N. The

iith of these lines contains integers xix_i and yiy_i (nonnegative integers

each at most 1,000,000,000).

输出格式

Please output a count of the number of crossing points described above, where FJ

re-cuts a point of grass that had grown back after being cut earlier.

题目大意

FJ 在给农场除草。

FJ 第一天位于坐标 (x1,y1)(x_1,y_1),此后的每一天,FJ 都会水平或竖直地移动到另一个坐标并除去其上的草。具体而言:我们认为 (x,y)(x+k,y)(x,y)\rightarrow (x+k,y) 视为一次水平移动,(x,y)(x,y+k)(x,y)\rightarrow (x,y+k) 视为一次竖直移动。

FJ 在连续的两天内不会使用同一种移动方式进行移动,即 FJ 使用水平移动以及竖直移动是交替的。

FJ 在第 dd 天除去的草会在 d+Td+T 天再次生长出来,其中 TT 是一个偶数

如果某次移动与先前的另外一次移动垂直交叉(即其中一个是水平移动,另外一个是竖直移动)且满足先前一次除去的草已经再次生长,那么就产生一次计数。

FJ 想知道,按照当前计划进行除草会产生多少次计数。

形式化题意

现给出 nn 个点,其中,第 ii 个点和第 i+1i+1 个点相连必定构成一条水平或竖直的线段,记为线段 ii。显然线段 ii 必定与线段 i+1i+1 共用一个端点。

额外保证线段 ii 必定与线段 i+1i+1 垂直。

对于某条线段 dd,你需要统计出线段 1dT1\cdots d-T 中有多少条与线段 dd 相交且垂直,记为 fdf_d

输出 ansans,其中 ans=i=1n1fians=\sum_{i=1}^{n-1} f_i

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

提示

Here, FJ crosses on day 7 a segment of grass he cut on day 2, which counts. The

other intersections do not count.

Note: This problem has expanded limits: 5 seconds per test case (10 for Python and Java), and 512 MB of memory.