PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000280. Tổng đoạn trên mảng vòng (Circular Range Sum)
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 đoạn trên mảng vòng (Circular Range Sum)
Circular Range Sum
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
An integer array is arranged in a circle in index order. Index 1 follows index . For each query , sum exactly consecutive elements starting at , including , moving in increasing index order and wrapping as needed. Each query satisfies .
Input
First line: . Second line: integers . Each of the next lines contains two integers .
Output
Print one answer per line, in query order.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; ; for every query.
- Subtask 3 (50 points): ; ; ; .
Example 1
Input
4 3
2 -3 4 1
3 3
4 2
2 4
Output
7
3
4
Explanation
The selected sequences are (4,1,2), (1,2), (-3,4,1,2); their sums are 7, 3, 4.
Example 2
Input
1 1
-7
1 1
Output
-7
Explanation
The circle has one element.