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

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

hide comments
2018-06-02 23:48:52
The test cases are extremely weak. Do not do this problem. Try this test case on your AC code and check what it gives by hand:
1
10 6
1 2 3 4 5 6 7 8 9 10
1 1 5 1
1 6 10 3
1 4 7 2
0 6 6 11
1 5 6 -1
2 3 8
It should output 479. Not 390.

Last edit: 2018-06-03 00:32:30
2018-06-02 16:13:27
DO NOT USE SPOJ TOOLKIT FOR COMPARISON GIVES WRONG ANSWER FOR MOST OF INPUTS
2018-03-30 18:02:29
It may be easy to find bug in your algorithm , but sometimes you need to see the assignment of space to your containers. Got 4 WA because of this.....I won't repeat it again
2018-03-01 10:46:51
Wooohooo!!! AC in 1 go! Nice problem because of the two types of queries.
2018-01-27 18:57:46
Slightly weak cases. When I checked with a brute force, my accepted code fails when any updated value is 0, because my code just ignores it in the lazy propagation when it does a check like if lazy[i]! Too lazy (heh) to fix it though, but just know, cases are weak!!
2018-01-17 14:45:22
weak test case!!!!
after getting AC with simple segment tree try to use lazy
2017-11-03 21:13:45
IT +LAZY
2017-06-20 15:56:52 shubham
AC in one go :)
2017-06-04 15:32:58
ac in one go
2017-05-21 21:35:25
AC:- SEGMENT TREE + LAZY PROPOGATION
Written code in 50 minute Too Bad -:-
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.