結果

問題 No.1332 Range Nearest Query
ユーザー akakimidoriakakimidori
提出日時 2021-01-08 21:54:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 183 ms / 2,500 ms
コード長 5,155 bytes
コンパイル時間 1,575 ms
コンパイル使用メモリ 158,320 KB
実行使用メモリ 29,608 KB
最終ジャッジ日時 2023-08-10 09:21:05
合計ジャッジ時間 10,735 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 153 ms
24,212 KB
testcase_04 AC 151 ms
24,128 KB
testcase_05 AC 154 ms
24,128 KB
testcase_06 AC 104 ms
27,312 KB
testcase_07 AC 104 ms
27,372 KB
testcase_08 AC 104 ms
29,352 KB
testcase_09 AC 102 ms
27,396 KB
testcase_10 AC 102 ms
27,376 KB
testcase_11 AC 104 ms
27,336 KB
testcase_12 AC 103 ms
27,368 KB
testcase_13 AC 104 ms
27,380 KB
testcase_14 AC 104 ms
27,332 KB
testcase_15 AC 104 ms
27,376 KB
testcase_16 AC 175 ms
29,420 KB
testcase_17 AC 170 ms
29,416 KB
testcase_18 AC 172 ms
29,424 KB
testcase_19 AC 172 ms
29,440 KB
testcase_20 AC 168 ms
29,428 KB
testcase_21 AC 167 ms
29,408 KB
testcase_22 AC 165 ms
29,432 KB
testcase_23 AC 170 ms
29,608 KB
testcase_24 AC 183 ms
29,408 KB
testcase_25 AC 177 ms
29,428 KB
testcase_26 AC 72 ms
26,884 KB
testcase_27 AC 66 ms
26,880 KB
testcase_28 AC 28 ms
8,308 KB
testcase_29 AC 29 ms
8,340 KB
testcase_30 AC 30 ms
8,344 KB
testcase_31 AC 24 ms
8,368 KB
testcase_32 AC 30 ms
8,340 KB
testcase_33 AC 30 ms
8,336 KB
testcase_34 AC 26 ms
8,368 KB
testcase_35 AC 27 ms
8,296 KB
testcase_36 AC 27 ms
8,328 KB
testcase_37 AC 28 ms
8,292 KB
testcase_38 AC 114 ms
16,844 KB
testcase_39 AC 58 ms
9,400 KB
testcase_40 AC 180 ms
29,184 KB
testcase_41 AC 80 ms
12,140 KB
testcase_42 AC 113 ms
16,648 KB
testcase_43 AC 90 ms
13,332 KB
testcase_44 AC 142 ms
23,360 KB
testcase_45 AC 132 ms
22,980 KB
testcase_46 AC 106 ms
14,364 KB
testcase_47 AC 124 ms
17,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin chmin, chmax ----------
trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------
// ---------- begin SegmentTree Point update Range query ----------
mod segment_tree {
    pub struct PURQ<T, F> {
        n: usize,
        a: Vec<T>,
        id: T,
        op: F,
    }
    #[allow(dead_code)]
    impl<T: Clone, F: Fn(&T, &T) -> T> PURQ<T, F> {
        pub fn new(n: usize, id: T, op: F) -> PURQ<T, F> {
            let mut k = 1;
            while k < n {
                k *= 2;
            }
            PURQ {
                n: k,
                a: vec![id.clone(); 2 * k],
                id: id,
                op: op,
            }
        }
        pub fn update(&mut self, x: usize, v: T) {
            let mut k = self.n + x;
            let a = &mut self.a;
            a[k] = v;
            k >>= 1;
            while k > 0 {
                a[k] = (self.op)(&a[2 * k], &a[2 * k + 1]);
                k >>= 1;
            }
        }
        pub fn update_tmp(&mut self, x: usize, v: T) {
            self.a[x + self.n] = v;
        }
        pub fn update_all(&mut self) {
            let a = &mut self.a;
            for k in (1..(self.n)).rev() {
                a[k] = (self.op)(&a[2 * k], &a[2 * k + 1]);
            }
        }
        pub fn find(&self, mut l: usize, mut r: usize) -> T {
            let mut p = self.id.clone();
            let mut q = self.id.clone();
            l += self.n;
            r += self.n;
            let a = &self.a;
            while l < r {
                if (l & 1) == 1 {
                    p = (self.op)(&p, &a[l]);
                    l += 1;
                }
                if (r & 1) == 1 {
                    r -= 1;
                    q = (self.op)(&a[r], &q);
                }
                l >>= 1;
                r >>= 1;
            }
            (self.op)(&p, &q)
        }
    }
}
// ---------- end SegmentTree Point update Range query ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------

use std::io::Write;

fn run() {
    input! {
        n: usize,
        x: [i64; n],
        q: usize,
        ask: [(usize1, usize, i64); q],
    }
    let mut ask = ask.into_iter().enumerate().map(|(k, (l, r, x))| (l, r, x, k)).collect::<Vec<_>>();
    ask.sort_by_key(|p| p.2);
    let mut x = x.into_iter().enumerate().collect::<Vec<_>>();
    x.sort_by_key(|p| p.1);
    let inf = 10i64.pow(10);
    let mut ans = vec![inf; q];
    {
        let mut seg = segment_tree::PURQ::new(n, -inf, |a, b| std::cmp::max(*a, *b));
        let mut p = 0;
        for &(l, r, y, k) in ask.iter() {
            while p < n && x[p].1 <= y {
                let (pos, x) = x[p];
                p += 1;
                seg.update(pos, x);
            }
            let x = seg.find(l, r);
            ans[k].chmin(y - x);
        }
    }
    {
        let mut seg = segment_tree::PURQ::new(n, inf, |a, b| std::cmp::min(*a, *b));
        let mut p = n;
        for &(l, r, y, k) in ask.iter().rev() {
            while p > 0 && x[p - 1].1 >= y {
                let (pos, x) = x[p- 1];
                p -= 1;
                seg.update(pos, x);
            }
            let x = seg.find(l, r);
            ans[k].chmin(x - y);
        }
    }
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for a in ans {
        writeln!(out, "{}", a).ok();
    }
}

fn main() {
    run();
}
0