Count interior crossings of perpendicular mower segments cut at least T days apart.
Hard8Segment treeGeometrySliding windowNo attempts yetTime limit5sMemory limit512 MBFarmer 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 (x1,y1), and on day d he mows along a straight segment from the previous day's position to (xd,yd). On the 2D map of the farm every move is horizontal or vertical, so xd=xd−1 or yd=yd−1. 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 d reappears on day d+T, so whenever his path meets a segment he cut at least T 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.
The first line contains N (2≤N≤100000) and T (1≤T≤N, T even).
Each of the next N lines gives the position of the mower on days 1 through N. Line i contains the integers xi and yi, both between 0 and 109.
Two consecutive positions may be equal. The move on such a day is a single point and takes part in no crossing.
Print the number of crossing points described above.
In the first example the path of day 7 meets the segment cut on day 2. The two days are T=4 or more apart, so this crossing counts. The other two crossings are only 3 days apart and do not count.