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

#PS0000276. Tổng có trọng số theo chỉ số (Weighted Prefix)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng có trọng số theo chỉ số (Weighted Prefix)

Weighted 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=lri⋅ai\sum_{i=l}^{r}i\cdot a_i for each query (l,r)(l,r). The weight is the absolute array index i.

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∣≤1000000|a_i|\le1000000; 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∣≤1000000|a_i|\le1000000; 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∣≤1000000|a_i|\le1000000; 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

12
10
12

Explanation

2-6+12+4=12; -6+12+4=10; 3·4=12.

Example 2

Input

1 1
-7
1 1

Output

-7

Explanation

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