CF1707E Replace

题目描述

给定一个长为 $n$ 的序列 $a_1,\ldots,a_n$,其中对于任意的 $i$ 满足 $1 \leq a_i \leq n$。 定义一个二元组函数如下: $$f((l,r))=(\min\{a_l,\ldots,a_r\},\max\{a_l,\ldots,a_r\})(l \leq r)$$ 你需要回答 $q$ 次询问,每次给定 $(l_i,r_i)$,问其最少经过多少次 $f$ 的调用(即 $(l,r) \rightarrow f((l,r))$)使得 $(l_i,r_i)$ 变成 $(1,n)$,若无解请输出 `-1`。

输入格式

输出格式

说明/提示

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) $ .