Nimrod and the bricks
Difficulty: Hard

Nimrod  likes to play bricks. He has an interesting bricks construction in the back yard. The bricks are arranged into a row of N stacks in such a way that the first one is a stack of 1 brick, the second one is a stack of 2 bricks, the third one is a stack of 3 bricks, and so on.

Nimrod has unlimited amount of bricks that are not forming part of the construction. As he likes to play bricks, he sets a contiguous interval of the construction with equal number of bricks in each stack, adding or removing bricks. It takes 1 unit of time to remove one brick from one stack or add a brick to one stack. He can not move a brick from one stack to another.

But there is a problem. He may have a chance to make a mess and be scolded by his mother. Therefore, he wants to know how much time it will take to restore an interval to its original form.

Nimrod is asking for your help.

<b>Input format</b>

The first line of the input contains two integers N (1 ≤ N ≤ 100000) — the number of contiguous stacks at the bricks construction, and Q (1 ≤ Q ≤ 100000) — the number of operations. Next Q lines describe an operation. There are two types of operations. Operation '1', is when Nimrod sets a whole interval starting at the A stack and ending at the B stack, to X bricks in each stack. It is in the form '1 A B X', where (1 ≤ A ≤ B ≤ N) and (1 ≤ X ≤ 10^8). Operation '2' is when Nimnrod wants to know how much time it will take to restore an interval to its original form. It is in the form '2 A B', where (1 ≤ A ≤ B ≤ N). (quotes for clarity only).

Output format

For every '2' query output one line containing the how much time it will take to restore an interval to its original form.

Sample input
<br>5 5
<br>1 4 5 1
<br>1 5 5 2
<br>2 2 5
<br>1 5 5 1
<br>2 5 5


Sample output
<br>6
<br>4


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
<pre>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
#include <set>
#include <cassert>
#include <algorithm>
using namespace std;

typedef long long ll;

int t;
int n, x, y, q,m, op, r, k, p, c, d;

long long val[500000];
int prop[500000];

ll calc(ll a, ll b, ll x) {
	if(x < a) {
		ll nn = (b-a+1LL);
		return nn * (nn + 1LL)/2LL + (a - x - 1LL) * nn;
	} else
	if(x > b) {
		ll nn = (b-a+1LL);
		return nn * (nn + 1LL)/2LL + (x - b - 1LL) * nn;
	}

	ll n1 = x - a;
	ll n2 = b - x;

	return n1 * (n1 + 1LL) / 2LL + n2 * (n2 + 1LL) / 2LL;
}

void push(int index, int a, int b) {
	prop[2*index] = prop[index];
	prop[2*index+1] = prop[index];

	int m = (a+b)/2;

	val[2*index] = calc(a,m, prop[index]);
	val[2*index+1] = calc(m+1,b, prop[index]);

	prop[index] = 0;
}

void update(int index, int a, int b) {
	if(a > d || b < c) return;
	if(a >= c && b <= d) {
		prop[index] = x;
		val[index] = calc(a,b,x);
		return;
	}

	if(prop[index]) {
		push(index, a, b);
	}

	update(2*index, a, (a+b)/2);
	update(2*index+1, (a+b)/2+1, b);

	val[index] = val[2*index] + val[2*index+1];
}

ll querie(int index, int a, int b) {
	if(a > d || b < c) return 0;
	if(a >= c && b <= d) {
		return val[index];
	}

	if(prop[index]) {
		push(index, a, b);
	}

	return querie(2*index, a, (a+b)/2) +
	querie(2*index+1, (a+b)/2+1, b);
}


int main() {

	scanf("%d%d", &n, &q);

	while(q--) {
		scanf("%d", &op);
		if(op == 1) {
			scanf("%d%d%d", &c, &d, &x);
			assert(c >= 1 && d <= n && c <= d && x > 0 && x <= 100000000);
			update(1,1,n);
		} else {
			scanf("%d%d", &c, &d);
			assert(c >= 1 && d <= n && c <= d);
			printf("%lld\n", querie(1,1,n));
		}
	}
	return 0;
}