| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 8 초 | 2048 MB | 21 | 11 | 10 | 50.000% |
The cartesian tree of a sequence of distinct integers, is an unique binary tree defined as follows.
In a binary tree, the depth of a node is defined as the distance from the root to the node.
You are given a permutation $p$ of elements 1,2,ドル\cdots,n$. You must solve $q$ queries of the following kind.
The first line contains two integers $n$ and $q$ (1ドル \leq n, q \leq 10^6$) --- the length of the permutation and number of queries respectively.
The second line contains $n$ distinct integers $p_1, p_2, \cdots, p_n$ (1ドル \leq p_i \leq n$) --- the permutation $p$.
The next $q$ lines contain two integers $l_i, r_i$ (1ドル \leq l_i \leq r_i \leq n$) --- the bounds of query $i$.
For each query, output the answer on a new line.
7 10 1 5 4 7 2 6 3 1 7 1 3 2 4 3 5 4 6 5 7 1 4 2 5 3 6 4 7
10 2 3 2 3 2 5 4 4 5