#P3246. [HNOI2016] 序列

    ID: 2295 远端评测题 2000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2016各省省选湖南前缀和st表,稀疏表

[HNOI2016] 序列

Description

Given a sequence of length nn: a1,a2,,ana_1, a_2, \cdots, a_n, denoted by a[1 ⁣:n]a[1 \colon n]. Similarly, a[l ⁣:r]a[l \colon r] (1lrn1 \leq l \leq r \leq n) denotes the sequence al,al+1,,ar1,ara_l, a_{l+1}, \cdots, a_{r-1}, a_r. If 1lstrn1 \leq l \leq s \leq t \leq r \leq n, then a[s ⁣:t]a[s \colon t] is called a subarray of a[l ⁣:r]a[l \colon r].

Now there are qq queries. Each query gives two numbers ll and rr, where 1lrn1 \leq l \leq r \leq n. For each query, find the sum of the minimum values over all subarrays of a[l ⁣:r]a[l \colon r]. For example, given the sequence 5,2,4,1,35, 2, 4, 1, 3 and the query l=1l = 1, r=3r = 3, the subarrays of a[1 ⁣:3]a[1 \colon 3] are $a[1 \colon 1], a[2 \colon 2], a[3 \colon 3], a[1 \colon 2], a[2 \colon 3], a[1 \colon 3]$. The sum of their minimum values is 5+2+4+2+2+2=175 + 2 + 4 + 2 + 2 + 2 = 17.

Input Format

The first line contains two integers nn and qq, the sequence length and the number of queries.

The second line contains nn integers separated by spaces. The ii-th integer is aia_i, the value of the ii-th element of the sequence.

Each of the next qq lines contains two integers ll and rr, describing a query.

Output Format

For each query, output one line containing the answer.

5 5
5 2 4 1 3
1 5
1 3
2 4
3 5
2 5
28 
17 
11 
11 
17

Hint

Constraints: For 100%100\% of the testdata, 1n,q1000001 \leq n, q \leq 100000, ai109|a_i| \leq 10^9.

Translated by ChatGPT 5