PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000291. Tổng các tổng đoạn con (Sum of All Subarray Sums in a Range)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tổng các tổng đoạn con (Sum of All Subarray Sums in a Range)
Sum of All Subarray Sums in a Range
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
For each inclusive query , sum the values of every nonempty contiguous subarray contained in that range: .
Input
First line: . Second line: array elements. Each of the next lines contains .
Output
Print each answer on its own line.
Subtasks
- Subtask 1 (20 points): , , . Additionally: .
- Subtask 2 (30 points): , , . Additionally: all queries satisfy .
- Subtask 3 (50 points): , , .
Example 1
Input
3 2
1 2 3
1 3
2 3
Output
20
10
Explanation
The six sums are 1, 2, 3, 3, 5, 6, totaling 20. The second range gives 2+3+5=10.
Example 2
Input
2 2
-2 3
1 2
1 1
Output
2
-2
Explanation
The sums are -2, 3, 1, totaling 2. The singleton gives -2.