結果
問題 | No.1332 Range Nearest Query |
ユーザー | akakimidori |
提出日時 | 2021-01-08 21:54:24 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 159 ms / 2,500 ms |
コード長 | 5,155 bytes |
コンパイル時間 | 10,906 ms |
コンパイル使用メモリ | 389,024 KB |
実行使用メモリ | 26,540 KB |
最終ジャッジ日時 | 2024-04-28 02:47:06 |
合計ジャッジ時間 | 19,159 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 0 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 159 ms
20,836 KB |
testcase_04 | AC | 158 ms
20,924 KB |
testcase_05 | AC | 153 ms
20,828 KB |
testcase_06 | AC | 107 ms
23,912 KB |
testcase_07 | AC | 107 ms
24,044 KB |
testcase_08 | AC | 82 ms
23,920 KB |
testcase_09 | AC | 84 ms
23,892 KB |
testcase_10 | AC | 84 ms
23,792 KB |
testcase_11 | AC | 83 ms
23,988 KB |
testcase_12 | AC | 83 ms
24,012 KB |
testcase_13 | AC | 84 ms
23,832 KB |
testcase_14 | AC | 83 ms
23,916 KB |
testcase_15 | AC | 113 ms
23,916 KB |
testcase_16 | AC | 130 ms
26,408 KB |
testcase_17 | AC | 130 ms
26,276 KB |
testcase_18 | AC | 133 ms
26,540 KB |
testcase_19 | AC | 131 ms
26,532 KB |
testcase_20 | AC | 129 ms
26,408 KB |
testcase_21 | AC | 131 ms
26,400 KB |
testcase_22 | AC | 128 ms
26,372 KB |
testcase_23 | AC | 131 ms
26,536 KB |
testcase_24 | AC | 131 ms
26,404 KB |
testcase_25 | AC | 128 ms
26,408 KB |
testcase_26 | AC | 53 ms
23,640 KB |
testcase_27 | AC | 57 ms
23,104 KB |
testcase_28 | AC | 21 ms
8,064 KB |
testcase_29 | AC | 22 ms
8,064 KB |
testcase_30 | AC | 23 ms
8,192 KB |
testcase_31 | AC | 20 ms
8,064 KB |
testcase_32 | AC | 26 ms
8,064 KB |
testcase_33 | AC | 25 ms
8,064 KB |
testcase_34 | AC | 22 ms
8,064 KB |
testcase_35 | AC | 21 ms
8,136 KB |
testcase_36 | AC | 21 ms
8,020 KB |
testcase_37 | AC | 23 ms
7,936 KB |
testcase_38 | AC | 96 ms
16,408 KB |
testcase_39 | AC | 52 ms
9,476 KB |
testcase_40 | AC | 127 ms
26,048 KB |
testcase_41 | AC | 62 ms
10,632 KB |
testcase_42 | AC | 89 ms
16,220 KB |
testcase_43 | AC | 77 ms
12,308 KB |
testcase_44 | AC | 113 ms
19,636 KB |
testcase_45 | AC | 105 ms
18,952 KB |
testcase_46 | AC | 84 ms
13,816 KB |
testcase_47 | AC | 100 ms
17,060 KB |
ソースコード
// ---------- 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(); }