#BS0000491. Tổng một đoạn trong dãy tổng đoạn con đã sắp xếp (Range Sum of Sorted Subarray Sums)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổng một đoạn trong dãy tổng đoạn con đã sắp xếp (Range Sum of Sorted Subarray Sums)
Range Sum of Sorted Subarray Sums
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
An array contains positive integers . A nonempty subarray is determined by two positions satisfying , and contains every entry from position through position . Its sum is .
Form a sequence containing the sums of all nonempty subarrays and arrange it in nondecreasing order. When distinct subarrays have equal sums, each still contributes its own entry to .
Given two positions in , numbered from , compute modulo .
Input
- The first line contains three integers : the original array length and the first and last requested positions in the sorted sequence.
- The second line contains exactly integers in their original order.
Output
Print one integer: the nonnegative remainder of the requested sum upon division by .
Subtasks
- Subtask 1 (20 points): ; for every ; .
- Subtask 2 (30 points): ; for every ; .
- Subtask 3 (50 points): ; for every ; .
Examples
Input 1
4 1 5
1 2 3 4
Output 1
13
Explanation 1
For , subarrays starting at position have sums ; at position : ; at position : ; at position : . The sorted sequence is . The first five entries add to , below the modulus.
Input 2
4 3 4
1 2 3 4
Output 2
6
Explanation 2
The sorted sums are again . Positions both contain : the subarray and the singleton give equal sums. Including both occurrences gives .
Input 3
4 1 10
1 2 3 4
Output 3
50
Explanation 3
All ten entries are requested. The first five sum to ; the last five sum to . The result is .