PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000281. Tổng đoạn trên dãy tuần hoàn vô hạn (Infinite Periodic Range Sum)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng đoạn trên dãy tuần hoàn vô hạn (Infinite Periodic Range Sum)

Infinite Periodic Range Sum

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given an integer array a1,…,ana_1,\ldots,a_n, form an infinite sequence by repeating it: bi=a((i−1) mod n)+1b_i=a_{((i-1)\bmod n)+1} for all i≥1i\ge1. For each query (L,R)(L,R), compute ∑i=LRbi\sum_{i=L}^R b_i, including both endpoints. Output the exact integer without taking a modulus.

Input

First line: n,qn,q. Second line: nn integers aia_i. Each of the next qq lines contains two integers L,RL,R.

Output

Print one answer per line, in query order.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; 1≤q≤401\le q\le40; ∣ai∣≤9|a_i|\le9; 1≤L≤R≤1001\le L\le R\le 100.
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le200000; 1≤q≤2000001\le q\le200000; ∣ai∣≤9|a_i|\le9; 1≤L≤R≤10181\le L\le R\le 10^{18}; (R−L+1) mod n=0(R-L+1)\bmod n=0 for every query.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le200000; 1≤q≤2000001\le q\le200000; ∣ai∣≤9|a_i|\le9; 1≤L≤R≤10181\le L\le R\le 10^{18}.

Example 1

Input

3 3
2 -3 4
1 6
2 5
5 5

Output

6
0
-3

Explanation

The first six elements form two periods with sum 6. Positions 2..5 are (-3,4,2,-3), sum 0. Position 5 is -3.

Example 2

Input

1 2
-2
1 10
8 10

Output

-20
-6

Explanation

Every element is -2; the ranges have 10 and 3 elements respectively.