結果

問題 No.1332 Range Nearest Query
ユーザー fukafukatanifukafukatani
提出日時 2021-01-09 11:29:51
言語 Rust
(1.77.0)
結果
AC  
実行時間 455 ms / 2,500 ms
コード長 3,246 bytes
コンパイル時間 14,829 ms
コンパイル使用メモリ 381,004 KB
実行使用メモリ 40,364 KB
最終ジャッジ日時 2024-04-28 18:51:06
合計ジャッジ時間 32,996 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 404 ms
29,588 KB
testcase_04 AC 412 ms
29,444 KB
testcase_05 AC 406 ms
29,572 KB
testcase_06 AC 329 ms
40,240 KB
testcase_07 AC 329 ms
39,848 KB
testcase_08 AC 326 ms
39,980 KB
testcase_09 AC 455 ms
39,852 KB
testcase_10 AC 324 ms
39,980 KB
testcase_11 AC 324 ms
40,104 KB
testcase_12 AC 329 ms
39,852 KB
testcase_13 AC 322 ms
39,828 KB
testcase_14 AC 332 ms
39,848 KB
testcase_15 AC 329 ms
39,848 KB
testcase_16 AC 439 ms
39,856 KB
testcase_17 AC 451 ms
40,108 KB
testcase_18 AC 434 ms
39,964 KB
testcase_19 AC 451 ms
40,108 KB
testcase_20 AC 443 ms
39,984 KB
testcase_21 AC 440 ms
39,984 KB
testcase_22 AC 443 ms
40,108 KB
testcase_23 AC 455 ms
39,980 KB
testcase_24 AC 432 ms
40,108 KB
testcase_25 AC 450 ms
40,104 KB
testcase_26 AC 236 ms
39,852 KB
testcase_27 AC 242 ms
40,364 KB
testcase_28 AC 165 ms
6,272 KB
testcase_29 AC 169 ms
6,272 KB
testcase_30 AC 168 ms
6,272 KB
testcase_31 AC 159 ms
6,400 KB
testcase_32 AC 171 ms
6,272 KB
testcase_33 AC 171 ms
6,272 KB
testcase_34 AC 163 ms
6,528 KB
testcase_35 AC 163 ms
6,400 KB
testcase_36 AC 164 ms
6,144 KB
testcase_37 AC 166 ms
6,400 KB
testcase_38 AC 327 ms
22,832 KB
testcase_39 AC 230 ms
6,900 KB
testcase_40 AC 431 ms
39,292 KB
testcase_41 AC 268 ms
10,912 KB
testcase_42 AC 336 ms
22,384 KB
testcase_43 AC 288 ms
14,888 KB
testcase_44 AC 370 ms
26,340 KB
testcase_45 AC 356 ms
25,376 KB
testcase_46 AC 309 ms
17,016 KB
testcase_47 AC 367 ms
24,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;

fn main() {
    let n = read::<usize>();
    let x = read_vec::<i64>();
    let q = read::<usize>();
    let mut queries = vec![];
    for i in 0..q {
        let v = read_vec::<i64>();
        let (l, r, x) = (v[0] as usize - 1, v[1] as usize - 1, v[2]);
        queries.push((l, r, x, i));
    }
    queries.sort_by_key(|e| -e.2);

    let mut xx = vec![];
    for i in 0..n {
        xx.push((x[i], i));
    }
    xx.sort();
    xx.reverse();

    let mut answers = vec![std::i64::MAX; q];

    let mut seg = SegTree::new(n, std::i64::MAX, |a, b| min(a, b));
    let mut xq = xx.iter().cloned().collect::<VecDeque<_>>();
    for &(l, r, x, i) in &queries {
        while let Some(&(xqx, xqi)) = xq.iter().next() {
            if xqx < x {
                break;
            }
            seg.update(xqi, xqx);
            xq.pop_front();
        }
        answers[i] = min(answers[i], seg.query(l, r + 1) - x);
    }

    queries.reverse();
    xx.reverse();
    let mut seg = SegTree::new(n, -std::i64::MAX / 2, |a, b| max(a, b));
    let mut xq = xx.iter().cloned().collect::<VecDeque<_>>();
    for &(l, r, x, i) in &queries {
        while let Some(&(xqx, xqi)) = xq.iter().next() {
            if xqx > x {
                break;
            }
            seg.update(xqi, xqx);
            xq.pop_front();
        }
        answers[i] = min(answers[i], x - seg.query(l, r + 1));
    }

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

#[derive(Clone)]
struct SegTree<T, F>
where
    F: Fn(T, T) -> T,
    T: std::clone::Clone + std::marker::Copy,
{
    n: usize,
    dat: Vec<T>,
    init: T,
    functor: F,
}

impl<T, F> SegTree<T, F>
where
    F: Fn(T, T) -> T,
    T: std::clone::Clone + std::marker::Copy,
{
    fn new(n: usize, init: T, f: F) -> SegTree<T, F> {
        let mut m = 1;
        // For simplicity, we use 2 ** n sized SegTree.
        while m < n {
            m *= 2;
        }
        SegTree {
            n: m,
            dat: vec![init; 2 * m - 1],
            init: init,
            functor: f,
        }
    }

    // dat[k] = a;
    fn update(&mut self, k: usize, a: T) {
        let mut k = k;
        k += self.n - 1;
        self.dat[k] = a;
        while k > 0 {
            k = (k - 1) / 2;
            self.dat[k] = (self.functor)(self.dat[k * 2 + 1], self.dat[k * 2 + 2]);
        }
    }

    // [a, b)
    fn query(&self, a: usize, b: usize) -> T {
        self.query_inner(a, b, 0, 0, self.n)
    }

    fn query_inner(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T {
        if r <= a || b <= l {
            return self.init;
        }
        if a <= l && r <= b {
            return self.dat[k];
        }

        let vl = self.query_inner(a, b, k * 2 + 1, l, (l + r) / 2);
        let vr = self.query_inner(a, b, k * 2 + 2, (l + r) / 2, r);
        (self.functor)(vl, vr)
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
0