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

#PS0000278. Tổng trọng số đa thức (Polynomial Moment Prefix)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng trọng số đa thức (Polynomial Moment Prefix)

Polynomial Moment Prefix

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given an integer array a1,…,ana_1,\ldots,a_n (possibly negative), compute ∑i=lr(i2+2i+3)ai\sum_{i=l}^{r}(i^2+2i+3)a_i for each query (l,r)(l,r). The weight depends on the absolute index i, not the start of the range.

Input

First line: n,qn,q. Second line: nn integers aia_i. Next qq lines: 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∣≤1000|a_i|\le1000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le200000; 1≤q≤2000001\le q\le200000; ∣ai∣≤1000|a_i|\le1000; 1≤l≤r≤n1\le l\le r\le n; l=rl=r for every query.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le200000; 1≤q≤2000001\le q\le200000; ∣ai∣≤1000|a_i|\le1000; 1≤l≤r≤n1\le l\le r\le n.

Example 1

Input

4 3
2 -3 4 1
1 4
2 4
3 3

Output

78
66
72

Explanation

The weights are 6,11,18,27. The answers are 12-33+72+27=78; -33+72+27=66; 18·4=72.

Example 2

Input

1 1
-7
1 1

Output

-42

Explanation

There is one element; multiply -7 by weight 6.