結果
問題 | No.1788 Same Set |
ユーザー | akakimidori |
提出日時 | 2021-12-17 00:41:40 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 493 ms / 4,000 ms |
コード長 | 7,087 bytes |
コンパイル時間 | 20,993 ms |
コンパイル使用メモリ | 386,324 KB |
実行使用メモリ | 28,976 KB |
最終ジャッジ日時 | 2024-09-14 00:41:50 |
合計ジャッジ時間 | 24,920 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,816 KB |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,944 KB |
testcase_08 | AC | 1 ms
6,944 KB |
testcase_09 | AC | 4 ms
7,192 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 57 ms
19,732 KB |
testcase_12 | AC | 125 ms
19,732 KB |
testcase_13 | AC | 157 ms
19,908 KB |
testcase_14 | AC | 238 ms
19,832 KB |
testcase_15 | AC | 312 ms
20,128 KB |
testcase_16 | AC | 371 ms
20,484 KB |
testcase_17 | AC | 359 ms
28,124 KB |
testcase_18 | AC | 435 ms
23,832 KB |
testcase_19 | AC | 264 ms
28,256 KB |
testcase_20 | AC | 77 ms
28,804 KB |
testcase_21 | AC | 329 ms
28,976 KB |
testcase_22 | AC | 356 ms
28,496 KB |
testcase_23 | AC | 274 ms
28,004 KB |
testcase_24 | AC | 327 ms
27,848 KB |
testcase_25 | AC | 450 ms
28,384 KB |
testcase_26 | AC | 426 ms
27,852 KB |
testcase_27 | AC | 93 ms
27,996 KB |
testcase_28 | AC | 493 ms
28,768 KB |
testcase_29 | AC | 416 ms
28,636 KB |
testcase_30 | AC | 90 ms
27,844 KB |
testcase_31 | AC | 439 ms
28,380 KB |
testcase_32 | AC | 377 ms
27,912 KB |
testcase_33 | AC | 400 ms
27,928 KB |
testcase_34 | AC | 449 ms
28,132 KB |
testcase_35 | AC | 448 ms
28,052 KB |
testcase_36 | AC | 440 ms
27,712 KB |
testcase_37 | AC | 436 ms
28,252 KB |
testcase_38 | AC | 427 ms
27,828 KB |
コンパイルメッセージ
warning: unused import: `std::io::Write` --> src/main.rs:185:5 | 185 | use std::io::Write; | ^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
ソースコード
// ---------- begin Lazy Segment Tree ---------- pub trait TE { type T: Clone; type E: Clone; fn fold(l: &Self::T, r: &Self::T) -> Self::T; fn eval(x: &Self::T, f: &Self::E) -> Self::T; fn merge(g: &Self::E, h: &Self::E) -> Self::E; fn e() -> Self::T; fn id() -> Self::E; } pub struct LazySegmentTree<R: TE> { size: usize, bit: usize, a: Vec<(R::T, R::E)>, } impl <R: TE> LazySegmentTree<R> { pub fn new(n: usize) -> LazySegmentTree<R> { let size = n.next_power_of_two(); let bit = size.trailing_zeros() as usize; LazySegmentTree { size: size, bit: bit, a: vec![(R::e(), R::id()); 2 * size], } } pub fn build_by(z: &[R::T]) -> LazySegmentTree<R> { let mut seg = LazySegmentTree::<R>::new(z.len()); for (a, z) in seg.a[seg.size..].iter_mut().zip(z.iter()) { a.0 = z.clone(); } let a = &mut seg.a; for i in (1..seg.size).rev() { a[i].0 = R::fold(&a[2 * i].0, &a[2 * i + 1].0); } seg } fn apply(&mut self, x: usize, op: &R::E) { let node = &mut self.a[x]; node.0 = R::eval(&node.0, op); node.1 = R::merge(&node.1, op); } fn propagate(&mut self, x: usize) { let mut op = R::id(); std::mem::swap(&mut op, &mut self.a[x].1); self.apply(2 * x, &op); self.apply(2 * x + 1, &op); } fn propagate_range(&mut self, l: usize, r: usize) { let x = l + self.size; let y = r + self.size; let mut k = self.bit; while (x >> k) == (y >> k) { self.propagate(x >> k); k -= 1; } for i in ((x.trailing_zeros() as usize + 1)..=k).rev() { self.propagate(x >> i); } for i in ((y.trailing_zeros() as usize + 1)..=k).rev() { self.propagate(y >> i); } } fn save_range(&mut self, l: usize, r: usize) { let mut x = l + self.size; let mut y = r + self.size; let mut p = (x & 1) == 1; let mut q = (y & 1) == 1; x >>= 1; y >>= 1; while 0 < x && x < y { if p { self.a[x].0 = R::fold(&self.a[2 * x].0, &self.a[2 * x + 1].0); } if q { self.a[y].0 = R::fold(&self.a[2 * y].0, &self.a[2 * y + 1].0); } p |= (x & 1) == 1; q |= (y & 1) == 1; x >>= 1; y >>= 1; } while 0 < x { self.a[x].0 = R::fold(&self.a[2 * x].0, &self.a[2 * x + 1].0); x >>= 1; } } pub fn update(&mut self, l: usize, r: usize, op: R::E) { if l == r { return; } self.propagate_range(l, r); let mut x = l + self.size; let mut y = r + self.size; while x < y { if x & 1 == 1 { self.apply(x, &op); x += 1; } if y & 1 == 1 { y -= 1; self.apply(y, &op); } x >>= 1; y >>= 1; } self.save_range(l, r); } pub fn find(&mut self, l: usize, r: usize) -> R::T { if l == r { return R::e(); } self.propagate_range(l, r); let mut x = l + self.size; let mut y = r + self.size; let mut p = R::e(); let mut q = R::e(); while x < y { if x & 1 == 1 { p = R::fold(&p, &self.a[x].0); x += 1; } if y & 1 == 1 { y -= 1; q = R::fold(&self.a[y].0, &q); } x >>= 1; y >>= 1; } R::fold(&p, &q) } } // ---------- end Lazy Segment Tree ---------- // ---------- 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; // 集合が等しい // 左端を固定した時最左の要素にのみ注目 // 区間0の個数とか捌けたらいいができるわけねえ // ある要素について考えると // 右端が s未満or以上 // みたいな条件になる // 区間0の個数クエリになる struct R; impl TE for R { type T = (i32, i64); type E = i32; fn fold(l: &Self::T, r: &Self::T) -> Self::T { if l.0 == r.0 { (l.0, l.1 + r.1) } else { std::cmp::min(*l, *r) } } fn eval(x: &Self::T, f: &Self::E) -> Self::T { (x.0 + *f, x.1) } fn merge(g: &Self::E, h: &Self::E) -> Self::E { *g + *h } fn e() -> Self::T { (0, 0) } fn id() -> Self::E { 0 } } fn run() { input! { n: usize, a: [usize; n], b: [usize; n], } let m = *a.iter().chain(&b).max().unwrap(); let mut range = vec![(n + 1, n + 1); m + 1]; let mut seg = LazySegmentTree::<R>::build_by(&vec![(0, 1); n + 1]); let mut ans = 0; for l in (0..n).rev() { if a[l] == b[l] { let (s, t) = range[a[l]]; seg.update(s.min(t), s.max(t), -1); range[a[l]] = (l, l); } else { let (s, t) = range[a[l]]; seg.update(s.min(t), s.max(t), -1); range[a[l]].0 = l; let (s, t) = range[a[l]]; seg.update(s.min(t), s.max(t), 1); let (s, t) = range[b[l]]; seg.update(s.min(t), s.max(t), -1); range[b[l]].1 = l; let (s, t) = range[b[l]]; seg.update(s.min(t), s.max(t), 1); } let p = seg.find(l, n); if p.0 == 0 { ans += p.1; } } println!("{}", ans); } fn main() { run(); }