結果
問題 | No.1332 Range Nearest Query |
ユーザー | fukafukatani |
提出日時 | 2021-01-09 11:27:08 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,229 bytes |
コンパイル時間 | 16,088 ms |
コンパイル使用メモリ | 377,620 KB |
実行使用メモリ | 36,656 KB |
最終ジャッジ日時 | 2024-11-17 13:37:49 |
合計ジャッジ時間 | 33,759 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 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 | 236 ms
36,524 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 | - |
ソースコード
#![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, 0, |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() }