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

#CT0000058. Tổng có trọng số (Weighted Sum)

Bài tập này chưa được xếp vào lộ trình.

Tổng có trọng số (Weighted Sum)

Weighted Sum

Version: Phuoc Hung OJ Extended

Problem Statement

Given an integer sequence A1,A2,…,ANA_1,A_2,\ldots,A_N. There are QQ queries. Each query gives L,R,TL,R,T, where 1≤L≤R≤N1\le L\le R\le N and T∈{1,2}T\in\{1,2\}.

For T=1T=1, compute

AL+2AL+1+3AL+2+⋯+(R−L+1)AR.A_L+2A_{L+1}+3A_{L+2}+\cdots+(R-L+1)A_R.

For T=2T=2, compute

(R−L+1)AL+(R−L)AL+1+⋯+2AR−1+AR.(R-L+1)A_L+(R-L)A_{L+1}+\cdots+2A_{R-1}+A_R.

Answer all queries in order.

Input

The first line contains N,QN,Q.

The second line contains A1,…,ANA_1,\ldots,A_N.

Each of the next QQ lines contains L,R,TL,R,T.

Output

Print QQ lines. Line ii contains the answer to query ii.

Subtasks

  • Subtask 1 (30%): 1≤N,Q≤10001\le N,Q\le1000, ∣Ai∣≤106|A_i|\le10^6.
  • Subtask 2 (30%): every query has T=1T=1, 1≤N,Q≤2⋅1051\le N,Q\le2\cdot10^5, ∣Ai∣≤106|A_i|\le10^6.
  • Subtask 3 (40%): 1≤N,Q≤2⋅1051\le N,Q\le2\cdot10^5, ∣Ai∣≤106|A_i|\le10^6.

Every answer fits in a signed 64-bit integer.

Examples

Example 1

Input

5 3
2 1 3 4 2
2 5 1
2 5 2
1 3 1

Output

27
23
13

Explanation

The first query is 1⋅1+2⋅3+3⋅4+4⋅2=271\cdot1+2\cdot3+3\cdot4+4\cdot2=27. The second query uses reversed weights.

Example 2

Input

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

Output

10
0
-4

Explanation

Array values may be negative; weights are still determined by positions inside the queried segment.

Example 3

Input

1 2
7
1 1 1
1 1 2

Output

7
7

Explanation

For a one-element segment, both query types assign weight 11.