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)

#![allow(non_snake_case)]

#[allow(unused_macros)]
macro_rules! debug {
    ( $x:ident [ $i:expr ] ) => {
        if cfg!(debug_assertions) {
            println!("{}[{}] = {:?}", stringify!($x), $i, $x[$i]);
        }
    };
    ( $x:ident [ $i:expr ][ $j:expr ] ) => {
        if cfg!(debug_assertions) {
            println!("{}[{}][{}] = {:?}", stringify!($x), $i, $j, $x[$i][$j]);
        }
    };
    ( $x:expr ) => {
        if cfg!(debug_assertions) {
            println!("{}:{} {} = {:?}", line!(), column!(), stringify!($x), $x);
        }
    };
}

/// Return the smallest power of 2 such that<br>
/// it is greater than or equal to <code>x</code>.
/// # Panics
/// <code>x == 0</code> will cause underflow.
pub fn clp(mut x: usize) -> usize {
    x -= 1;
    x = x | (x >>  1);
    x = x | (x >>  2);
    x = x | (x >>  4);
    x = x | (x >>  8);
    x = x | (x >> 16);
    x = x | (x >> 32);
    x + 1
}

struct RangeUpdateQuery {
    n: usize,
    a: Vec<Option<i64>>,
}

impl RangeUpdateQuery {
    pub fn new(m: usize) -> Self {
        let n = clp(m);
        Self {
            n: n,
            a: vec![Some(0); 2 * n - 1]
        }
    }

    fn _update(&mut self, update_first: usize, update_last: usize, new_value: i64, node: usize, node_first: usize, node_last: usize) {
        //println!("update_first = {}, update_last = {}, node = {}, node_first = {}, node_last = {}",
        //    update_first, update_last, node, node_first, node_last);
        if node_last <= update_first || update_last <= node_first {
            //println!("nothing to do");
            return;
        } else if update_first <= node_first && node_last <= update_last {
            //println!("change all elements");
            self.a[node] = Some(new_value);
        } else {
            //println!("split");
            if self.a[node].is_some() {
                self.a[2 * node + 1] = self.a[node];
                self.a[2 * node + 2] = self.a[node];
                self.a[node] = None;
            }
            let node_mid: usize = node_first + (node_last - node_first) / 2;
            self._update(update_first, update_last, new_value, 2 * node + 1, node_first, node_mid);
            self._update(update_first, update_last, new_value, 2 * node + 2, node_mid, node_last);
        }
    }

    pub fn update(&mut self, update_first: usize, update_last: usize, new_value: i64) {
        let n = self.n;
        self._update(update_first, update_last, new_value, 0, 0, n);
    }

    fn _query(&self, query_first: usize, query_last: usize, node: usize, node_first: usize, node_last: usize) -> i64 {
        //println!("query_first = {}, query_last = {}, node = {}, node_first = {}, node_last = {}",
            //query_first, query_last, node, node_first, node_last);
        if node_last <= query_first || query_last <= node_first {
            //println!("out of search scope, return unit");
            0
        } else if self.a[node].is_some() {
            let ret = self.a[node].unwrap() * (std::cmp::min(query_last, node_last) - std::cmp::max(query_first, node_first)) as i64;
            //println!("already calculated, return {:?}", ret);
            ret
        } else {
            //println!("new calculation start");
            let node_mid: usize = node_first + (node_last - node_first) / 2;
            let left: i64 = self._query(query_first, query_last, node * 2 + 1, node_first, node_mid);
            let right: i64 = self._query(query_first, query_last, node * 2 + 2, node_mid, node_last);
            //println!("new calculation end. return {:?}", (self.f)(left, right));
            left + right
        }
    }

    pub fn query(&self, first: usize, last: usize) -> i64 {
        self._query(first, last, 0, 0, self.n)
    }
}

fn main() {
    use std::io::{Write, BufWriter};

    let stdin = std::io::stdin();
    let mut stdin = my::io::InputReader::new(stdin.lock());
    let stdout = std::io::stdout();
    let mut stdout = BufWriter::new(stdout);

    let n: usize = stdin.read();
    let q: usize = stdin.read();
    let mut ruq = RangeUpdateQuery::new(n);
    for _ in 0..q {
        let query_type: i32 = stdin.read();
        if query_type == 0 {
            let first: usize = stdin.read();
            let last: usize = stdin.read::<usize>() + 1;
            let x: i64 = stdin.read();
            //println!("update query [{}, {}) <- {}", first, last, x);
            ruq.update(first, last, x);
        } else if query_type == 1 {
            let first: usize = stdin.read();
            let last: usize = stdin.read::<usize>() + 1;
            //println!("search query [{}, {})", first, last);
            stdout.write_fmt(format_args!("{}\n", ruq.query(first, last)));
        }
        //println!("");
    }
}

#[allow(unused_must_use)]
mod my {
    pub mod io {
        use std::io::{BufRead, ErrorKind, Result};
        use std::str::{FromStr, from_utf8};
        use std::fmt::Debug;

        pub fn read_word<R: BufRead>(r: &mut R, buf: &mut Vec<u8>)
                                              -> Result<usize> {
            let mut read = 0;
            loop {
                let (done, used) = {
                    let available = match r.fill_buf() {
                        Ok(n) => n,
                        Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
                        Err(e) => return Err(e)
                    };
                    match available.iter().position(|&x| x == b'\n' || x == b' ') {
                        Some(i) => {
                            buf.extend_from_slice(&available[..i]);
                            (true, i + 1)
                        }
                        None => {
                            buf.extend_from_slice(available);
                            (false, available.len())
                        }
                    }
                };
                r.consume(used);
                read += used;
                if done || used == 0 {
                    return Ok(read);
                }
            }
        }

        pub struct InputReader<R: BufRead> {
            reader: R,
            buf: Vec<u8>,
        }

        impl<R: BufRead> InputReader<R> {
            pub fn new(reader: R) -> InputReader<R> {
                InputReader {
                    reader: reader,
                    buf: Vec::<u8>::new(),
                }
            }

            pub fn read_word(&mut self) {
                read_word(&mut self.reader, &mut self.buf);
            }
            
            pub fn read<T>(&mut self) -> T
                where T: FromStr,
                      T::Err: Debug
            {
                self.buf.clear();
                self.read_word();
                from_utf8(&self.buf).unwrap().trim().parse().unwrap()
            }
        }
    }
}