#P4737. [CERC2017] Buffalo Barricades

[CERC2017] Buffalo Barricades

题目描述

A pasture in the wild west can be represented as a rectangular grid embedded in the upper-right quadrant of a standard coordinate system. A herd of nn buffalos is scattered throughout the grid, each occupying a unit square. Buffalos are numbered 11 through nn; buffalo jj is located in the unit square whose upper-right corner is the point with integer coordinates (xj,yj)(x_j,y_j). The coordinate axes represent two rivers meeting at the origin, restricting buffalo movement downwards and leftwards.

A total of mm settlers arrive, one by one, and each claims a piece of land using the following procedure:

  1. The settler picks a point with integer coordinates and installs a single fence post at that point.The point he picks is guaranteed to be free of any previously installed fence posts or fences.Moreover, no two fence posts will have the same xx-coordinate and no two fence posts will have the same yy-coordinates.
  2. Starting from the fence post, the settler builds horizontal and vertical fence segments leftwards and downwards, respectively. Each segment is built to be as long as possible — i. e. until it reaches the river or another fence.
  3. The settler claims all the land in the connected area bounded with fences and rivers whose upper-right corner is his fence post. Of course, he claims all the buffalos inside as well. Note that settlers arriving later may claim pieces of land already claimed by earlier settlers.

For each settler, find the total number of buffalos he claimed when he arrived.

输入格式

The first line contains an integer n(1n300000)n(1 \le n \le 300 000) — the number of buffalos. The jthj-th of the following nn lines contains two integers xjx_j and yj(1xj,yj109)y_j(1 \le x_j,y_j \le 10^9) — the location of the jthj-th buffalo.No two buffalos will share the same location. The following line contains an integer m(1m300000)m(1 \le m \le 300 000) — the number of settlers. The jthj-th of the following mm lines contains two integers xjx^{'}_{j} and yj(1xj,yj109)y^{'}_{j}(1 \le x^{'}_{j},y^{'}_{j} \le 10^9) — the coordinates of the fence postinstalled by the jthj-th settler. All xjx^{'}_{j} are different and all yjy^{'}_{j} are different.

输出格式

Output mm lines. The jthj-th line should contain the number of buffalos claimed by the jthj-th settler upon arrival.

7
1 1
4 2
6 2
5 3
2 5
4 7
7 5
4
4 4
8 2
9 6
6 5
2
1
3
2