Mowing the Field
Time limit5sMemory limit512 MB
Count interior crossings of perpendicular mower segments cut at least T days apart.
- Level
Hard8 of 10
- Topics
- Segment tree, Geometry, Sliding window
- Solved
- No attempts yet
Problem
Farmer John runs his farm well in almost every way, except that he is always late with the mowing. He moves the mower only once a day. On day 1 he starts at , and on day he mows along a straight segment from the previous day's position to . On the 2D map of the farm every move is horizontal or vertical, so or . John alternates horizontal and vertical moves on successive days.
He works so slowly that grass he already cut grows back before he is done. Grass cut on day reappears on day , so whenever his path meets a segment he cut at least days earlier, he cuts the same spot a second time. John wants to know how bad his routine is, so he wants to count how often that happens.
Count the crossing points where John re-cuts grass that had grown back. Only perpendicular meetings count, meaning a point shared by a horizontal segment and a vertical segment that is an endpoint of neither segment.
Input
The first line contains () and (, even).
Each of the next lines gives the position of the mower on days through . Line contains the integers and , both between and .
Two consecutive positions may be equal. The move on such a day is a single point and takes part in no crossing.
Output
Print the number of crossing points described above.
Hint
In the first example the path of day 7 meets the segment cut on day 2. The two days are or more apart, so this crossing counts. The other two crossings are only 3 days apart and do not count.