結果
問題 | No.875 Range Mindex Query |
ユーザー | masaki.arizuka |
提出日時 | 2019-09-22 22:35:10 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 112 ms / 2,000 ms |
コード長 | 3,758 bytes |
コンパイル時間 | 16,208 ms |
コンパイル使用メモリ | 379,360 KB |
実行使用メモリ | 6,784 KB |
最終ジャッジ日時 | 2024-09-19 03:46:22 |
合計ジャッジ時間 | 15,605 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 1 ms
5,376 KB |
testcase_09 | AC | 1 ms
5,376 KB |
testcase_10 | AC | 1 ms
5,376 KB |
testcase_11 | AC | 112 ms
6,400 KB |
testcase_12 | AC | 88 ms
5,376 KB |
testcase_13 | AC | 81 ms
6,528 KB |
testcase_14 | AC | 78 ms
6,784 KB |
testcase_15 | AC | 108 ms
6,656 KB |
testcase_16 | AC | 93 ms
6,656 KB |
testcase_17 | AC | 102 ms
6,784 KB |
testcase_18 | AC | 101 ms
6,784 KB |
ソースコード
#[allow(unused_macros)] macro_rules! debug { ($($a:expr),*) => { #[cfg(debug_assertions)] writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*); } } #[allow(unused_imports)] use std::cmp::{min, max}; #[allow(unused_imports)] use std::io::{stdout, stdin, BufWriter, Write}; pub mod ds { pub struct SegmentTree<T, F> { n: usize, pub data: Vec<T>, init: T, f: F, } impl<T, F> SegmentTree<T, F> where T: Copy, F: Fn(T, T) -> T, { pub fn new(size: usize, init: T, f: F) -> Self { let mut n = 1; while n < size { n *= 2; } SegmentTree { n: n, init: init, f: f, data: vec![init; n * 2 - 1], } } pub fn update(&mut self, mut k: usize, x: T) { k += self.n - 1; self.data[k] = (self.f)(self.data[k], x); while k > 0 { k = (k - 1) / 2; self.data[k] = (self.f)(self.data[2 * k + 1], self.data[2 * k + 2]); } } pub fn set(&mut self, mut k: usize, x: T) { k += self.n - 1; self.data[k] = x; while k > 0 { k = (k - 1) / 2; self.data[k] = (self.f)(self.data[2 * k + 1], self.data[2 * k + 2]); } } #[doc = " [l, r)"] pub fn query(&self, l: usize, r: usize) -> T { assert!(l < r); self.do_query(l, r, 0, 0, self.n) } fn do_query(&self, l: usize, r: usize, k: usize, a: usize, b: usize) -> T { if b <= l || r <= a { self.init } else if l <= a && b <= r { self.data[k] } else { let q1 = self.do_query(l, r, k * 2 + 1, a, (a + b) / 2); let q2 = self.do_query(l, r, k * 2 + 2, (a + b) / 2, b); (self.f)(q1, q2) } } } } use ds::SegmentTree; fn main() { let out = std::io::stdout(); let mut out = BufWriter::new(out.lock()); macro_rules! puts { ($($format:tt)*) => (write!(out,$($format)*).unwrap()); } let stdin = std::io::stdin(); let mut sc = Scanner { reader: stdin.lock() }; let n:usize = sc.read(); let q:usize = sc.read(); let aa = sc.read_vec(n); const INF: usize = 1 << 60; let mut seg = SegmentTree::new(n, (INF, n), |a, b| min(a, b)); for i in 0..n { seg.set(i, (aa[i], i)); } for _ in 0..q { let t:usize = sc.read(); let l:usize = sc.read::<usize>()-1; let r:usize = sc.read::<usize>()-1; if t == 1 { let (cur_l, _) = seg.query(l, l+1); let (cur_r, _) = seg.query(r, r+1); seg.set(l, (cur_r, l)); seg.set(r, (cur_l, r)); } else { let (_, idx) = seg.query(l, r+1); puts!("{}\n", idx+1); } } } pub struct Scanner<R> { reader: R, } impl<R: std::io::Read> Scanner<R> { pub fn read<T: std::str::FromStr>(&mut self) -> T { use std::io::Read; let buf: String = self .reader .by_ref() .bytes() .map(|b| b.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect(); buf.parse::<T>().ok().expect("Parse error.") } pub fn read_vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.read()).collect() } pub fn chars(&mut self) -> Vec<char> { self.read::<String>().chars().collect() } }