#PS0000313. Truy vấn dãy không giảm (Increasing Array Queries)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 integers . Each query specifies an inclusive subarray .
One operation increases exactly one entry in that subarray by . 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 , the array length and the query count.
- The second line contains integers .
- Each of the following lines contains integers , 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): , (), . , .
- Subtask 2 (30 points): , (), . The initial array is nondecreasing.
- Subtask 3 (50 points): , (), .
Example 1
Input
5 3
2 10 4 2 5
3 5
2 2
1 4
Output
2
0
14
Explanation
For , the copy is . Raising to costs two operations and yields . The singleton needs zero operations. For , the copy is ; the last two entries must be at least , which costs operations. The answers are .
Example 2
Input
4 3
5 1 4 2
1 4
2 4
3 4
Output
8
2
2
Explanation
For , raise the final three entries of to , costing . For , raise the last entry of to , costing . For , raising to also costs . These counts are minimal because earlier entries cannot be decreased.