Goddess of Olympos

시간 제한3초메모리 제한2048 MB

요약
길이가 n인 기온 배열과 q개의 (x, y) 쌍이 주어질 때, 최솟값이 x이고 최댓값이 y인 부분 배열의 개수를 각 쌍마다 구한다.
난이도

어려움10점 중 9점

유형
분할 정복, 세그먼트 트리, 배열, 조합론
정답자
아직 제출이 없습니다

문제

Mr. Tzanca Hurricane wants to go visit his goddess girlfriend who lives on Mount Olympus. When planning his trip, he knows the temperature on the mountain for nn consecutive days. The temperature values can be viewed as an array tt of nn integers where t_it\_i represents the temperature on the ii-th day.

Since the gasoline prices went up, he is very cautious with the gas consumption of his red Ferrari. In particular, he doesn't want to waste gas on cooling or heating. Mr. Hurricane doesn't want the temperature to be too low or too high. He has qq temperature ranges that he finds comfortable.

For each temperature range (x,y)(x, y) Mr. Hurricane gives you, he is curious how many different pairs (ℓ,r)(\ell, r) there are such that min⁡(t_ℓ,t_ℓ+1,…,t_r)=x\min(t\_{\ell}, t\_{\ell + 1}, \ldots, t\_{r}) = x and max⁡(t_ℓ,t_ℓ+1,…,t_r)=y\max(t\_{\ell}, t\_{\ell + 1}, \ldots, t\_{r}) = y. Please help him figure that out.

입력

The first line of input contains two integers nn and qq (1≤n,q≤1051 \leq n, q \leq 10^{5}).

The second line contains nn integers t_1,t_2,…,t_nt\_1, t\_2, \ldots, t\_n (1≤t_i≤n1 \leq t\_i \leq n).

The next qq lines describe the queries. The ii-th of them contains the integers x_ix\_i and y_iy\_i (1≤x_i≤y_i≤n1 \leq x\_i \leq y\_i \leq n).

출력

The output will consist of qq lines, the ii-th line containing a single integer, the answer to the ii-th query.

예제1

  1. 예제 1

    입력
    6 3
    3 4 2 2 1 5
    1 5
    2 3
    2 4
    
    예상 출력
    5
    0
    4