This page is still under construction.

Parts of this page are still being built. What you see may change.

Buffalo Barricades

Time limit5sMemory limit512 MB

Summary
For each settler arriving in order, count the buffalos inside the region bounded by rivers and fences whose upper right corner is the settler's post.
Level

Hard8 of 10

Topics
Sorting, Prefix sum, Stack, BFS
Solved
No attempts yet

Problem

A pasture in the wild west is a rectangular grid in the upper right quadrant of the coordinate plane. nn buffalos are scattered over the pasture, and each one occupies a single unit square. The buffalos are numbered 11 through nn, and buffalo jj stands in the unit square whose upper right corner is the integer point (xj,yj)(x_j, y_j). The two coordinate axes are rivers that meet at the origin, so no buffalo can leave to the left or downward.

mm settlers arrive one at a time, and each of them claims land this way.

  1. The settler picks an integer point and drives one fence post into it. The chosen point holds no earlier post, and no earlier fence runs through it. No two posts share an xx coordinate, and no two posts share a yy coordinate.
  2. Starting at the post, the settler builds a horizontal fence to the left and a vertical fence downward. Each fence runs as far as it can, until it reaches a river or a fence that is already standing.
  3. The settler takes the whole connected area bounded by fences and rivers whose upper right corner is his post, together with every buffalo inside it. A settler who arrives later may take land that an earlier settler already claimed.

For each settler, find how many buffalos he claimed at the moment he arrived.

Input

The first line contains the number of buffalos nn (1≤n≤3000001 \le n \le 300000). The jj-th of the next nn lines contains two integers xjx_j and yjy_j (1≤xj,yj≤1091 \le x_j, y_j \le 10^9), the position of buffalo jj. No two buffalos share a position.

The next line contains the number of settlers mm (1≤m≤3000001 \le m \le 300000). The jj-th of the next mm lines contains two integers xj′x'_j and yj′y'_j (1≤xj′,yj′≤1091 \le x'_j, y'_j \le 10^9), the post of the jj-th settler. All xj′x'_j are different, and all yj′y'_j are different.

Output

Print mm lines. Line jj contains the number of buffalos claimed by the jj-th settler when he arrived.

Hint

The picture shows the pasture of the first example after all four settlers have arrived. The gray disks are buffalos, and the white circles are fence posts.

Examples1

  1. Example 1

    Input
    7
    1 1
    4 2
    6 2
    5 3
    2 5
    4 7
    7 5
    4
    4 4
    8 2
    9 6
    6 5
    
    Expected output
    2
    1
    3
    2