use input::input_array; use input::input_vec; use std::cmp::Ordering; use std::collections::BTreeMap; fn main() { let [n, _m] = input_array::(); let a = input_vec::(); let mut dist = BTreeMap::from_iter(vec![ (0, usize::MAX), (1, 0), (n, n - 1), (n + 1, usize::MAX), ]); for &x1 in a.iter().rev() { let x2 = x1 + 1; lerp(&mut dist, x1); lerp(&mut dist, x2); match dist[&x1].cmp(&dist[&x2]) { Ordering::Equal => {} Ordering::Less => { let section = dist[&x1] as isize - x1 as isize; let on_line = |&(x, y): &(&usize, &usize)| *y as isize == section + *x as isize; lerp(&mut dist, x1 - 1); *dist.get_mut(&x1).unwrap() += usize::from(dist[&(x1 - 1)] >= dist[&x1]); if let Some((&(mut x3), &(mut y3))) = dist.range(x2 + 1..).next().filter(on_line) { while let Some((&x, &y)) = dist.range(x3 + 1..).next().filter(on_line) { dist.remove(&x3); (x3, y3) = (x, y); } lerp(&mut dist, x3 + 1); dist.insert(x3, y3 - 1); } dist.insert(x2, dist[&x2] - 1); } Ordering::Greater => { let section = dist[&x2] as isize + x2 as isize; let on_line = |&(x, y): &(&usize, &usize)| *y as isize == section - *x as isize; lerp(&mut dist, x2 + 1); *dist.get_mut(&x2).unwrap() += usize::from(dist[&(x2 + 1)] >= dist[&x2]); if let Some((&(mut x0), &(mut y0))) = dist.range(..x1).next_back().filter(on_line) { while let Some((&x, &y)) = dist.range(..x0).next_back().filter(on_line) { dist.remove(&x0); (x0, y0) = (x, y); } lerp(&mut dist, x0 - 1); dist.insert(x0, y0 - 1); } dist.insert(x1, dist[&x1] - 1); } } } println!( "{}", (2..=n) .map(|i| { lerp(&mut dist, i); dist[&i].to_string() }) .collect::>() .join(" ") ); } fn lerp(map: &mut BTreeMap, x: usize) { if !map.contains_key(&x) { let (x0, &y0) = map.range(..=x).next_back().unwrap(); let (x1, &y1) = map.range(x..).next().unwrap(); assert!(y0 == y1 || y0.abs_diff(y1) == x1 - x0); let y = y0 + (x - x0) * (y1 - y0) / (x1 - x0); map.insert(x, y); } } // input {{{ #[allow(dead_code)] mod input { use std::cell::Cell; use std::convert::TryFrom; use std::io::stdin; use std::io::BufRead; use std::io::BufReader; use std::io::Lines; use std::io::Stdin; use std::str::FromStr; use std::sync::Mutex; use std::sync::Once; type Server = Mutex>>; static ONCE: Once = Once::new(); pub struct Lazy(Cell>); unsafe impl Sync for Lazy {} fn line() -> String { static SYNCER: Lazy = Lazy(Cell::new(None)); ONCE.call_once(|| { SYNCER .0 .set(Some(Mutex::new(BufReader::new(stdin()).lines()))); }); unsafe { (*SYNCER.0.as_ptr()) .as_ref() .unwrap() .lock() .unwrap() .next() .unwrap() .unwrap() } } pub trait ForceFromStr: FromStr { fn force_from_str(s: &str) -> Self; } impl ForceFromStr for T where T: FromStr, E: std::fmt::Debug, { fn force_from_str(s: &str) -> Self { s.parse().unwrap() } } pub fn input_array() -> [T; N] where T: std::fmt::Debug, { <[_; N]>::try_from(input_vec()).unwrap() } pub fn input_vec() -> Vec { line() .split_whitespace() .map(T::force_from_str) .collect::>() } pub fn input() -> T { T::force_from_str(&line()) } } // }}}