結果
問題 | No.1332 Range Nearest Query |
ユーザー | akakimidori |
提出日時 | 2021-01-08 21:54:24 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 207 ms / 2,500 ms |
コード長 | 5,155 bytes |
コンパイル時間 | 13,237 ms |
コンパイル使用メモリ | 383,940 KB |
実行使用メモリ | 26,540 KB |
最終ジャッジ日時 | 2024-11-16 11:21:47 |
合計ジャッジ時間 | 23,170 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 188 ms
20,836 KB |
testcase_04 | AC | 186 ms
20,936 KB |
testcase_05 | AC | 186 ms
20,960 KB |
testcase_06 | AC | 111 ms
24,012 KB |
testcase_07 | AC | 111 ms
23,916 KB |
testcase_08 | AC | 110 ms
23,760 KB |
testcase_09 | AC | 110 ms
23,888 KB |
testcase_10 | AC | 110 ms
24,048 KB |
testcase_11 | AC | 111 ms
23,916 KB |
testcase_12 | AC | 110 ms
23,784 KB |
testcase_13 | AC | 110 ms
23,920 KB |
testcase_14 | AC | 111 ms
24,044 KB |
testcase_15 | AC | 111 ms
24,040 KB |
testcase_16 | AC | 207 ms
26,412 KB |
testcase_17 | AC | 163 ms
26,408 KB |
testcase_18 | AC | 160 ms
26,408 KB |
testcase_19 | AC | 161 ms
26,536 KB |
testcase_20 | AC | 163 ms
26,388 KB |
testcase_21 | AC | 156 ms
26,540 KB |
testcase_22 | AC | 159 ms
26,404 KB |
testcase_23 | AC | 161 ms
26,412 KB |
testcase_24 | AC | 159 ms
26,400 KB |
testcase_25 | AC | 159 ms
26,412 KB |
testcase_26 | AC | 67 ms
23,596 KB |
testcase_27 | AC | 70 ms
23,104 KB |
testcase_28 | AC | 26 ms
8,040 KB |
testcase_29 | AC | 27 ms
8,040 KB |
testcase_30 | AC | 29 ms
8,064 KB |
testcase_31 | AC | 24 ms
8,192 KB |
testcase_32 | AC | 29 ms
8,064 KB |
testcase_33 | AC | 29 ms
8,172 KB |
testcase_34 | AC | 25 ms
8,096 KB |
testcase_35 | AC | 25 ms
8,008 KB |
testcase_36 | AC | 27 ms
8,064 KB |
testcase_37 | AC | 28 ms
8,064 KB |
testcase_38 | AC | 111 ms
16,444 KB |
testcase_39 | AC | 58 ms
9,412 KB |
testcase_40 | AC | 158 ms
26,040 KB |
testcase_41 | AC | 77 ms
10,756 KB |
testcase_42 | AC | 108 ms
16,252 KB |
testcase_43 | AC | 88 ms
12,308 KB |
testcase_44 | AC | 136 ms
19,512 KB |
testcase_45 | AC | 126 ms
18,932 KB |
testcase_46 | AC | 103 ms
13,816 KB |
testcase_47 | AC | 120 ms
17,108 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(); }