HORRIBLE - Horrible Queries


World is getting more evil and it's getting tougher to get into the Evil League of Evil. Since the legendary Bad Horse has retired, now you have to correctly answer the evil questions of Dr. Horrible, who has a PhD in horribleness (but not in Computer Science). You are given an array of N elements, which are initially all 0. After that you will be given C commands. They are -

* 0 p q v - you have to add v to all numbers in the range of p to q (inclusive), where p and q are two indexes of the array.

* 1 p q - output a line containing a single integer which is the sum of all the array elements between p and q (inclusive)

 

Input

In the first line you'll be given T, number of test cases.

Each test case will start with N (N <= 100 000) and C (C <= 100 000). After that you'll be given C commands in the format as mentioned above. 1 <= p, q <= N and 1 <= v <= 10^7.

Output

Print the answers of the queries.

Example

Input:
1
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

hide comments
aayush_b1999: 2020-03-27 21:43:01

can be done using sqrt decomposition.
change size to 420

shammya: 2020-02-06 19:37:03

1.Naive segment tree with lazy propagation
2. Be careful the query sum can exceed int so use long long

mrmajumder: 2020-02-05 09:07:33

Took me two days to solve it.
First tried to learn iterative approach for segtree, wrote the full code, but idk where there was some bug for indexing or something.
Then learned the recursive approach, finally solved the problem! Totally worth it!! :)

sangmai: 2020-01-22 04:58:16

not using lazy propagation cost me two tle, one run time error for two small array size, and 8 WA for not using long long int and some silly mistakes. Now finally AC.

gustavo_farani: 2020-01-13 22:32:44

Segmentation fault with less than 4*n as size for Segment Tree's array

Last edit: 2020-01-13 22:41:40
transonviet: 2020-01-03 16:42:41

Solve it using c++ 4.3.2 i'm using c++ 8.3 and it gave WA =((((

lscat: 2020-01-02 12:05:32

sometimes, p and q don't satisfy that condition: `1 <= p, q <= N`.

Last edit: 2020-01-02 12:10:15
luvkumbi: 2019-12-29 12:53:29

Just undestand this:-
https://www.geeksforgeeks.org/lazy-propagation-in-segment-tree/

If not clear even then:-
https://www.youtube.com/watch?v=xuoQdt5pHj0&t=196s

my code:-
<snip>

Last edit: 2022-10-17 21:54:48
ahmet25: 2019-12-18 15:55:27

when you have WA try to increase the array size, 1.000.000 works.
I don't know but 100.000 for array size and 400.005 for segment array size didn't work.

azizulalif: 2019-11-01 11:51:19

Clear your array properly....


Added by:Iqram Mahmud
Date:2010-12-04
Time limit:2.329s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own. Thanks to Emir Habul.