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

#FT0000016. Những truy vấn kinh khủng (Horrible Queries)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Những truy vấn kinh khủng (Horrible Queries)

Horrible Queries

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Initially you have an array a1,a2,…,ana_1,a_2,\ldots,a_n of nn zeros, indexed from 11 through nn. Process cc operations of two types: add to an entire range, or ask for a range sum.

Command 0 l r v adds vv to each element of the inclusive interval [l,r][l,r]. Overlapping updates accumulate. Command 1 l r asks for the current sum al+al+1+⋯+ara_l+a_{l+1}+\cdots+a_r.

Operations are processed in order. There is no input line containing initial array values, since all initial values are prescribed to be zero. The exact query result may require a wide integer type.

Input

  • Line 1: n c gives array length and number of operations.
  • Next cc lines: each line contains one of the following commands, in execution order:
    • 0 l r v: add vv to every element in [l,r][l,r].
    • 1 l r: query sum over inclusive interval [l,r][l,r].

Output

For each 1 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 (20 points):

  • 1≤n≤241\le n\le 24.
  • 1≤c≤301\le c\le 30.
  • 1≤v≤1071\le v\le 10^7.
  • 1≤l≤r≤n1\le l\le r\le n.
  • all elements are initially 00.

Subtask 2 (30 points):

  • 1≤n≤12001\le n\le 1200.
  • 1≤c≤16001\le c\le 1600.
  • 1≤v≤1071\le v\le 10^7.
  • 1≤l≤r≤n1\le l\le r\le n.
  • all elements are initially 00.

Subtask 3 (50 points):

  • 1≤n≤1000001\le n\le 100000.
  • 1≤c≤1000001\le c\le 100000.
  • 1≤v≤1071\le v\le 10^7.
  • 1≤l≤r≤n1\le l\le r\le n.
  • all elements are initially 00.

Examples

Input

8 6
0 2 4 26
0 4 8 80
0 4 5 20
1 8 8
0 5 7 14
1 4 8

Output

80
508

Explanation

There are n=8n=8 elements. The initial values are [0, 0, 0, 0, 0, 0, 0, 0].

  1. Command 1 — 0 2 4 26: Add 2626 to each element of [2,4][2,4]; the current array is [0, 26, 26, 26, 0, 0, 0, 0].

  2. Command 2 — 0 4 8 80: Add 8080 to each element of [4,8][4,8]; the current array is [0, 26, 26, 106, 80, 80, 80, 80].

  3. Command 3 — 0 4 5 20: Add 2020 to each element of [4,5][4,5]; the current array is [0, 26, 26, 126, 100, 80, 80, 80].

  4. Command 4 — 1 8 8: The sum on [8,8][8,8] is 80=8080=80, so print 80.

  5. Command 5 — 0 5 7 14: Add 1414 to each element of [5,7][5,7]; the current array is [0, 26, 26, 126, 114, 94, 94, 80].

  6. Command 6 — 1 4 8: The sum on [4,8][4,8] is 126+114+94+94+80=508126 + 114 + 94 + 94 + 80=508, so print 508.

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