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

#PS0000290. Tổng bình phương vị trí tương đối (Relative Quadratic Weighted Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng bình phương vị trí tương đối (Relative Quadratic Weighted Sum)

Relative Quadratic Weighted Sum

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

For each inclusive query [l,r][l,r], compute ∑i=lr(i−l+1)2ai\sum_{i=l}^{r}(i-l+1)^2a_i in the given array. The first element of the queried segment has weight 1.

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
2 -1 3
1 3
2 3

Output

25
11

Explanation

First: 2−4+27=252-4+27=25. Second: −1+12=11-1+12=11.

Example 2

Input

1 1
-5
1 1

Output

-5

Explanation

The single element has weight 1, so the answer is -5.