結果
問題 | No.1332 Range Nearest Query |
ユーザー | fukafukatani |
提出日時 | 2021-01-09 11:29:51 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 631 ms / 2,500 ms |
コード長 | 3,246 bytes |
コンパイル時間 | 12,969 ms |
コンパイル使用メモリ | 377,936 KB |
実行使用メモリ | 40,236 KB |
最終ジャッジ日時 | 2024-11-17 13:41:44 |
合計ジャッジ時間 | 31,873 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 511 ms
29,460 KB |
testcase_04 | AC | 354 ms
29,572 KB |
testcase_05 | AC | 358 ms
29,696 KB |
testcase_06 | AC | 363 ms
40,236 KB |
testcase_07 | AC | 296 ms
39,976 KB |
testcase_08 | AC | 345 ms
39,852 KB |
testcase_09 | AC | 302 ms
39,848 KB |
testcase_10 | AC | 301 ms
39,980 KB |
testcase_11 | AC | 307 ms
40,232 KB |
testcase_12 | AC | 298 ms
39,728 KB |
testcase_13 | AC | 294 ms
39,856 KB |
testcase_14 | AC | 301 ms
39,808 KB |
testcase_15 | AC | 405 ms
39,852 KB |
testcase_16 | AC | 486 ms
39,980 KB |
testcase_17 | AC | 631 ms
40,236 KB |
testcase_18 | AC | 391 ms
39,976 KB |
testcase_19 | AC | 383 ms
39,980 KB |
testcase_20 | AC | 391 ms
39,856 KB |
testcase_21 | AC | 389 ms
39,980 KB |
testcase_22 | AC | 389 ms
39,980 KB |
testcase_23 | AC | 388 ms
39,984 KB |
testcase_24 | AC | 383 ms
39,980 KB |
testcase_25 | AC | 381 ms
39,976 KB |
testcase_26 | AC | 236 ms
39,856 KB |
testcase_27 | AC | 224 ms
40,236 KB |
testcase_28 | AC | 150 ms
6,144 KB |
testcase_29 | AC | 157 ms
6,144 KB |
testcase_30 | AC | 160 ms
6,400 KB |
testcase_31 | AC | 149 ms
6,528 KB |
testcase_32 | AC | 159 ms
6,400 KB |
testcase_33 | AC | 160 ms
6,272 KB |
testcase_34 | AC | 152 ms
6,272 KB |
testcase_35 | AC | 158 ms
6,400 KB |
testcase_36 | AC | 158 ms
6,272 KB |
testcase_37 | AC | 157 ms
6,272 KB |
testcase_38 | AC | 302 ms
22,708 KB |
testcase_39 | AC | 219 ms
6,892 KB |
testcase_40 | AC | 385 ms
39,396 KB |
testcase_41 | AC | 250 ms
10,908 KB |
testcase_42 | AC | 305 ms
22,516 KB |
testcase_43 | AC | 271 ms
15,020 KB |
testcase_44 | AC | 331 ms
26,340 KB |
testcase_45 | AC | 321 ms
25,372 KB |
testcase_46 | AC | 279 ms
17,028 KB |
testcase_47 | AC | 312 ms
24,300 KB |
ソースコード
#![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() }