Problem:
Score : 500 points

Problem Statement

You have a string S of length N.
Initially, all characters in S are 1s.

You will perform queries Q times.
In the i-th query, you are given two integers L_i, R_i and a character D_i (which is a digit).
Then, you must replace all characters from the L_i-th to the R_i-th (inclusive) with D_i.

After each query, read the string S as a decimal integer, and print its value modulo 998,244,353.

Constraints

1 \leq N, Q \leq 200,000

1 \leq L_i \leq R_i \leq N

1 \leq D_i \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
L_1 R_1 D_1
:
L_Q R_Q D_Q

Output

Print Q lines.
In the i-th line print the value of S after the i-th query, modulo 998,244,353.

Sample Input 1

8 5
3 6 2
1 4 7
3 8 3
2 2 2
4 5 1

Sample Output 1

11222211
77772211
77333333
72333333
72311333

Sample Input 2

200000 1
123 456 7

Sample Output 2

641437905

Don't forget to take the modulo.

Solution in Rust (1.42.0) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(unused_macros)]
use itertools::Itertools;
use std::cmp::{max, min};
use std::collections::*;
use std::fmt::Write as _Write;
use std::i64;
use std::io::{stdin, BufWriter, Read, Write};
use std::usize;

#[allow(unused_macros)]
macro_rules! parse {
    ($it: ident ) => {};
    ($it: ident, ) => {};
    ($it: ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, $var:ident $($r:tt)*) => {
        let $var = parse_val!($it, usize);
        parse!($it $($r)*);
    };
}

#[allow(unused_macros)]
macro_rules! parse_val {
    ($it: ident, [$t:tt; $len:expr]) => {
        (0..$len).map(|_| parse_val!($it, $t)).collect::<Vec<_>>();
    };
    ($it: ident, ($($t: tt),*)) => {
        ($(parse_val!($it, $t)),*)
    };
    ($it: ident, u1) => {
        $it.next().unwrap().parse::<usize>().unwrap() -1
    };
    ($it: ident, $t: ty) => {
        $it.next().unwrap().parse::<$t>().unwrap()
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    parse!(it, n: usize, q: usize);
    let mut s = BTreeSet::<(usize, usize, i64)>::new();
    let mut rm = vec![];
    let mut add = vec![];
    let mut ones = vec![0; n];
    let m = 998244353;
    let mut c = 1;
    for i in (0..n).rev() {
        ones[i] = c;
        c *= 10;
        c += 1;
        c %= m;
    }
    let mut ret = ones[0];
    let fx = |l: usize, r: usize, d: i64| ((ones[l] - if r < n { ones[r] } else { 0 }) * d) % m;
    s.insert((0, n, 1));
    for _ in 0..q {
        parse!(it, l: u1, r: usize, d: i64);
        rm.clear();
        add.clear();
        add.push((l, r, d));
        for &(li, ri, di) in s.range((l, 0, 0)..(r, !0, !0)) {
            rm.push((li, ri, di));
            if ri > r {
                add.push((r, ri, di));
            }
        }

        for &(li, ri, di) in &rm {
            ret -= fx(li, ri, di);
            ret %= m;
            s.remove(&(li, ri, di));
        }
        rm.clear();

        if let Some(&(li, ri, z)) = s.range(..(r, r, !0)).rev().next() {
            if ri > l {
                rm.push((li, ri, z));
                add.push((li, l, z));
            }
            if ri > r {
                add.push((r, ri, z));
            }
        }
        for &(li, ri, di) in &rm {
            ret -= fx(li, ri, di);
            ret %= m;
            s.remove(&(li, ri, di));
        }
        for &(li, ri, di) in &add {
            ret += fx(li, ri, di);
            ret %= m;
            s.insert((li, ri, di));
        }
        // eprintln!("rm  {:?}", rm); // DEBUG
        // eprintln!("add {:?}", add); // DEBUG
        // eprintln!("s   {:?}", s); // DEBUG
        ret %= m;
        if ret < 0 {
            ret += m;
        }
        println!("{:?}", ret); // DEBUG
    }
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}
trait ChMinMax {
    fn chmin(&mut self, other: Self);
    fn chmax(&mut self, other: Self);
}
impl<T> ChMinMax for T
where
    T: PartialOrd,
{
    fn chmin(&mut self, other: Self) {
        if *self > other {
            *self = other
        }
    }
    fn chmax(&mut self, other: Self) {
        if *self < other {
            *self = other
        }
    }
}