#SGM0000038. Tổng bình phương với cây đoạn (Sum of Squares with Segment Tree)

Tổng bình phương với cây đoạn (Sum of Squares with Segment Tree)

Sum of Squares with Segment Tree

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Maintain an array with range assignment and range addition; queries return the sum of squares on a range.

Input

The first line contains n,qn,q and the next line the array. Operations are:

  • 0 l r x: assign every value in [l,r][l,r] to xx.
  • 1 l r x: add xx to every value in [l,r][l,r].
  • 2 l r: print ∑i=lrai2\sum_{i=l}^{r}a_i^2.

Output

Print one line for each type 2 query. The PHOJ version omits the original multi-case Case k: line.

Subtasks

  • 20 points: n,q≤50n,q\le50.
  • 30 points: n,q≤5000n,q\le5000.
  • 50 points: n,q≤105n,q\le10^5, ∣ai∣,∣x∣≤1000|a_i|,|x|\le1000; use 64-bit signed integers for intermediate values.

Examples

Input

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

Output

30
7
13

Explanation

The initial square sum is 3030. Assigning the last two values to 1 gives 77. Adding 1 to those two positions produces 1 2 2 2, whose square sum is 13.