Problem:
RSQ and RUQ

Write a program which manipulates a sequence $A$ = {$a_0, a_1, ..., a_{n-1}$} with the following operations:

$update(s, t, x)$: change $a_s, a_{s+1}, ..., a_t$ to $x$.

$getSum(s, t)$: print the sum of $a_s, a_{s+1}, ..., a_t$.

Note that the initial values of $a_i ( i = 0, 1, ..., n-1 )$ are 0.

Input

$n$ $q$
$query_1$
$query_2$
:
$query_q$

In the first line, $n$ (the number of elements in $A$) and $q$ (the number of queries) are given. Then, $i$-th query $query_i$ is given in the following format:

0 $s$ $t$ $x$

or

1 $s$ $t$

The first digit represents the type of the query. '0' denotes $update(s, t, x)$
and '1' denotes $find(s, t)$.

Output

For each $getSum$ query, print the sum in a line.

Constraints

$1 ≤ n ≤ 100000$

$1 ≤ q ≤ 100000$

$0 ≤ s ≤ t < n$

$-1000 ≤ x ≤ 1000$

Sample Input 1

6 7
0 1 3 1
0 2 4 -2
1 0 5
1 0 1
0 3 5 3
1 3 4
1 0 5

Sample Output 1

-5
1
6
8

Solution in C++14 (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

template<class T, class E> struct LazySegmentTree {
    using F = function<T(T, T)>;
    using G = function<T(T, E)>;
    using H = function<E(E, E)>;
    using P = function<E(E, int)>;
private :
    int n;
    F f;
    G g;
    H h;
    P p;
    T id1;
    E id0;
    vector<T> node;
    vector<E> lazy;
public :
    LazySegmentTree (int sz, F f, G g, H h, T id1, E id0, P p)
            : f(f), g(g), h(h), id1(id1), id0(id0), p(p) {
        n = 1; while (n < sz) n *= 2;
        node.resize(2*n-1, id1);
        lazy.resize(2*n-1, id0);
    }

    LazySegmentTree (vector<T> v, F f, G g, H h, T id1, E id0, P p)
            : f(f), g(g), h(h), id1(id1), id0(id0), p(p) {
        n = 1; while (n < (int)v.size()) n *= 2;
        node.resize(2*n-1, id1);
        lazy.resize(2*n-1, id0);
        for (int i = 0; i < (int)v.size(); i++){
            node[i+n-1] = v[i];
        }
        for (int i = n-2; i >= 0; i--) {
            node[i] = f(node[2*i+1], node[2*i+2]);
        }
    }

    inline void eval (int len, int k) {
        if (lazy[k] == id0) return;
        if (k*2+1 < n*2-1) {
            lazy[2*k+1] = h(lazy[2*k+1], lazy[k]);
            lazy[2*k+2] = h(lazy[2*k+2], lazy[k]);
        }
        node[k] = g(node[k], p(lazy[k], len));
        lazy[k] = id0;
    }

    T update (int a, int b, E x, int k, int l, int r) {
        eval(r-l, k);
        if (r <= a || b <= l) return node[k];
        if (a <= l && r <= b) {
            lazy[k] = h(lazy[k], x);
            return g(node[k], p(lazy[k], r-l));
        }
        return node[k] = f(update(a, b, x, 2*k+1, l, (l+r)/2),
                           update(a, b, x, 2*k+2, (l+r)/2, r));
    }

    T update (int a, int b, E x) {
        return update(a, b, x, 0, 0, n);
    }

    T query (int a, int b, int k, int l, int r) {
        eval(r-l, k);
        if (r <= a || b <= l) return id1;
        if (a <= l && r <= b) return node[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }

    T query (int a, int b) {
        return query(a, b, 0, 0, n);
    }

};


int main() {
    int n, q;
    cin >> n >> q;
    
    const long long id1 = 0LL;
    const long long id0 = INT64_MAX;
    auto f = [](long long a, long long b) { return a + b; };
    auto g = [&](long long a, long long b) { return (b == id0 ? a : b); };
    auto h = [&](long long a, long long b) { return (b == id0 ? a : b); };
    auto p = [](long long a, int b) { return (long long)a*b; };
    LazySegmentTree<long long, long long> seg(vector<long long>(n, 0LL), f, g, h, id1, id0, p);


    while (q--) {
        int op;
        cin >> op;
        if (op == 0) {
            int s, t;
            long long x;
            cin >> s >> t >> x;
            seg.update(s, t+1, x);
        } else {
            int s, t;
            cin >> s >> t;
            cout << seg.query(s, t+1) << endl;
        }
    }
    return 0;
}