#P15298. [ROI 2012 Day 2] mosaic 马赛克

    ID: 17377 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>贪心2012线段树二分Special Judge构造ROI(俄罗斯)

[ROI 2012 Day 2] mosaic 马赛克

Background

Translation source: loj #5459. 「ROI 2012 Day 2」Mosaic.

Problem Description

All magnetic mosaic elements made by ABBYY are rectangles. Two elements can be connected only if they share at least one dimension (length, width, or both). Magnetic elements cannot be rotated or flipped. If two mosaic elements cannot be connected, they are called an inharmonious pair. For example, 1×21 \times 2 and 2×32 \times 3 form an inharmonious pair, while 2×32 \times 3 and 1×31 \times 3, or 2×32 \times 3 and 2×32 \times 3, are harmonious pairs.

ABBYY’s designers placed all mosaic elements in a single line, but did not connect them. We call a consecutive segment of elements in the line a set. They chose some sets to create an art installation, and need to determine whether each set contains an inharmonious pair of elements.

You need to write a program that, for each query segment of consecutive elements, finds the indices of two elements that form an inharmonious pair, or reports that no such pair exists in the segment.

Input Format

The first line of the input contains an integer NN (2≤N≤100000)(2 \leq N \leq 100000), the number of mosaic elements.

The next NN lines each contain two integers AiA_i and BiB_i (1≤Ai,Bi≤109,1≤i≤N)(1 \leq A_i, B_i \leq 10^{9}, 1 \leq i \leq N), representing the length and width of the ii-th mosaic element.

The (N+2)(N + 2)-th line contains an integer KK (1≤K≤100000)(1 \leq K \leq 100000), the number of sets to be checked for inharmonious pairs.

The next KK lines each contain two integers N1N_1 and N2N_2 (1≤N1<N2≤N)(1 \leq N_1 < N_2 \leq N), the indices of the first and last elements of a set, within which you need to find an inharmonious pair.

Output Format

The output should contain KK lines. Each line contains two integers separated by a space, the indices of two mosaic elements that form an inharmonious pair in the corresponding set. If multiple answers exist, you may output any one. If there is no inharmonious pair in the set, output 0 0.

4
2 2
1 2
1 3
2 3
2
2 3
2 4
0 0
4 2

Hint

Detailed additional constraints and scores for subtasks are shown in the table below.

Subtask Score Additional Constraints
11 2020 Number of mosaic elements N≤100N \leq 100, number of sets K≤100K \leq 100.
22 3030 Number of mosaic elements N≤1000N \leq 1000, number of sets K≤1000K \leq 1000.
33 2020 Number of mosaic elements N≤5000N \leq 5000, number of sets K≤5000K \leq 5000.
44 3030 Number of mosaic elements N≤100000N \leq 100000, number of sets K≤100000K \leq 100000.

Translated by ChatGPT 5