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 Rust (judge verdict: Accepted)

use std::io;
use std::io::prelude::*;

struct SegTree {
    n: usize,
    nodes: Vec<(i32, Option<i32>)>,
}

impl SegTree {
    fn new(n: usize) -> SegTree {
        let n = 1 << f64::log2(n as f64).ceil() as usize;
        SegTree {
            n,
            nodes: vec![(0, None); 2 * n - 1],
        }
    }

    fn update(&mut self, s: usize, t: usize, x: i32, ni: usize, l: usize, r: usize) {
        self.eval(ni);
        if r <= s || t <= l {
            return;
        }
        if s <= l && r <= t {
            self.nodes[ni].1 = Some(x * (r - l) as i32);
            self.eval(ni);
            return;
        }
        let m = (l + r) / 2;
        self.update(s, t, x, 2 * ni + 1, l, m);
        self.update(s, t, x, 2 * ni + 2, m, r);
        self.nodes[ni].0 = self.nodes[2 * ni + 1].0 + self.nodes[2 * ni + 2].0;
    }

    fn eval(&mut self, ni: usize) -> i32 {
        match self.nodes[ni].1.take() {
            Some(x) => {
                self.nodes[ni].0 = x;
                if ni < self.n - 1 {
                    self.nodes[2 * ni + 1].1 = Some(x / 2);
                    self.nodes[2 * ni + 2].1 = Some(x / 2);
                }
            }
            None => {}
        }
        self.nodes[ni].0
    }

    fn get_sum(&mut self, s: usize, t: usize, ni: usize, l: usize, r: usize) -> i32 {
        self.eval(ni);
        if s <= l && r <= t {
            self.nodes[ni].0
        } else if r <= s || t <= l {
            0
        } else {
            let m = (l + r) / 2;
            self.get_sum(s, t, 2 * ni + 1, l, m) + self.get_sum(s, t, 2 * ni + 2, m, r)
        }
    }
}

fn main() {
    let stdin = io::stdin();
    let mut lines = stdin.lock().lines();

    let line0 = lines.next().unwrap().unwrap();
    let n = line0.split_whitespace().next().unwrap().parse().unwrap();

    let mut st = SegTree::new(n);

    for l in lines.map(Result::unwrap) {
        let mut words = l.split_whitespace();
        let com = words.next().unwrap().parse().unwrap();
        let s: usize = words.next().unwrap().parse().unwrap();
        let t: usize = words.next().unwrap().parse().unwrap();
        let n = st.n;
        match com {
            0 => {
                let x = words.next().unwrap().parse().unwrap();
                st.update(s, t + 1, x, 0, 0, n);
            }
            1 => println!("{}", st.get_sum(s, t + 1, 0, 0, n)),
            _ => (),
        }
    }
}