QOJ.ac

QOJ

Time Limit: 5 s Memory Limit: 256 MB Total points: 100

#7822. Target

Statistics

A modern laser shooting range has decided to assemble a new target. This target consists of a rectangular region completely covered by rectangular digital screens of various sizes. Periodically, various objects appear on the screens, and one must hit them.

However, assembling such a target turned out to be difficult. The instructions did not specify exactly where each screen should be attached. Instead, for each screen, it listed the screens adjacent to it that share a common boundary segment when traversing the perimeter of the screen clockwise, starting from the bottom-left corner. The orientation of the screen was unambiguous, so it was clear where the top and bottom were, and there were no problems.

The assembled target was connected to a computer to automatically determine, based on the coordinates of the point where the laser hits, which screen (or screens, in case of hitting a boundary) the laser hit. Unfortunately, the disk with this program was lost. Help the shooting range staff and write a program that will determine the screen(s) hit by the laser, knowing the coordinates of the hit point and the information about the arrangement of the screens in the instructions.

Input

The first line contains two positive integers separated by a space: $N$ — the number of screens and $K$ — the number of hits, $1 \le N \le 10\,000$, $1 \le K \le 40\,000$.

Next, there are $N$ blocks, where the $i$-th block describes the $i$-th screen (screens are numbered starting from 1). Each block consists of two lines:

  • The first line contains three positive integers separated by a space: $W_i$ — the screen size along the $X$ axis, $H_i$ — the screen size along the $Y$ axis, $G_i$ — the number of boundary segments of the screen. $1 \le W_i, H_i \le 10^6$, $4 \le G_i \le \max(4, N + 2)$.
  • The second line contains $G_i$ integers separated by a space — the numbers of the screens adjacent to the $i$-th screen when traversing its boundary, starting from the bottom-left corner. If a boundary segment is external (i.e., there is no screen on the other side of this boundary segment), then instead of the adjacent screen number, it is $-1$.

Next, there are $K$ lines, each of which specifies one laser hit point. The $j$-th line contains two integers separated by a space — the coordinates of the $j$-th hit $X_j$ and $Y_j$, $-100 \le X_j, Y_j \le 10^7$.

The origin of the coordinate system is the bottom-left corner of the rectangular target composed of all the given screens. The $X$ axis is directed to the right, and the $Y$ axis is directed upwards.

Output

For each of the $K$ hits, output on the $j$-th line the integers separated by a space — the numbers of the screens that the $j$-th hit landed on, sorted in ascending order. If for the $j$-th point there are no such screens (the shot missed the target), output $-1$ on the $j$-th line.

Examples

Input 1

3 5
1 10 100 5
-1 -1 -1 2 3
10 1000 4
3 1 -1 -1
100 1000 4
-1 1 2 -1
100 1
5 5
105 1000
105 1100
150 2000

Output 1

2 3
3
1 2
1
-1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.