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

23162번 - Matryoshka Dolls 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
5 초 512 MB275392016.529%

문제

Denisson is an average enjoyer of Matryoshka dolls. A set of matryoshkas consists of a wooden figure, which separates at the middle, top from bottom, to reveal a smaller figure of the same sort inside, which has, in turn, another figure inside of it, and so on.

Recently he bought a set of these toys and placed them on the table in some order. There are exactly $n$ dolls of different sizes in his set, so the current order of matryoshkas can be represented as a permutation $p$ of length $n,ドル where $p_i$ is equal to the size of $i$-th doll.

Denisson usually has a very tight schedule, but today he wants to relax with his toys, and will play the following game:

  • Firstly, he will choose some segment $[l, r]$ of his dolls permutation;
  • Then he will take the smallest matryoshka on this segment and place it into the matryoshka of the next size. The time needed for him to perform this procedure equals $|i - j|$ where $p_i$ and $p_j$ are sizes of the two smallest dolls on the chosen segment;
  • He will repeat this action until there is only one matryoshka left on the segment. The total time needed for his game is the sum of times needed for all performed procedures.

Suddenly, he realized that his interesting game could last for a very long time, but he really cares about his schedule. He came to you with $q$ different segments $[l_i, r_i]$ and wonders what time he needs to play the game on each of these segments. He hopes that you will not spend too much time finding it out.

입력

The first line contains two integers $n$ and $q$ --- the number of matryoshkas and the number of requests (1ドル \leq n \leq 10^5,ドル 1ドル \leq q \leq 5 \cdot 10^5$).

The second line describes the order of the matryoshkas represented as a permutation $p$ (1ドル \leq p_i \leq n$).

The following $q$ lines describe requests in the order they are received. Each request is described by two integers $l_i$ and $r_i$ (1ドル \le l_i \le r_i \le n$) --- endpoints of the $i$-th segment.

출력

For $i$-th request, print the total time needed to play the game on segment $[l_i, r_i]$.

제한

예제 입력 1

5 5
1 5 2 4 3
1 5
1 4
1 3
1 2
1 1

예제 출력 1

7
5
3
1
0

힌트

출처

Camp > Petrozavodsk Programming Camp > Summer 2021 > Day 7: Moscow IPT Contest D번

Contest > Open Cup > 2021/2022 Season > Stage 1: Grand Prix of Dolgoprudny D번

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

출처

대학교 대회

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

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