Painting Fence Posts
Time limit3sMemory limit1024 MB
Given a rectilinear fence loop and cow walks that take the shorter route, count how many times each post is touched.
- Level
Medium6 of 10
- Topics
- Prefix sum, Sorting, Geometry
- Solved
- No attempts yet
Problem
Farmer John's cows () each like to take a daily walk around the fence that encloses his pasture. Whenever a cow walks past a fence post, she brushes against it, so Farmer John has to repaint the posts regularly.
The fence has posts (, even). Each post is a different 2D point on a map of the farm (). Each post connects to the two adjacent posts by a fence segment that is either vertical or horizontal, so the fence is a polygon whose sides are parallel to the x or y axis. The last post connects back to the first post, forming a closed loop around the pasture. Fence segments overlap only at their endpoints. Each post is the endpoint of exactly two fence segments, and any two segments that meet at an endpoint are perpendicular. Given the post locations, the fence is uniquely determined.
Each cow has a preferred starting point and ending point on the fence. These may be at posts or between posts. A cow walks from her start to her end along the fence. Since the fence is a closed loop, there are two possible routes, and each cow takes the shorter one. There are never ties.
A cow touches a post if she walks past it, or if the post is her start or end point. Compute the number of daily touches each post receives, so Farmer John knows which post to repaint next.
Input
The first line contains and . Each of the next lines contains two integers, the position of a fence post. The posts are listed in no particular order. Each of the next lines contains four integers , the start and end of one cow's walk.
Output
Print integers, the number of touches received by each fence post.