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

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

1 lộ trình

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 [l,r][l,r], sum the values of every nonempty contiguous subarray contained in that range: ∑l≤x≤y≤r∑i=xyai\sum_{l\le x\le y\le r}\sum_{i=x}^{y}a_i.

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∣≤100|a_i|\le100, 1≤l≤r≤n1\le l\le r\le n. Additionally: n≤30,q≤40n\le30, q\le40.
  • Subtask 2 (30 points): 1≤n,q≤2000001\le n,q\le200000, ∣ai∣≤100|a_i|\le100, 1≤l≤r≤n1\le l\le r\le n. Additionally: all queries satisfy l=rl=r.
  • Subtask 3 (50 points): 1≤n,q≤2000001\le n,q\le200000, ∣ai∣≤100|a_i|\le100, 1≤l≤r≤n1\le l\le r\le n.

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.