結果

問題 No.1332 Range Nearest Query
ユーザー StrorkisStrorkis
提出日時 2021-01-09 19:47:06
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 4,941 bytes
コンパイル時間 13,545 ms
コンパイル使用メモリ 400,152 KB
実行使用メモリ 25,932 KB
最終ジャッジ日時 2024-11-18 10:26:51
合計ジャッジ時間 25,081 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,820 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 72 ms
25,912 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

struct Scanner<R> { r: R, buf: Vec<u8> }

impl<R: std::io::BufRead> Scanner<R> {
    fn new(r: R) -> Scanner<R> {
        Scanner { r, buf: Vec::with_capacity(8000) }
    }

    fn next(&mut self) -> &str {
        self.buf.clear();
        loop {
            let (done, used) = {
                let available = self.r.fill_buf().unwrap();
                match available.iter().position(|b| !b.is_ascii_whitespace()) {
                    Some(i) => (true, i),
                    None => (false, available.len()),
                }
            };
            self.r.consume(used);
            if done || used == 0 { break; }
        }
        loop {
            let (done, used) = {
                let available = self.r.fill_buf().unwrap();
                match available.iter().position(|b| b.is_ascii_whitespace()) {
                    Some(i) => {
                        self.buf.extend_from_slice(&available[..i]);
                        (true, i)
                    }
                    None => {
                        self.buf.extend_from_slice(&available);
                        (false, available.len())
                    }
                }
            };
            self.r.consume(used);
            if done || used == 0 {
                unsafe {
                    return std::str::from_utf8_unchecked(&self.buf);
                }
            }
        }
    }
}

macro_rules! parse {
    ($s:expr, Chars) => ($s.chars().collect::<Vec<_>>());
    ($s:expr, Bytes) => ($s.bytes().collect::<Vec<_>>());
    ($s:expr, Usize1) => (parse!($s, usize) - 1);
    ($s:expr, $t:ty) => ($s.parse::<$t>().unwrap());
}

macro_rules! read {
    ($sc:expr, [$t:tt; $n:expr]) => (
        (0..$n).map(|_| read!($sc, $t)).collect::<Vec<_>>()
    );
    ($sc:expr, ($($t:tt),*)) => (($(read!($sc, $t)),*));
    ($sc:expr, $t:ident) => (parse!($sc.next(), $t));
    ($sc:expr, $($t:tt),*) => (($(read!($sc, $t)),*));
}

// reference: https://github.com/atcoder/ac-library
struct SegmentTree<T> {
    n: usize, len: usize, height: usize,
    op: fn(T, T) -> T, e: fn() -> T,
    node: Vec<T>,
}

impl<T: Clone + Copy> SegmentTree<T> {
    fn new(n: usize, op: fn(T, T) -> T, e: fn() -> T) -> SegmentTree<T> {
        let (mut len, mut height) = (1, 1);
        while len < n {
            len *= 2;
            height += 1;
        }
        let node = vec![e(); 2 * len];
        SegmentTree { n, len, height, op, e, node }
    }

    fn from(v: &[T], op: fn(T, T) -> T, e: fn() -> T) -> SegmentTree<T> {
        let mut st = SegmentTree::new(v.len(), op, e);
        for i in 0..v.len() { st.node[i + st.len] = v[i]; }
        for i in (1..st.len).rev() {
            st.update(i);
        }
        st
    }

    fn update(&mut self, k: usize) {
        self.node[k] = (self.op)(self.node[2 * k], self.node[2 * k + 1]);
    }

    fn set(&mut self, mut p: usize, x: T) {
        assert!(p < self.n);
        p += self.len;
        self.node[p] = x;
        for i in 1..self.height { self.update(p >> i) };
    }

    fn prod(&self, mut l: usize, mut r: usize) -> T {
        assert!(l <= r && r <= self.n);
        let (mut sml, mut smr) = ((self.e)(), (self.e)());
        l += self.len;
        r += self.len;

        while l < r {
            if l & 1 != 0 {
                sml = (self.op)(sml, self.node[l]);
                l += 1;
            }
            if r & 1 != 0 {
                r -= 1;
                smr = (self.op)(self.node[r], smr);
            }
            l >>= 1;
            r >>= 1;
        }

        (self.op)(sml, smr)
    }
}

fn main() {
    use std::io::Write;
    let (stdin, stdout) = (std::io::stdin(), std::io::stdout());
    let mut scanner = Scanner::new(stdin.lock());
    let mut writer = std::io::BufWriter::new(stdout.lock());
    macro_rules! scan {
        ($($t:tt)*) => (read!(scanner, $($t)*));
    }
    macro_rules! println {
        ($($arg:tt)*) => (writeln!(writer, $($arg)*).ok());
    }

    use std::cmp::{max, min};

    let n = scan!(usize);
    let x = scan!([i64; n]);
    let q = scan!(usize);
    let queries = scan!([(Usize1, Usize1, i64); q]);

    let mut low_st = SegmentTree::new(n, |v1, v2| max(v1, v2), || 0);
    let mut high_st = SegmentTree::from(&x, |v1, v2| min(v1, v2), || std::i64::MAX);

    let mut query_order = (0..q).collect::<Vec<_>>();
    query_order.sort_by_key(|&i| queries[i].2);
    let mut x_order = (0..n).collect::<Vec<_>>();
    x_order.sort_by_key(|&i| x[i]);

    let mut ans = vec![0; q];
    let mut k = 0;
    for i in query_order {
        let (l, r, y) = queries[i];
        while k < n && x[x_order[k]] <= y {
            let j = x_order[k];
            low_st.set(j, x[j]);
            high_st.set(j, std::i64::MAX);
            k += 1;
        }
        ans[i] = min(
            y - low_st.prod(l, r + 1),
            high_st.prod(l, r + 1) - y,
        );
    }

    for ans in ans {
        println!("{}", ans);
    }
}
0