Mosaic
Time limit1sMemory limit1024 MB
Given up to 100000 rectangles in a fixed order, answer range queries asking for any two pieces in the range that share no matching side length.
- Level
Hard8 of 10
- Topics
- Array, Sorting, Divide and conquer, Prefix sum
- Solved
- No attempts yet
Problem
All pieces of the ABBYY magnetic mosaic are rectangular. Two pieces can be joined only if at least one of their dimensions matches: length, width, or both. Magnetic pieces cannot be rotated or flipped. A pair of mosaic pieces that cannot be joined is called inharmonious. For example, the pair and is inharmonious, while the pairs and , and and , are harmonious.
The designers at ABBYY laid out all mosaic pieces in a row without joining them. A set is several consecutive pieces in this row. They chose several sets of pieces that they want to keep for an installation. For each such set they need to determine whether it contains an inharmonious pair of pieces.
Write a program that, for various sets of consecutive mosaic pieces, determines the indices of the pieces forming an inharmonious pair, or reports that no such pair exists.
Input
The first line of the input file contains a single number , the number of pieces in the mosaic (). The next lines contain two integers and each, the length and width of the -th mosaic piece (, ).
The -th line contains a single integer , the number of sets in which the indices of two inharmonious pieces must be determined (). The next lines contain pairs of integers and , the indices of the first and last pieces of a set in which two inharmonious mosaic pieces must be found ().
Output
The output file must contain lines, each with two space-separated numbers: the indices of the mosaic pieces forming an inharmonious pair in the corresponding set. If there are several answers, output any of them. If a set has no inharmonious pair, output 0 0 on the corresponding line.