PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000313. Truy vấn dãy không giảm (Increasing Array Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn dãy không giảm (Increasing Array Queries)

Increasing Array Queries

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

An array contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n. Each query specifies an inclusive subarray [l,r][l,r].

One operation increases exactly one entry in that subarray by 11. The resulting subarray is nondecreasing if every entry other than the first is at least as large as its predecessor.

For each query, find the minimum number of operations needed. Queries are independent: operations apply to a fresh copy and do not alter the original array.

Input

  • The first line contains integers n,qn,q, the array length and the query count.
  • The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.
  • Each of the following qq lines contains integers l,rl,r, the endpoints of the subarray.

Output

For each query, print its minimum operation count on a separate line, in input order.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤2000001\le n,q\le200000, 1≤ai≤1091\le a_i\le10^9 (1≤i≤n1\le i\le n), 1≤l≤r≤n1\le l\le r\le n. n≤30n\le30, q≤40q\le40.
  • Subtask 2 (30 points): 1≤n,q≤2000001\le n,q\le200000, 1≤ai≤1091\le a_i\le10^9 (1≤i≤n1\le i\le n), 1≤l≤r≤n1\le l\le r\le n. The initial array is nondecreasing.
  • Subtask 3 (50 points): 1≤n,q≤2000001\le n,q\le200000, 1≤ai≤1091\le a_i\le10^9 (1≤i≤n1\le i\le n), 1≤l≤r≤n1\le l\le r\le n.

Example 1

Input

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

Output

2
0
14

Explanation

For [3,5][3,5], the copy is (4,2,5)(4,2,5). Raising 22 to 44 costs two operations and yields (4,4,5)(4,4,5). The singleton [2,2][2,2] needs zero operations. For [1,4][1,4], the copy is (2,10,4,2)(2,10,4,2); the last two entries must be at least 1010, which costs 6+8=146+8=14 operations. The answers are 2,0,142,0,14.

Example 2

Input

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

Output

8
2
2

Explanation

For [1,4][1,4], raise the final three entries of (5,1,4,2)(5,1,4,2) to 55, costing 4+1+3=84+1+3=8. For [2,4][2,4], raise the last entry of (1,4,2)(1,4,2) to 44, costing 22. For [3,4][3,4], raising 22 to 44 also costs 22. These counts are minimal because earlier entries cannot be decreased.