// use std::cell::{Ref, RefMut, RefCell}; // use std::sync::{Arc, Mutex}; #[allow(unused_macros)] macro_rules! input { ( $($t:ty),* ) => {{ let mut s = String::new(); std::io::stdin().read_line(&mut s); let mut splits = s.trim().split_whitespace(); ($( { splits.next().unwrap().parse::<$t>().unwrap() },)*) }} } #[allow(unused_macros)] macro_rules! invec { ( $ t : ty ) => {{ let mut s = String::new(); match std::io::stdin().read_line(&mut s) { Ok(0) => Vec::<$t>::new(), Ok(n) => s .trim() .split_whitespace() .map(|s| s.parse::<$t>().unwrap()) .collect::>(), Err(_) => Vec::<$t>::new(), } }}; } enum SegmentTree { Leaf(u32), Node(u32, usize, Box, Box), } impl SegmentTree { fn query(&self, from: usize, to: usize, n: usize) -> (u32, usize) { match self { SegmentTree::Leaf(v) => (*v, 0), SegmentTree::Node(v, index, left, right) => { if to - from + 1 == n { return (*v, *index); } let boundary = n / 2; if from < boundary && boundary <= to { let right_to = to - boundary; let (lval, lindex) = left.as_ref().query(from, boundary - 1, boundary); let (rval, rindex) = right.as_ref().query(0, right_to, n - boundary); let rindex = rindex + boundary; if lval < rval { return (lval, lindex); } else { return (rval, rindex); } } if from < boundary { return left.as_ref().query(from, to, boundary); } else { // println!("ltmp = {}, rtmp = {}", ltmp, rtmp); let right_from = from - boundary; let right_to = to - boundary; let (rval, rindex) = right.as_ref().query(right_from, right_to, n - boundary); return (rval, rindex + boundary); } } } } fn update(&mut self, index: usize, value: u32, n: usize) { match self { SegmentTree::Leaf(ref mut v) => { // println!("updated leaf val = {}", value); *v = value; // (*v)[index] = value; // SegmentTree::Leaf(index, v) return; }, SegmentTree::Node(ref mut val, ref mut ind, ref mut left, ref mut right) => { let boundary = n / 2; if index < boundary { // println!("updating left : index = {}, value = {}", index, value); left.update(index, value, boundary); // println!("updated left : value = {}, fact value = {}", value, left.val()); } else { // let right_index = index - boundary; // println!("updating right : index = {}, value = {}", index, value); right.update(index - boundary, value, n - boundary); // println!("updated right : value = {}, fact value = {}", value, right.val()); } let (new_val, new_index) = if left.val() <= right.val() { (left.val(), left.ind()) } else { (right.val(), right.ind() + boundary) }; *val = new_val; *ind = new_index; } } } fn new(a: &[u32], n: usize) -> SegmentTree { if n == 1 { // println!("new(leaf) : n = {}, index = {}", n, index); SegmentTree::Leaf(a[0], ) } else { // println!("new : n = {}, index = {}", n, index); let half = n / 2; let left = SegmentTree::new(&a[0..half], half); let right = SegmentTree::new(&a[half..n], n - half); // println!("new : n = {}, index = {} leftright end", n, index); // let left_value = left.val; // let right_value = right.val; let (i, val) = if left.val() <= right.val() { (left.ind(), left.val()) } else { (right.ind() + half, right.val()) }; // println!("i updated"); SegmentTree::Node(val, i, Box::new(left), Box::new(right)) } } fn val(&self) -> u32 { match *self { SegmentTree::Node(v, _, _, _) => v, SegmentTree::Leaf(v) => v, } } fn ind(&self) -> usize { match *self { SegmentTree::Node(_, i, _, _) => i, SegmentTree::Leaf(_) => 0, } } } #[allow(unused_must_use)] #[allow(unused_variables)] fn solve() { let (n, q) = input!(usize, usize); let a = invec!(u32); // println!("kokoko"); // let mtx = Arc::new(Mutex::new(a)); let mut tree = SegmentTree::new(&a[..], n); // println!("kokoko"); for _ in 0..q { let (op, l, r) = input!(u8, usize, usize); let l = l - 1; let r = r - 1; match op { 1 => { let (ltmp, _) = tree.query(l, l, n); let (rtmp, _) = tree.query(r, r, n); // println!("ltmp = {}, rtmp = {}", ltmp, rtmp); (&mut tree).update(l, rtmp, n); // println!("swapping {}, {}...", l, r); (&mut tree).update(r, ltmp, n); }, 2 => { let res = tree.query(l, r, n); println!("{}", res.1 + 1); }, _ => {}, } } } fn main() { solve(); }