結果

問題 No.1332 Range Nearest Query
ユーザー akakimidoriakakimidori
提出日時 2021-02-15 19:47:06
言語 Rust
(1.72.1)
結果
AC  
実行時間 502 ms / 2,500 ms
コード長 1,963 bytes
コンパイル時間 1,635 ms
コンパイル使用メモリ 151,956 KB
実行使用メモリ 66,576 KB
最終ジャッジ日時 2023-09-30 18:09:15
合計ジャッジ時間 18,683 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 434 ms
48,852 KB
testcase_04 AC 434 ms
48,828 KB
testcase_05 AC 446 ms
48,848 KB
testcase_06 AC 224 ms
66,480 KB
testcase_07 AC 223 ms
66,556 KB
testcase_08 AC 224 ms
66,552 KB
testcase_09 AC 218 ms
66,572 KB
testcase_10 AC 226 ms
66,572 KB
testcase_11 AC 225 ms
66,528 KB
testcase_12 AC 232 ms
66,544 KB
testcase_13 AC 225 ms
66,572 KB
testcase_14 AC 224 ms
66,576 KB
testcase_15 AC 231 ms
66,564 KB
testcase_16 AC 483 ms
66,552 KB
testcase_17 AC 480 ms
66,560 KB
testcase_18 AC 487 ms
66,480 KB
testcase_19 AC 483 ms
66,556 KB
testcase_20 AC 496 ms
66,556 KB
testcase_21 AC 487 ms
66,576 KB
testcase_22 AC 485 ms
66,576 KB
testcase_23 AC 502 ms
66,484 KB
testcase_24 AC 493 ms
66,556 KB
testcase_25 AC 495 ms
66,572 KB
testcase_26 AC 160 ms
66,548 KB
testcase_27 AC 156 ms
66,476 KB
testcase_28 AC 15 ms
5,220 KB
testcase_29 AC 16 ms
5,236 KB
testcase_30 AC 17 ms
5,152 KB
testcase_31 AC 13 ms
5,248 KB
testcase_32 AC 18 ms
5,228 KB
testcase_33 AC 18 ms
5,184 KB
testcase_34 AC 14 ms
5,184 KB
testcase_35 AC 14 ms
5,220 KB
testcase_36 AC 14 ms
5,152 KB
testcase_37 AC 15 ms
5,228 KB
testcase_38 AC 339 ms
35,112 KB
testcase_39 AC 106 ms
6,276 KB
testcase_40 AC 492 ms
65,156 KB
testcase_41 AC 198 ms
13,672 KB
testcase_42 AC 331 ms
34,072 KB
testcase_43 AC 262 ms
21,248 KB
testcase_44 AC 406 ms
44,288 KB
testcase_45 AC 381 ms
41,032 KB
testcase_46 AC 322 ms
25,924 KB
testcase_47 AC 358 ms
38,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn read() -> (Vec<u32>, Vec<(usize, usize, u32)>) {
    use std::io::*;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let mut next = || it.next().unwrap().parse::<u32>().unwrap();
    let n = next();
    let x = (0..n).map(|_| next()).collect::<Vec<_>>();
    let q = next();
    let ask = (0..q).map(|_| {
        let l = next() as usize - 1;
        let r = next() as usize;
        let x = next();
        (l, r, x)
    }).collect::<Vec<_>>();
    (x, ask)
}

fn main() {
    let (x, ask) = read();
    let size = x.len().next_power_of_two();
    let mut seg = vec![vec![]; 2 * size];
    for (seg, x) in seg[size..].iter_mut().zip(x.into_iter()) {
        seg.push(x);
    }
    for i in (1..size).rev() {
        let mut a = Vec::with_capacity(seg[2 * i].len() + seg[2 * i + 1].len());
        a.extend_from_slice(&seg[2 * i]);
        a.extend_from_slice(&seg[2 * i + 1]);
        a.sort();
        a.dedup();
        seg[i] = a;
    }
    let calc = |a: &[u32], x: u32| -> u32 {
        let y = a.binary_search_by_key(&(x, 0), |a| (*a, 1)).unwrap_err();
        let mut ans = std::u32::MAX;
        a.get(y).map(|a| ans = ans.min(*a - x));
        a.get(y - 1).map(|a| ans = ans.min(x - *a));
        ans
    };
    let find = |mut l: usize, mut r: usize, x: u32| -> u32 {
        l += size;
        r += size;
        let mut ans = std::u32::MAX;
        while l < r {
            if l & 1 == 1 {
                ans = ans.min(calc(&seg[l], x));
                l += 1;
            }
            if r & 1 == 1 {
                r -= 1;
                ans = ans.min(calc(&seg[r], x));
            }
            l >>= 1;
            r >>= 1;
        }
        ans
    };
    use std::io::Write;
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for (l, r, x) in ask {
        writeln!(out, "{}", find(l, r, x)).ok();
    }
}
0