PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000102. Truy vấn tổng tích lũy (Cumulative Sum Query)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Truy vấn tổng tích lũy (Cumulative Sum Query)
Cumulative Sum Query
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem Statement
William Macfarlane has an array of integers indexed from zero. For each pair of indices , find the sum of all elements between those positions, including both endpoints. The two endpoints may be given in either order.
Input
The first line contains integer .
The second line contains the array elements.
The third line contains integer , the number of queries.
Each of the next lines contains two indices .
Output
Print lines, one sum per query in input order.
Subtasks
- Subtask 1 (20 points): ; ; for all ; .
- Subtask 2 (30 points): ; ; for all ; ; .
- Subtask 3 (50 points): ; ; for all ; .
Examples
Input
3
1 4 1
3
1 1
1 2
0 2
Output
4
5
6
Explanation
The indices are . The interval contains 4; contains 4 and 1, totaling 5; contains all three elements, totaling .