Logo
(追記) (追記ここまで)

33744번 - Depth of Cartesian Tree 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
8 초 2048 MB21111050.000%

문제

The cartesian tree of a sequence of distinct integers, is an unique binary tree defined as follows.

  • The cartesian tree of a sequence with one element is the tree of one node;
  • The root of the cartesian tree of a sequence corresponds to the maximum value of the sequence;
  • If the root does not correspond to the leftmost index, the left subtree is the cartesian tree of the subarray left to it;
  • If the root does not correspond to the rightmost index, the right subtree is the cartesian tree of the subarray right to it.

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.

  • $l$ $r$: Assume that you construct a cartesian tree of the subsequence $p_l,p_{l+1},\cdots,p_r$. Answer the sum of the depths of all nodes in the tree.

입력

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.

제한

예제 입력 1

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

예제 출력 1

10
2
3
2
3
2
5
4
4
5

힌트

출처

Camp > Osijek Competitive Programming Camp > Summer 2024 > Day 5: OCPC Potluck Contest 2 D번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /