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

#FT0000017. Cộng đoạn và tổng đoạn (Range Add Range Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cộng đoạn và tổng đoạn (Range Add Range Sum)

Range Add Range Sum

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence a1,a2,…,ana_1,a_2,\ldots,a_n of positive integers, indexed from 11. Support two operations: increase every element in an interval, and obtain the current sum of an interval.

Command 1 l r k increases each element from position ll through rr by kk. As both endpoints are included, exactly r−l+1r-l+1 elements change and the entire array sum increases by k(r−l+1)k(r-l+1).

Command 2 l r prints the current sum al+al+1+⋯+ara_l+a_{l+1}+\cdots+a_r. Previous additions remain in effect; queries do not modify the array. Results may exceed the signed 32-bit integer range.

Input

  • Line 1: n m gives array length and operation count.
  • Line 2: a_1 a_2 ... a_n gives the initial values in index order.
  • Next mm lines: each line contains one of the following commands, in execution order:
    • 1 l r k: add kk to all elements in [l,r][l,r].
    • 2 l r: query sum over inclusive interval [l,r][l,r].

Output

For each 2 l r command, print the requested integer computed from the current state on its own line, in query order. Update commands produce no output.

Subtasks

Subtask 1 (15 points):

  • 1≤n≤81\le n\le 8.
  • 1≤m≤101\le m\le 10.
  • aia_i and kk are positive integers.
  • 1≤l≤r≤n1\le l\le r\le n in every command.
  • ∑i=1nai≤2⋅1018\sum_{i=1}^n a_i\le 2\cdot 10^{18} at all times.

Subtask 2 (20 points):

  • 1≤n≤10001\le n\le 1000.
  • 1≤m≤100001\le m\le 10000.
  • aia_i and kk are positive integers.
  • 1≤l≤r≤n1\le l\le r\le n in every command.
  • ∑i=1nai≤2⋅1018\sum_{i=1}^n a_i\le 2\cdot 10^{18} at all times.

Subtask 3 (65 points):

  • 1≤n≤1000001\le n\le 100000.
  • 1≤m≤1000001\le m\le 100000.
  • aia_i and kk are positive integers.
  • 1≤l≤r≤n1\le l\le r\le n in every command.
  • ∑i=1nai≤2⋅1018\sum_{i=1}^n a_i\le 2\cdot 10^{18} at all times.

Examples

Input

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

Output

10
11
11

Explanation

There are n=4n=4 elements. The initial values are [1, 2, 3, 4].

  1. Command 1 — 2 1 4: The sum on [1,4][1,4] is 1+2+3+4=101 + 2 + 3 + 4=10, so print 10.

  2. Command 2 — 1 2 4 3: Add 33 to each element of [2,4][2,4]; the current array is [1, 5, 6, 7].

  3. Command 3 — 2 2 3: The sum on [2,3][2,3] is 5+6=115 + 6=11, so print 11.

  4. Command 4 — 1 1 1 5: Add 55 to each element of [1,1][1,1]; the current array is [6, 5, 6, 7].

  5. Command 5 — 2 1 2: The sum on [1,2][1,2] is 6+5=116 + 5=11, so print 11.

Collect the answers to output-producing commands in their input order, printing one answer per line.