Give array A consist of N (1 <= N <= 10^5) positive integer less than 10^6. Given Q (1 <= Q <= 10^5) queries, for each query of the form (L, R) (1 <= L <= R <= N), print out:
min(A[L], max(A[L+1], min(A[L+2], ...A[R]))))
Note that we DON'T take the min value of (A[L+2], A[L+3], ..., A[R-1], a[R]). 'Min' and 'max' are interleaved.
- For example: A[10] = {3, 1, 4, 5, 5, 2, 7, 8, 1, 1} and 1 query (1, 8):
min(A[1], max(A[2], min(A[3], max(A[4], min(A[5], max(A[6], min(A[7], A[8])))))))
= min(3, max(1, min(4, max(5, min(5, max(2, min(7, 8)))))))
= min(3, max(1, min(4, max(5, min(5, max(2, 7))))))
= min(3, max(1, min(4, max(5, min(5, 7)))))
= min(3, max(1, min(4, max(5, 5))))
=min(3, max(1, min(4, 5)))
=min(3, max(1, 4))
=min(3, 4)
= 3
- My solution is for each query, consider all A[i] (L <= i <= R) to get the result. But it is not possible because N <= 10^5. Is there any other solution to this problem? Thanks everyone.