use std::{cmp::min, collections::BTreeMap}; use proconio::{fastout, input, marker::Usize1}; const DIRS: [(isize, isize); 4] = [(0, 1), (1, 0), (0, -1), (-1, 0)]; trait OrdExt { fn chmax(&mut self, other: Self) -> bool; fn chmin(&mut self, other: Self) -> bool; } impl OrdExt for T { fn chmax(&mut self, other: Self) -> bool { if *self < other { *self = other; true } else { false } } fn chmin(&mut self, other: Self) -> bool { if *self > other { *self = other; true } else { false } } } #[fastout] fn main() { input! { _N: usize, Q: usize, } let mut map = BTreeMap::from([(0, 0)]); for _ in 0..Q { input! { T: usize, } if T == 1 { input! { x: Usize1, } let left = map.range(..x).next_back(); let right = map.range(x..).next(); match (left, right) { (Some((&l, &cl)), Some((&r, &cr))) => { println!("{}", min(cl + x - l, cr + r - x)); } (Some((&l, &cl)), None) => { println!("{}", cl + x - l); } (None, Some((&r, &cr))) => { println!("{}", cr + r - x); } (None, None) => unreachable!(), } } else { input! { x: Usize1, c: usize, } let mut isok = true; if let Some((&l, &cl)) = map.range(..x).next_back() { isok &= c < cl + x - l; } if let Some((&r, &cr)) = map.range(x..).next() { isok &= c < cr + r - x; } if isok { map.insert(x, c); } } } }