#P4747. [CERC2017] Intrinsic Interval

[CERC2017] Intrinsic Interval

题目描述

Given a permutation π\pi of integers 11 through nn, an interval in π\pi is a consecutive subsequence consisting of consecutive numbers. More precisely, for indices aa and bb where 1abn1 \le a \le b \le n, the subsequence πab=(πa,πa+1,...,πb)\pi^b_a = (\pi_a, \pi_{a+1}, . . . ,\pi_b) is an interval if sorting it would yield a sequence of consecutive integers.

For example, in permutation π=(3,1,7,5,6,4,2)\pi = (3, 1, 7, 5, 6, 4, 2), the subsequence π36\pi^6_3 is an interval (it contains the numbers 44 through 77) while π13\pi^3_1 is not.

For a subsequence πxy\pi^y_x its intrinsic interval is any interval πab\pi^b_a that contains the given subsequence (axyb)(a \le x \le y \le b) and that is, additionally, as short as possible. Of course, the length of an interval is defined as the number of elements it contains.

Given a permutation π\pi and mm of its subsequences, find some intrinsic interval for each subsequence.

输入格式

The first line contains an integer n(1n100000)n(1 \le n \le 100 000) — the size of the permutation π\pi. The following line contains nn different integers π1,π2,...,πn(1πjn)\pi_1, \pi_2, . . . , \pi_n (1 \le \pi_j \le n) — the permutation itself.

The following line contains an integer m(1m100000)m(1 \le m \le 100 000) — the number of subsequences. The jthj-th of the following mm lines contains integers xjx_j and yj(1xjyjn)y_j(1 \le x_j \le y_j \le n) — the endpoints of the jthj-th subsequence.

输出格式

Output mm lines. The jthj-th line should contain two integers aja_j and bjb_j where 1ajbjn1 \le a_j \le b_j \le n — the endpoints of some intrinsic interval of the jthj-th subsequence πxjyj\pi^{y_j}_{x_j}.

题目大意

题目描述

对于正整数 1,2,3n1,2,3 \cdots n 的一个排列 π\pi,若它的一个子串 π[a..b]\pi[a..b] 排序后是连续正整数,则称 π[a..b]\pi[a..b] 是一个“区间”。例如对排列 pi=3,1,7,5,6,4,2pi={3,1,7,5,6,4,2},子串 π[3..6]\pi[3..6] 是一个“区间”(因为它包含 4,5,6,74,5,6,7),π[1..3]\pi[1..3] 则不是。

一个子串的“本征区间”是包含该子串的最短区间。“包含”是指:若 π[x..y]\pi[x..y] 的本征区间是 π[a..b]\pi[a..b],则 axyba \le x \le y \le b

给定一个排列 π\pi 及其 mm 个子串,求每个子串的“本征区间”。

输入格式

第一行一个整数 n(1n100000)n(1 \le n \le 100000)

第二行 nn 个整数,代表排列 π\pi

第三行一个整数 m(1m100000)m(1 \le m \le 100000)

此后 mm 行,每行两个整数 x,y(1xyn)x,y(1 \le x \le y \le n),代表子串 π[x..y]\pi[x..y]

输出格式

输出 mm 行,每行两个整数 a,b(1abn)a,b(1 \le a \le b \le n),代表子串对应的本征区间 π[a..b]\pi[a..b]

7
3 1 7 5 6 4 2
3
3 6
7 7
1 3

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

1 4
3 7
3 7
3 10
7 10