For each query range in a permutation, find the smallest subarray containing it whose values form a set of consecutive integers.
Hard9Segment treeStackPrefix sumNo attempts yetTime limit3sMemory limit512 MBYou are given a permutation π of the integers 1 through n. For indices a and b with 1≤a≤b≤n, the consecutive subsequence πab=(πa,πa+1,…,πb) is an interval if sorting it yields a run of consecutive integers. For example, in the permutation π=(3,1,7,5,6,4,2) the subsequence π36 is an interval because it holds the numbers 4 through 7, while π13 is not.
The intrinsic interval of a subsequence πxy is the shortest interval πab that contains it, so a≤x≤y≤b. The length of an interval is the number of elements in it. Such a shortest interval is always unique. The intersection of two overlapping intervals is again an interval, so the intersection of all intervals containing πxy is itself an interval, and that intersection is the intrinsic interval.
You are given π and m of its subsequences. Find the intrinsic interval of each subsequence.
The first line contains the size n (1≤n≤100000) of the permutation π. The second line contains n different integers π1,π2,…,πn (1≤πj≤n), the permutation itself.
The third line contains the number of subsequences m (1≤m≤100000). The j-th of the following m lines contains the endpoints xj and yj (1≤xj≤yj≤n) of the j-th subsequence.
Print m lines. The j-th line must contain the endpoints aj and bj (1≤aj≤bj≤n) of the intrinsic interval of the subsequence πxjyj, separated by a space.