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

#PS0000292. Tổng đoạn cực dài trên dãy tuần hoàn (Very Long 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 cực dài trên dãy tuần hoàn (Very Long Periodic Range Sum)

Very Long Periodic Range Sum

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

The infinite sequence is defined by bi=ai mod nb_i=a_{i\bmod n} for i≥0i\ge0. For each half-open range [L,R)[L,R), sum its elements. An empty range has sum zero.

Input

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

Output

Print each answer on its own line.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤2000001\le n,q\le200000, ∣ai∣≤9|a_i|\le9, 0≤L≤R≤10180\le L\le R\le10^{18}. Additionally: n≤30,q≤40n\le30, q\le40, R≤100R\le100.
  • Subtask 2 (30 points): 1≤n,q≤2000001\le n,q\le200000, ∣ai∣≤9|a_i|\le9, 0≤L≤R≤10180\le L\le R\le10^{18}. Additionally: all queries satisfy (R−L) mod n=0(R-L)\bmod n=0.
  • Subtask 3 (50 points): 1≤n,q≤2000001\le n,q\le200000, ∣ai∣≤9|a_i|\le9, 0≤L≤R≤10180\le L\le R\le10^{18}.

Example 1

Input

3 3
2 -3 4
0 8
2 5
4 4

Output

5
3
0

Explanation

Two complete periods give 6 and the remaining 2,-3 give 5. The second range gives 4+2-3=3; the empty range gives 0.

Example 2

Input

1 1
9
0 10

Output

90

Explanation

There are exactly 10 copies of 9.