#P4737. Buffalo Land Claim

    ID: 17981 Type: Default 1000ms 256MiB

Buffalo Land Claim

Buffalo Land Claim

A pasture in the Wild West is modeled as the non‐negative quadrant of the Cartesian plane. There are n buffalos, each occupying a distinct unit square. Buffalo j is located in the unit square whose upper–right corner is at (xj, yj) (all coordinates are integers). The two coordinate axes are rivers that limit movement to the left and downward.

A total of m settlers arrive one by one. When a settler arrives, he performs the following steps:

  1. The settler picks a point with integer coordinates and places a fence post there. This point is free (no fence posts or fence segments exist there) and no two fence posts share the same x‐ or y–coordinate.
  2. Starting from the fence post at (a, b), he constructs two fence segments: one horizontally leftwards and one vertically downwards. Each segment is built to be as long as possible – extending until it meets either a river (i.e. the coordinate axis) or an existing fence.
  3. The settler then claims the entire connected region that is bounded by the newly built fences along with any existing fences and rivers. This region is exactly the set of unit squares whose upper–right corners (x, y) satisfy
    \( L < x \le a \) and \( D < y \le b \),
    where
    \( L = \max\{ x_j : x_j b \} \) (if no such fence post exists, take \( L = 0 \)) and \( D = \max\{ y_j : y_j a \} \) (if no such fence post exists, take \( D = 0 \)).

In addition to claiming the land, the settler also claims all buffalos located within that region. Note that later settlers may claim land (and buffalos) that have been claimed earlier.

Your task is: For each settler (in order of arrival), compute the total number of buffalos he claimed when he arrived.

inputFormat

The first line contains two integers n and m, representing the number of buffalos and the number of settlers respectively.

The next n lines each contain two integers x and y, denoting that a buffalo is located in the unit square whose upper–right corner is at \( (x, y) \).

The following m lines each contain two integers a and b, representing the coordinate of the fence post chosen by a settler in order of arrival.

outputFormat

Output m lines. The i–th line should contain one integer: the number of buffalos claimed by the i–th settler upon his arrival.

sample

3 2
2 3
4 5
1 1
5 7
7 5
3

0

</p>