結果

問題 No.2743 Twisted Lattice
ユーザー Yukino DX.Yukino DX.
提出日時 2024-05-01 16:24:45
言語 Rust
(1.77.0)
結果
AC  
実行時間 723 ms / 3,000 ms
コード長 3,117 bytes
コンパイル時間 13,854 ms
コンパイル使用メモリ 407,272 KB
実行使用メモリ 45,960 KB
最終ジャッジ日時 2024-05-01 16:25:12
合計ジャッジ時間 22,238 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 703 ms
44,416 KB
testcase_01 AC 405 ms
14,628 KB
testcase_02 AC 388 ms
13,664 KB
testcase_03 AC 723 ms
44,292 KB
testcase_04 AC 693 ms
45,832 KB
testcase_05 AC 685 ms
45,832 KB
testcase_06 AC 708 ms
45,960 KB
testcase_07 AC 659 ms
45,832 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
//use itertools::{iproduct, Itertools};
use proconio::input;
use proconio::marker::*;
use std::collections::*;

fn main() {
    input! {
        _:usize,
        _:usize,
        n:usize,
        ab:[(i64,i64);n],
    }

    let amb = ab.iter().map(|&(a, b)| a - b).collect::<Vec<_>>();
    let apb = ab.iter().map(|&(a, b)| a + b).collect::<Vec<_>>();

    let mut cums = BTreeMap::new();
    let mut cums_rev = BTreeMap::new();
    let mut cols = HashMap::new();

    for i in 0..n {
        cols.entry(ab[i].1).or_insert(vec![]).push(ab[i].0);
    }
    for vals in cols.values_mut() {
        vals.sort();
    }

    let mut sorted_idx = (0..n).collect::<Vec<_>>();
    sorted_idx.sort_by_key(|&i| ab[i].1);

    let mut cum = INF;
    for &i in sorted_idx.iter() {
        cum = cum.min(amb[i]);
        cums.insert(ab[i].1, cum);
    }

    sorted_idx.reverse();
    cum = INF;
    for &i in sorted_idx.iter() {
        cum = cum.min(apb[i]);
        cums_rev.insert(ab[i].1, cum);
    }

    for i in 0..n {
        let mut ans = INF;
        if let Some((_, &cmb)) = cums.range(..=ab[i].1 - 2).last() {
            ans = ans.min(apb[i] + cmb - 2);
        }
        if let Some((_, &cpb)) = cums_rev.range(ab[i].1 + 2..).next() {
            ans = ans.min(amb[i] + cpb - 2);
        }
        if let Some(&c) = cols.get(&(ab[i].1 - 1)).map(|xs| xs.first().unwrap()) {
            ans = ans.min(ab[i].0.max(c));
        }
        if let Some(&c) = cols.get(&(ab[i].1 + 1)).map(|xs| xs.first().unwrap()) {
            ans = ans.min(ab[i].0.max(c));
        }
        if let Some(cand) = cols.get(&ab[i].1).map(|xs| {
            let id = xs.lower_bound(ab[i].0 + 1);
            let mut res = INF;
            if id < xs.len() {
                res = res.min(xs[id] - ab[i].0);
            }

            let id = xs.upper_bound(ab[i].0 - 1);
            if !0 != id {
                res = res.min(ab[i].0 - xs[id]);
            }

            res
        }) {
            ans = ans.min(cand);
        }

        println!("{}", ans);
    }
}

const INF: i64 = std::i64::MAX;

use crate::bound::Bound;
mod bound {
    pub trait Bound<T> {
        fn lower_bound(&self, val: T) -> usize;
        fn upper_bound(&self, val: T) -> usize;
    }

    impl<T> Bound<T> for [T]
    where
        T: Ord + Copy,
    {
        fn lower_bound(&self, val: T) -> usize {
            let mut ok = self.len();
            let mut ng = !0;
            while ok.wrapping_sub(ng) > 1 {
                let m = ok.wrapping_add(ng) / 2;
                if val <= self[m] {
                    ok = m;
                } else {
                    ng = m;
                }
            }

            ok
        }

        fn upper_bound(&self, val: T) -> usize {
            let mut ok = !0;
            let mut ng = self.len();
            while ng.wrapping_sub(ok) > 1 {
                let m = ng.wrapping_add(ok) / 2;
                if self[m] <= val {
                    ok = m;
                } else {
                    ng = m;
                }
            }

            ok
        }
    }
}
0