Under construction

Still being built. Things may change

Painting Fence Posts

Time limit3sMemory limit1024 MB

Summary
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 NN cows (1≤N≤1051 \leq N \leq 10^5) 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 PP posts (4≤P≤2⋅1054 \leq P \leq 2\cdot 10^5, PP even). Each post is a different 2D point (x,y)(x,y) on a map of the farm (0≤x,y≤1090 \leq x, y \leq 10^9). 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 NN and PP. Each of the next PP lines contains two integers, the position (x,y)(x, y) of a fence post. The posts are listed in no particular order. Each of the next NN lines contains four integers x1x_1 y1y_1 x2x_2 y2y_2, the start (x1,y1)(x_1, y_1) and end (x2,y2)(x_2, y_2) of one cow's walk.

Output

Print PP integers, the number of touches received by each fence post.

Examples3

  1. Example 1

    Input
    5 4
    3 1
    1 5
    3 5
    1 1
    2 1 1 5
    1 5 3 4
    3 1 3 5
    2 1 2 1
    3 2 3 3
    
    Expected output
    1
    2
    2
    1
    
  2. Example 2

    Input
    2 8
    1 1
    1 2
    0 2
    0 3
    0 0
    0 1
    2 3
    2 0
    1 1 2 1
    1 0 1 3
    
    Expected output
    1
    0
    0
    0
    1
    1
    1
    2
    
  3. Example 3

    Input
    1 12
    0 0
    2 0
    2 1
    1 1
    1 2
    3 2
    3 3
    1 3
    1 4
    2 4
    2 5
    0 5
    2 2 0 2
    
    Expected output
    1
    1
    1
    1
    1
    0
    0
    0
    0
    0
    0
    0