Replace
Contest: Codeforces Round 808 (Div. 1)
Difficulty rating: 3500
Tags: binary search, data structures

You are given an integer array $$$a_1,\ldots, a_n$$$, where $$$1\le a_i \le n$$$ for all $$$i$$$.

There's a "replace" function $$$f$$$ which takes a pair of integers $$$(l, r)$$$, where $$$l \le r$$$, as input and outputs the pair $$$$$$f\big( (l, r) \big)=\left(\min\{a_l,a_{l+1},\ldots,a_r\},\, \max\{a_l,a_{l+1},\ldots,a_r\}\right).$$$$$$

Consider repeated calls of this function. That is, from a starting pair $$$(l, r)$$$ we get $$$f\big((l, r)\big)$$$, then $$$f\big(f\big((l, r)\big)\big)$$$, then $$$f\big(f\big(f\big((l, r)\big)\big)\big)$$$, and so on.

Now you need to answer $$$q$$$ queries. For the $$$i$$$-th query you have two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\le l_i\le r_i\le n$$$). You must answer the minimum number of times you must apply the "replace" function to the pair $$$(l_i,r_i)$$$ to get $$$(1, n)$$$, or report that it is impossible.

Input format:
The first line contains two positive integers $$$n$$$, $$$q$$$ ($$$1\le n,q\le 10^5$$$) — the length of the sequence $$$a$$$ and the number of the queries.

The second line contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1\le a_i\le n$$$) — the sequence $$$a$$$.

Each line of the following $$$q$$$ lines contains two integers $$$l_i$$$, $$$r_i$$$ ($$$1\le l_i\le r_i\le n$$$) — the queries.

Output format:
For each query, output the required number of times, or $$$-1$$$ if it is impossible.

Constraints:
Time limit: 1.5 s
Memory limit: 1024.0 MB

Note:
In the first example, $$$n=5$$$ and $$$a=[2,5,4,1,3]$$$.

For the first query: $$$(4,4)\to(1,1)\to(2,2)\to(5,5)\to(3,3)\to(4,4)\to\ldots$$$, so it's impossible to get $$$(1,5)$$$.

For the second query, you already have $$$(1,5)$$$.

For the third query: $$$(1,4)\to(1,5)$$$.

For the fourth query: $$$(3,5)\to(1,4)\to(1,5)$$$.

For the fifth query: $$$(4,5)\to(1,3)\to(2,5)\to(1,5)$$$.

For the sixth query: $$$(2,3)\to(4,5)\to(1,3)\to(2,5)\to(1,5)$$$.

Example input:
5 6
2 5 4 1 3
4 4
1 5
1 4
3 5
4 5
2 3

Example output:
-1
0
1
2
3
4

Example input:
6 3
2 3 4 6 1 2
5 6
2 5
2 3

Example output:
5
1
3

Editorial:
Let $$$f([l,r])$$$ be the "replace" function. That means $$$f([l,r])=[\min\{a_i|l\le i\le r\},\max\{a_i|l\le i\le r\}]$$$. $$$f^k([l,r])$$$ means applying the function $$$k$$$ times.
When $$$[a_1,b_1]\bigcap[a_2,b_2]\ne \emptyset$$$, Let $$$j=\max\{a_1,a_2\}$$$, then $$$j\in[a_1,b_1]$$$ and $$$j\in[a_{2},b_{2}]$$$. So $$$a_j\in f([a_1,b_1])$$$ and $$$a_j\in f([a_{2},b_{2}])$$$. That means $$$f([a_1,b_1])\bigcap f([a_{2},b_{2}])\ne \emptyset$$$, so $$$f([\min\{a_1,a_{2}\},\max\{b_1,b_{2}\}])=f([a_1,b_1])\bigcup f([a_{2},b_{2}])$$$. When $$$m>2$$$ it can be similarly proved that if $$$\forall 1\le i<m,[a_i,b_i]\bigcap [a_{i+1},b_{i+1}]\ne \emptyset$$$, then $$$f(\bigcup\limits_{1\le i\le m}[a_i,b_i])=\bigcup\limits_{1\le i\le m}f([a_i,b_i])$$$.
And we also know that if $$$[a_1,b_1]\bigcap [a_2,b_2]\ne \emptyset$$$, then $$$f([a_1,b_1])\bigcap f([a_2,b_2])\ne \emptyset$$$, then $$$f^k([a_1,b_1])\bigcap f^k([a_2,b_2])\ne \emptyset$$$. So we know $$$f^k(\bigcup\limits_{1\le i\le m}[a_i,b_i])=\bigcup\limits_{1\le i\le m}f^k([a_i,b_i])$$$.
So $$$f^k([l,r])=\bigcup\limits_{l\le i<r}f^k([i,i+1])$$$. We can use $$$f^{p+q}([l,r])=f^p(f^q([l,r]))$$$ to easily get $$$f^{2^k}([i,i+1])$$$ in $$$O(n\log^2n)$$$ at first, then we can calculate $$$f^{2^k}([l,r])$$$ in $$$O(\log n)$$$. Use binary search we can get answer in $$$O(\log^2n)$$$. So the time complexity is $$$O((n+q)\log^2n)$$$.
We can use BIT instead of segment tree to make our program run faster.