#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 buffalos is scattered throughout the grid, each occupying a unit square. Buffalos are numbered through ; buffalo is located in the unit square whose upper-right corner is the point with integer coordinates . The coordinate axes represent two rivers meeting at the origin, restricting buffalo movement downwards and leftwards.
A total of settlers arrive, one by one, and each claims a piece of land using the following procedure:
- 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 -coordinate and no two fence posts will have the same -coordinates.
- 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.
- 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 — the number of buffalos. The of the following lines contains two integers and — the location of the buffalo.No two buffalos will share the same location. The following line contains an integer — the number of settlers. The of the following lines contains two integers and — the coordinates of the fence postinstalled by the settler. All are different and all are different.
输出格式
Output lines. The line should contain the number of buffalos claimed by the 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