Problem:
Score : 100 points

Problem Statement

You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types.

The type of i-th query is represented by T_i.

T_i=1: You are given two integers X_i,V_i. Replace the value of A_{X_i} with V_i.

T_i=2: You are given two integers L_i,R_i. Calculate the maximum value among A_{L_i},A_{L_i+1},\cdots,A_{R_i}.

T_i=3: You are given two integers X_i,V_i. Calculate the minimum j such that X_i \leq j \leq N, V_i \leq A_j. If there is no such j, answer j=N+1 instead.

Constraints

1 \leq N \leq 2 \times 10^5

0 \leq A_i \leq 10^9

1 \leq Q \leq 2 \times 10^5

1 \leq T_i \leq 3

1 \leq X_i \leq N, 0 \leq V_i \leq 10^9 (T_i=1,3)

1 \leq L_i \leq R_i \leq N (T_i=2)

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 \cdots A_N
First query
Second query
\vdots
Q-th query

Each query is given in the following format:

If T_i=1,3,

T_i X_i V_i

If T_i=2,

T_i L_i R_i

Output

For each query with T_i=2, 3, print the answer.

Sample Input 1

5 5
1 2 3 2 1
2 1 5
3 2 3
1 3 1
2 2 4
3 1 3

Sample Output 1

3
3
2
6

First query: Print 3, which is the maximum of (A_1,A_2,A_3,A_4,A_5)=(1,2,3,2,1).

Second query: Since 3>A_2, j=2 does not satisfy the condition．Since 3 \leq A_3, print j=3.

Third query: Replace the value of A_3 with 1. It becomes A=(1,2,1,2,1).

Fourth query: Print 2, which is the maximum of (A_2,A_3,A_4)=(2,1,2).

Fifth query: Since there is no j that satisfies the condition, print j=N+1=6.

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

use seg_tree::*;
use std::cmp::*;
use std::io::*;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o).unwrap();
    stdout().write_all(&o).unwrap();
}

struct Oracle;

impl SegTreeOracle for Oracle {
    type S = usize;

    fn e() -> Self::S {
        usize::min_value()
    }

    fn op(l: Self::S, r: Self::S) -> Self::S {
        max(l, r)
    }
}

pub fn run(i: impl BufRead, o: &mut impl Write) -> std::io::Result<()> {
    let mut i = CpReader::new(i);
    let (n, q) = i.read::<(usize, usize)>();
    let mut t = SegTree::<Oracle>::from_iter(i.read_iter::<usize>(n));
    for q in i.read_iter::<(i8, usize, usize)>(q) {
        match q.0 {
            1 => {
                t.set(q.1 - 1, q.2);
            }
            2 => writeln!(o, "{}", t.prod(q.1 - 1..=q.2 - 1))?,
            3 => writeln!(o, "{}", t.max_right(q.1 - 1, |x| x < q.2) + 1)?,
            _ => unreachable!(),
        }
    }
    Ok(())
}

pub mod utils {
    use super::*;

    pub struct CpReader<R: BufRead> {
        r: R,
        b: Vec<u8>,
    }
    impl<R: BufRead> CpReader<R> {
        pub fn new(r: R) -> Self {
            CpReader {
                r: r,
                b: Vec::new(),
            }
        }
        pub fn read_word(&mut self) -> &[u8] {
            self.b.clear();
            loop {
                let b = self.r.fill_buf().unwrap();
                assert!(!b.is_empty());
                if let Some(p) = b.iter().position(|&x| x == b' ' || x == b'\n') {
                    self.b.extend_from_slice(&b[..p]);
                    self.r.consume(p + 1);
                    return &self.b;
                }
                self.b.extend_from_slice(b);
                let b_len = b.len();
                self.r.consume(b_len);
            }
        }
        pub fn read_word_str(&mut self) -> &str {
            unsafe { std::str::from_utf8_unchecked(self.read_word()) }
        }

        pub fn read_line(&mut self) -> &[u8] {
            self.b.clear();
            self.r.read_until(b'\n', &mut self.b).unwrap();
            if self.b.last() == Some(&b'\n') {
                &self.b[..self.b.len() - 1]
            } else {
                &self.b
            }
        }
        pub fn read_line_str(&mut self) -> &str {
            unsafe { std::str::from_utf8_unchecked(self.read_line()) }
        }

        pub fn read<T: CpIn>(&mut self) -> T {
            T::read_from(self)
        }

        pub fn read_vec<T: CpIn>(&mut self, n: usize) -> Vec<T> {
            (0..n).map(|_| self.read()).collect()
        }
        pub fn read_iter<'a, T: CpIn>(&'a mut self, n: usize) -> CpIter<'a, R, T> {
            CpIter {
                r: self,
                n: n,
                _pd: Default::default(),
            }
        }
    }
    pub struct CpIter<'a, R: BufRead + 'a, T> {
        r: &'a mut CpReader<R>,
        n: usize,
        _pd: std::marker::PhantomData<fn() -> T>,
    }
    impl<'a, R: BufRead, T: CpIn> Iterator for CpIter<'a, R, T> {
        type Item = T;
        fn next(&mut self) -> Option<T> {
            if self.n == 0 {
                None
            } else {
                self.n -= 1;
                Some(self.r.read())
            }
        }
        fn size_hint(&self) -> (usize, Option<usize>) {
            (self.n, Some(self.n))
        }
    }
    impl<'a, R: BufRead, T: CpIn> ExactSizeIterator for CpIter<'a, R, T> {}

    pub trait CpIn {
        fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self;
    }

    impl CpIn for u64 {
        fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self {
            read_u64_fast(&mut r.r)
        }
    }
    impl CpIn for i64 {
        fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self {
            read_i64_fast(&mut r.r)
        }
    }
    impl CpIn for char {
        fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self {
            let b = r.r.fill_buf().unwrap()[0] as char;
            r.r.consume(1);
            let s = r.r.fill_buf().unwrap()[0];
            assert!(s == b' ' || s == b'\n');
            r.r.consume(1);
            b
        }
    }

    macro_rules! cpin_tuple {
        ($($t:ident),*) => {
            impl<$($t: CpIn),*> CpIn for ($($t),*) {
                fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self {
                    ($($t::read_from(r)),*)
                }
            }
        };
    }

    macro_rules! cpin_cast {
        ($t_self:ty, $t_read:ty) => {
            impl CpIn for $t_self {
                fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self {
                    <$t_read>::read_from(r) as $t_self
                }
            }
        };
    }
    macro_rules! cpin_parse {
        ($t:ty) => {
            impl CpIn for $t {
                fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self {
                    r.read_word_str().parse().unwrap()
                }
            }
        };
    }
    cpin_cast!(usize, u64);
    cpin_cast!(u32, u64);
    cpin_cast!(u16, u64);
    cpin_cast!(i32, i64);
    cpin_cast!(i16, i64);
    cpin_cast!(i8, i64);

    cpin_parse!(f64);

    cpin_tuple!(T1, T2);
    cpin_tuple!(T1, T2, T3);
    cpin_tuple!(T1, T2, T3, T4);
    cpin_tuple!(T1, T2, T3, T4, T5);

    fn read_u64_fast<R: BufRead>(r: &mut R) -> u64 {
        let mut value = 0;
        loop {
            let buf = r.fill_buf().unwrap();
            assert!(!buf.is_empty());
            let mut idx = 0;
            while let Some(&c) = buf.get(idx) {
                if b'0' <= c && c <= b'9' {
                    value = value * 10 + (c - b'0') as u64;
                    idx += 1;
                } else {
                    r.consume(idx + 1);
                    return value;
                }
            }
            r.consume(idx);
        }
    }
    fn read_i64_fast<R: BufRead>(r: &mut R) -> i64 {
        let sign = match r.fill_buf().unwrap()[0] {
            b'+' => {
                r.consume(1);
                1
            }
            b'-' => {
                r.consume(1);
                -1
            }
            _ => 1,
        };
        read_u64_fast(r) as i64 * sign
    }
}
mod internal_bit {
    pub fn ceil_pow2(n: usize) -> usize {
        let mut x = 0;
        while (1 << x) < n {
            x += 1;
        }
        return x;
    }
}
mod helpers {
    use std::ops::{Bound, Range, RangeBounds};
    pub fn to_range(len: usize, range: impl RangeBounds<usize>) -> Range<usize> {
        let start = match range.start_bound() {
            Bound::Included(idx) => *idx,
            Bound::Excluded(idx) => *idx + 1,
            Bound::Unbounded => 0,
        };
        let end = match range.end_bound() {
            Bound::Included(idx) => *idx + 1,
            Bound::Excluded(idx) => *idx,
            Bound::Unbounded => len,
        };
        debug_assert!(start <= end);
        debug_assert!(end <= len);
        start..end
    }
}

mod seg_tree {
    #![allow(dead_code)]
    use crate::{helpers::to_range, internal_bit::ceil_pow2};
    use std::ops::RangeBounds;

    pub struct SegTree<O: SegTreeOracle> {
        n: usize,
        d: Vec<O::S>,
        size: usize,
        log: usize,
    }

    impl<O: SegTreeOracle> SegTree<O> {
        pub fn new(n: usize) -> Self {
            let log = ceil_pow2(n);
            let size = 1 << log;
            let d = vec![O::e(); 2 * size];
            Self { n, d, size, log }
        }
        pub fn from_slice(v: &[O::S]) -> Self {
            let mut s = Self::new(v.len());
            s.d[s.size..s.size + v.len()].copy_from_slice(v);
            for i in (1..s.size).rev() {
                s.update(i);
            }
            s
        }
        pub fn from_iter(iter: impl Iterator<Item = O::S> + ExactSizeIterator) -> Self {
            let len = iter.len();
            let mut s = Self::new(len);
            let mut idx = s.size;
            for item in iter {
                s.d[idx] = item;
                idx += 1;
            }
            for i in (1..s.size).rev() {
                s.update(i);
            }
            s
        }

        #[inline]
        pub fn set(&mut self, index: usize, value: O::S) {
            let p = index + self.size;
            self.d[p] = value;
            for i in 1..=self.log {
                self.update(p >> i)
            }
        }

        #[inline]
        pub fn get(&self, index: usize) -> O::S {
            self.d[index + self.size]
        }

        pub fn prod(&mut self, range: impl RangeBounds<usize>) -> O::S {
            let range = to_range(self.n, range);
            let mut l = range.start;
            let mut r = range.end;
            let mut sml = O::e();
            let mut smr = O::e();
            l += self.size;
            r += self.size;
            while l < r {
                if l & 1 != 0 {
                    sml = O::op(sml, self.d[l]);
                    l += 1;
                };
                if r & 1 != 0 {
                    r -= 1;
                    smr = O::op(self.d[r], smr);
                }
                l >>= 1;
                r >>= 1;
            }
            return O::op(sml, smr);
        }
        #[inline]
        pub fn all_prod(&self) -> O::S {
            self.d[1]
        }

        pub fn max_right(&self, mut l: usize, f: impl Fn(O::S) -> bool) -> usize {
            debug_assert!(l <= self.n);
            debug_assert!(f(O::e()));
            if l == self.n {
                return self.n;
            }
            l += self.size;
            let mut sm = O::e();
            loop {
                while l % 2 == 0 {
                    l >>= 1;
                }
                if !f(O::op(sm, self.d[l])) {
                    while l < self.size {
                        l = 2 * l;
                        if f(O::op(sm, self.d[l])) {
                            sm = O::op(sm, self.d[l]);
                            l += 1;
                        }
                    }
                    return l - self.size;
                }
                sm = O::op(sm, self.d[l]);
                l += 1;
                if (l & l.wrapping_neg()) == l {
                    break;
                }
            }
            self.n
        }
        pub fn min_left(&self, mut r: usize, f: impl Fn(O::S) -> bool) -> usize {
            debug_assert!(r <= self.n);
            debug_assert!(f(O::e()));
            if r == 0 {
                return 0;
            }
            r += self.size;
            let mut sm = O::e();
            loop {
                r -= 1;
                while r > 1 && (r % 2) != 0 {
                    r >>= 1;
                }
                if !f(O::op(self.d[r], sm)) {
                    while r < self.size {
                        r = 2 * r + 1;
                        if f(O::op(self.d[r], sm)) {
                            sm = O::op(self.d[r], sm);
                            r -= 1;
                        }
                    }
                    return r + 1 - self.size;
                }
                sm = O::op(self.d[r], sm);
                if (r & r.wrapping_neg()) == r {
                    break;
                }
            }
            0
        }

        #[inline]
        fn update(&mut self, k: usize) {
            self.d[k] = O::op(self.d[2 * k], self.d[2 * k + 1]);
        }
    }

    pub trait SegTreeOracle {
        type S: Copy;
        fn e() -> Self::S;
        fn op(l: Self::S, r: Self::S) -> Self::S;
    }
}