SEGSQRSS - Sum of Squares with Segment Tree


Segment trees are extremely useful. In particular "Lazy Propagation" (i.e. see here, for example) allows one to compute sums over a range in O(lg(n)), and update ranges in O(lg(n)) as well. In this problem you will compute something much harder:

The sum of squares over a range with range updates of 2 types:

1) increment in a range

2) set all numbers the same in a range.

Input

There will be T (T <= 25) test cases in the input file. First line of the input contains two positive integers, N (N <= 100,000) and Q (Q <= 100,000). The next line contains N integers, each at most 1000. Each of the next Q lines starts with a number, which indicates the type of operation:

2 st nd -- return the sum of the squares of the numbers with indices in [st, nd] {i.e., from st to nd inclusive} (1 <= st <= nd <= N).

1 st nd x -- add "x" to all numbers with indices in [st, nd] (1 <= st <= nd <= N, and -1,000 <= x <= 1,000).

0 st nd x -- set all numbers with indices in [st, nd] to "x" (1 <= st <= nd <= N, and -1,000 <= x <= 1,000).

Output

For each test case output the “Case <caseno>:” in the first line and from the second line output the sum of squares for each operation of type 2. Intermediate overflow will not occur with proper use of 64-bit signed integer.

Example

Input:
2
4 5
1 2 3 4
2 1 4
0 3 4 1
2 1 4
1 3 4 1
2 1 4
1 1
1
2 1 1

Output:
Case 1:
30
7
13
Case 2:
1

hide comments
eagleshadow: 2019-05-30 16:31:53

AC in one GO!!!

msabeer: 2019-04-04 21:59:10

The problem is nice but the dataset is weak. Got AC with mistakes in code.

pranto_84: 2019-04-01 14:38:09

too weak test case, i got wrong ans with slightly incorrect code first.
Thanks magicarp for your test case :)

itssanat: 2019-03-07 15:04:55

weak test case

ab_biswas09: 2019-03-02 10:50:09

AC in One Go >> Cakewalk

abhinav_jain02: 2019-01-27 11:40:01

Very very weak test cases.
Got AC even with slightly incorrect code at first

yaseenmollik: 2018-11-15 03:34:57

I assume the test case is too weak.. After two hours of trying I have written 204 line of code using segment tree and lazy propagation.. And it got accepted without any freaking tle or wrong answer!! Who knows what happened - _-. Btw, great problem..learned a lot..

tien0903: 2018-11-13 15:35:44

1 punch and AC with freaking way :D
lazy is real

phoemur: 2018-10-23 18:07:06

Please do not mix up "sum of the squares" with "square of the sums"...

Simple Segtree gets AC without lazy propagation

exesharkx: 2018-10-08 09:26:00

AC in one goo!


Added by:Chen Xiaohong
Date:2012-07-11
Time limit:1.106s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64