結果
問題 | No.2220 Range Insert & Point Mex |
ユーザー | akakimidori |
提出日時 | 2023-02-18 00:40:34 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 133 ms / 2,000 ms |
コード長 | 6,459 bytes |
コンパイル時間 | 11,463 ms |
コンパイル使用メモリ | 402,032 KB |
実行使用メモリ | 24,192 KB |
最終ジャッジ日時 | 2024-07-21 12:56:38 |
合計ジャッジ時間 | 15,822 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,812 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,940 KB |
testcase_10 | AC | 1 ms
6,944 KB |
testcase_11 | AC | 1 ms
6,944 KB |
testcase_12 | AC | 1 ms
6,944 KB |
testcase_13 | AC | 82 ms
20,180 KB |
testcase_14 | AC | 83 ms
21,104 KB |
testcase_15 | AC | 81 ms
21,444 KB |
testcase_16 | AC | 78 ms
19,964 KB |
testcase_17 | AC | 81 ms
20,620 KB |
testcase_18 | AC | 82 ms
20,996 KB |
testcase_19 | AC | 84 ms
20,076 KB |
testcase_20 | AC | 132 ms
24,064 KB |
testcase_21 | AC | 133 ms
23,416 KB |
testcase_22 | AC | 127 ms
24,192 KB |
testcase_23 | AC | 55 ms
21,404 KB |
testcase_24 | AC | 48 ms
16,768 KB |
testcase_25 | AC | 36 ms
14,076 KB |
testcase_26 | AC | 53 ms
19,696 KB |
testcase_27 | AC | 37 ms
13,496 KB |
testcase_28 | AC | 10 ms
6,940 KB |
testcase_29 | AC | 12 ms
6,944 KB |
testcase_30 | AC | 11 ms
6,940 KB |
testcase_31 | AC | 43 ms
19,684 KB |
testcase_32 | AC | 36 ms
14,500 KB |
testcase_33 | AC | 35 ms
14,680 KB |
testcase_34 | AC | 103 ms
21,772 KB |
testcase_35 | AC | 105 ms
20,944 KB |
testcase_36 | AC | 102 ms
20,332 KB |
testcase_37 | AC | 103 ms
21,844 KB |
testcase_38 | AC | 57 ms
20,200 KB |
ソースコード
use std::io::Write; fn run() { input! { n: usize, p: [(usize1, usize, usize); n], q: usize, x: [usize1; q], } let mut z = vec![0]; for p in p.iter() { z.push(p.2); z.push(p.2 + 1); } z.sort(); z.dedup(); let mut seg = SegmentTreePURQ::new(z.len(), 100000i32, |a, b| std::cmp::min(*a, *b)); for i in 0..z.len() { seg.update_tmp(i, 0); } seg.update_all(); let mut event = vec![]; event.extend(p.iter().map(|p| (p.0, 1, p.2))); event.extend(p.iter().map(|p| (p.1, -1, p.2))); event.extend((0..q).map(|k| (x[k], 2, k))); event.sort(); let mut ans = vec![0; q]; for (_, v, x) in event { if v == 2 { ans[x] = seg.max_right(0, |v| *v > 0); } else { let pos = z.binary_search(&x).unwrap(); seg.get_mut(pos, |node| *node += v); } } 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(); } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] 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_export] 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_export] 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 ---------- // ---------- begin segment tree Point Update Range Query ---------- pub struct SegmentTreePURQ<T, F> { n: usize, size: usize, data: Vec<T>, e: T, op: F, } impl<T, F> SegmentTreePURQ<T, F> where T: Clone, F: Fn(&T, &T) -> T, { pub fn new(n: usize, e: T, op: F) -> Self { assert!(n > 0); let size = n.next_power_of_two(); let data = vec![e.clone(); 2 * size]; SegmentTreePURQ { n, size, data, e, op, } } pub fn update_tmp(&mut self, x: usize, v: T) { assert!(x < self.n); self.data[x + self.size] = v; } pub fn update_all(&mut self) { for i in (1..self.size).rev() { self.data[i] = (self.op)(&self.data[2 * i], &self.data[2 * i + 1]); } } pub fn update(&mut self, x: usize, v: T) { assert!(x < self.n); let mut x = x + self.size; self.data[x] = v; x >>= 1; while x > 0 { self.data[x] = (self.op)(&self.data[2 * x], &self.data[2 * x + 1]); x >>= 1; } } pub fn get_mut<G>(&mut self, x: usize, mut g: G) where G: FnMut(&mut T) { assert!(x < self.n); let mut x = x + self.size; g(&mut self.data[x]); x >>= 1; while x > 0 { self.data[x] = (self.op)(&self.data[2 * x], &self.data[2 * x + 1]); x >>= 1; } } pub fn find(&self, l: usize, r: usize) -> T { assert!(l <= r && r <= self.n); if l == r { return self.e.clone(); } let mut l = self.size + l; let mut r = self.size + r; let mut x = self.e.clone(); let mut y = self.e.clone(); while l < r { if l & 1 == 1 { x = (self.op)(&x, &self.data[l]); l += 1; } if r & 1 == 1 { r -= 1; y = (self.op)(&self.data[r], &y); } l >>= 1; r >>= 1; } (self.op)(&x, &y) } pub fn max_right<P>(&self, l: usize, f: P) -> usize where P: Fn(&T) -> bool, { assert!(l <= self.n); assert!(f(&self.e)); if l == self.n { return self.n; } let mut l = l + self.size; let mut sum = self.e.clone(); while { l >>= l.trailing_zeros(); let v = (self.op)(&sum, &self.data[l]); if !f(&v) { while l < self.size { l <<= 1; let v = (self.op)(&sum, &self.data[l]); if f(&v) { sum = v; l += 1; } } return l - self.size; } sum = v; l += 1; l.count_ones() > 1 } {} self.n } pub fn min_left<P>(&self, r: usize, f: P) -> usize where P: Fn(&T) -> bool, { assert!(r <= self.n); assert!(f(&self.e)); if r == 0 { return 0; } let mut r = r + self.size; let mut sum = self.e.clone(); while { r -= 1; while r > 1 && r & 1 == 1 { r >>= 1; } let v = (self.op)(&self.data[r], &sum); if !f(&v) { while r < self.size { r = 2 * r + 1; let v = (self.op)(&self.data[r], &sum); if f(&v) { sum = v; r -= 1; } } return r + 1 - self.size; } sum = v; (r & (!r + 1)) != r } {} 0 } } // ---------- end segment tree Point Update Range Query ----------