#P3136. Farmer John's Mowing Crossings

    ID: 16394 Type: Default 1000ms 256MiB

Farmer John's Mowing Crossings

Farmer John's Mowing Crossings

Farmer John is reliable in most aspects of managing his farm except one: he mows very slowly. He starts on day 1 at a point \( (x_1,y_1) \) and on day \( d \) he moves from \( (x_{d-1},y_{d-1}) \) to \( (x_d,y_d) \) along a straight line. The move is either horizontal or vertical, i.e. either \( x_d=x_{d-1} \) or \( y_d=y_{d-1} \). Moreover, FJ alternates between horizontal and vertical moves on successive days.

However, the grass grows back. Any section cut on day \( d \) will reappear on day \( d+T \). Thus if FJ's current mowing path (on day \( d' \)) crosses a segment mowed on day \( d \) with \( d'\ge d+T \), then he will cut grass at the same point again. Only perpendicular crossings are counted, meaning that the intersection point happens between a horizontal and a vertical segment, and the crossing point is not an endpoint of either segment.

Your task is to count the total number of such crossing events.

Note: The intersection between a horizontal segment having endpoints \( (x_{1},y) \) and \( (x_{2},y) \) (with \( x_{1} < x_{2} \)) and a vertical segment having endpoints \( (x,y_{1}) \) and \( (x,y_{2}) \) (with \( y_{1} < y_{2} \)) is valid if and only if
\( x_{1} < x < x_{2} \) and \( y_{1} < y < y_{2} \).

inputFormat

The first line of input contains two integers \( N \) and \( T \), where \( N \) (\( N\geq2 \)) is the number of days (the first day is only a starting position) and \( T \) is the regrowth time.

Each of the next \( N \) lines contains two space-separated integers \( x_i \) and \( y_i \), representing FJ's position on day \( i \). The moves from day \( i \) to day \( i+1 \) (for \( 1 \le i \lt N \)) are such that the move is either horizontal or vertical and the directions alternate.

outputFormat

Output a single integer representing the number of times Farmer John's mowing path crosses a segment on which the grass has regrown (taking into account only perpendicular crossings, where the crossing point is not an endpoint of either segment).

sample

6 3
0 0
0 1
1 1
1 0
2 0
2 2
0

</p>