struct Foo { vs: Vec, tb: Vec } impl Foo { fn new(n: usize) -> Self { let mut x = 1; while x < n { x *= 2; } Self { vs: vec![std::i64::MAX; n], tb: { let mut tb = vec![0; x * 2]; for i in 0 .. x { tb[x + i - 1] = std::cmp::min(n - 1, i); } while x > 1 { x /= 2; for i in 0 .. x { tb[x + i - 1] = tb[(x + i - 1) * 2 + 1]; } } tb } } } fn set(&mut self, i: usize, v: i64) { self.vs[i] = v; } fn update(&mut self, i: usize) { let mut p = i + self.tb.len() / 2 - 1; while p > 0 { p = (p - 1) / 2; if self.vs[self.tb[p * 2 + 2]] < self.vs[self.tb[p * 2 + 1]] { self.tb[p] = self.tb[p * 2 + 2]; } else { self.tb[p] = self.tb[p * 2 + 1] } } } fn get_range(&self, p: usize, lb: usize, ub: usize, lp: usize, rp: usize) -> usize { if lb == lp && ub == rp + 1 { self.tb[p] } else { let h = (ub + lb) / 2; if lp < h && h <= rp { let p1 = self.get_range(p * 2 + 1, lb, h, lp, h - 1); let p2 = self.get_range(p * 2 + 2, h, ub, h, rp); if self.vs[p2] < self.vs[p1] { p2 } else { p1 } } else if rp < h { self.get_range(p * 2 + 1, lb, h, lp, rp) } else { self.get_range(p * 2 + 2, h, ub, lp, rp) } } } fn get(&self, lp: usize, rp: usize) -> usize { self.get_range(0, 0, self.tb.len() / 2, lp, rp) } } fn main() { use std::io::{stdin, Read}; let mut buf = String::new(); stdin().read_to_string(&mut buf).unwrap(); let mut tok = buf.split_whitespace(); let mut get = || tok.next().unwrap(); macro_rules! get { ($t:ty) => (get().parse::<$t>().unwrap()); () => (get!(i64)); } let n = get!(usize); let q = get!(); let mut foo = Foo::new(n); for i in 0 .. n { let v = get!(); foo.set(i, v); foo.update(i); } for _ in 0 .. q { let c = get!(); let lp = get!(usize) - 1; let rp = get!(usize) - 1; if c == 1 { foo.vs.swap(lp, rp); foo.update(lp); foo.update(rp); } else { println!("{}", foo.get(lp, rp) + 1); } } }