結果
問題 | No.1788 Same Set |
ユーザー | akakimidori |
提出日時 | 2022-11-23 01:21:18 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 438 ms / 4,000 ms |
コード長 | 6,557 bytes |
コンパイル時間 | 13,079 ms |
コンパイル使用メモリ | 380,208 KB |
実行使用メモリ | 20,168 KB |
最終ジャッジ日時 | 2024-09-23 08:00:42 |
合計ジャッジ時間 | 25,888 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,944 KB |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 3 ms
7,144 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 158 ms
11,968 KB |
testcase_12 | AC | 226 ms
12,032 KB |
testcase_13 | AC | 257 ms
11,932 KB |
testcase_14 | AC | 314 ms
12,076 KB |
testcase_15 | AC | 356 ms
12,316 KB |
testcase_16 | AC | 384 ms
12,672 KB |
testcase_17 | AC | 314 ms
19,928 KB |
testcase_18 | AC | 371 ms
15,892 KB |
testcase_19 | AC | 231 ms
20,072 KB |
testcase_20 | AC | 216 ms
20,044 KB |
testcase_21 | AC | 367 ms
20,004 KB |
testcase_22 | AC | 387 ms
20,032 KB |
testcase_23 | AC | 342 ms
20,064 KB |
testcase_24 | AC | 376 ms
20,124 KB |
testcase_25 | AC | 427 ms
20,056 KB |
testcase_26 | AC | 429 ms
20,076 KB |
testcase_27 | AC | 263 ms
20,076 KB |
testcase_28 | AC | 438 ms
20,168 KB |
testcase_29 | AC | 418 ms
20,016 KB |
testcase_30 | AC | 270 ms
20,012 KB |
testcase_31 | AC | 418 ms
20,168 KB |
testcase_32 | AC | 375 ms
20,108 KB |
testcase_33 | AC | 375 ms
20,120 KB |
testcase_34 | AC | 404 ms
20,136 KB |
testcase_35 | AC | 394 ms
20,132 KB |
testcase_36 | AC | 391 ms
20,132 KB |
testcase_37 | AC | 392 ms
20,060 KB |
testcase_38 | AC | 382 ms
20,068 KB |
ソースコード
fn main() { input! { n: usize, a: [usize1; n], b: [usize1; n], } let m = *a.iter().chain(b.iter()).max().unwrap() + 1; let mut p = vec![(n, n); m]; let mut seg = LazySegmentTree::build((0..n).map(|_| (0, 1)), n, R); let mut ans = 0u64; for (i, (&a, &b)) in a.iter().zip(b.iter()).enumerate().rev() { let (x, y) = p[a]; seg.update(x.min(y), x.max(y), -1); seg.update(i.min(y), i.max(y), 1); p[a] = (i, y); let (x, y) = p[b]; seg.update(x.min(y), x.max(y), -1); seg.update(x.min(i), x.max(i), 1); p[b] = (x, i); let v = seg.find(i, n); if v.0 == 0 { ans += v.1 as u64; } } println!("{}", ans); } struct R; impl TE for R { type T = (i32, u32); type E = i32; fn fold(&self, 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(&self, x: &Self::T, f: &Self::E) -> Self::T { (x.0 + *f, x.1) } fn merge(&self, g: &Self::E, h: &Self::E) -> Self::E { *g + *h } fn e(&self) -> Self::T { (std::i32::MAX / 2, 0) } fn id(&self) -> Self::E { 0 } } // ---------- 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 Lazy Segment Tree ---------- pub trait TE { type T: Clone; type E: Clone; fn fold(&self, l: &Self::T, r: &Self::T) -> Self::T; fn eval(&self, x: &Self::T, f: &Self::E) -> Self::T; fn merge(&self, g: &Self::E, h: &Self::E) -> Self::E; fn e(&self) -> Self::T; fn id(&self) -> Self::E; } pub struct LazySegmentTree<R: TE> { n: usize, size: usize, bit: u32, op: R, data: Vec<(R::T, R::E)>, } impl<R: TE> LazySegmentTree<R> { pub fn new(n: usize, op: R) -> Self { assert!(n > 0); let size = n.next_power_of_two(); let bit = size.trailing_zeros(); let data = vec![(op.e(), op.id()); 2 * size]; Self { n, size, bit, op, data, } } pub fn build<I>(init: I, n: usize, op: R) -> Self where I: Iterator<Item = R::T>, { let mut seg = Self::new(n, op); for (data, ini) in seg.data[seg.size..].iter_mut().zip(init) { data.0 = ini; } for i in (1..seg.size).rev() { seg.pull(i); } seg } pub fn update(&mut self, l: usize, r: usize, f: R::E) { assert!(l <= r && r <= self.n); if l == r { return; } self.push_range(l, r); let mut s = l + self.size; let mut t = r + self.size; while s < t { if s & 1 == 1 { self.apply(s, &f); s += 1; } if t & 1 == 1 { t -= 1; self.apply(t, &f); } s >>= 1; t >>= 1; } let l = l + self.size; let r = r + self.size; for k in 1..=self.bit { if (l >> k) << k != l { self.pull(l >> k); } if (r >> k) << k != r { self.pull((r - 1) >> k); } } } pub fn find(&mut self, l: usize, r: usize) -> R::T { assert!(l <= r && r <= self.n); if l == r { return self.op.e(); } self.push_range(l, r); let mut l = l + self.size; let mut r = r + self.size; let mut p = self.op.e(); let mut q = self.op.e(); while l < r { if l & 1 == 1 { p = self.op.fold(&p, &self.data[l].0); l += 1; } if r & 1 == 1 { r -= 1; q = self.op.fold(&self.data[r].0, &q); } l >>= 1; r >>= 1; } self.op.fold(&p, &q) } pub fn set_at(&mut self, x: usize, v: R::T) { assert!(x < self.n); let x = x + self.size; for k in (1..=self.bit).rev() { self.push(x >> k); } self.data[x].0 = v; for k in 1..=self.bit { self.pull(x >> k); } } fn push_range(&mut self, l: usize, r: usize) { let l = l + self.size; let r = r + self.size; for k in (1..=self.bit).rev() { if (l >> k) << k != l { self.push(l >> k); } if (r >> k) << k != r { self.push((r - 1) >> k); } } } fn apply(&mut self, x: usize, f: &R::E) { self.data[x].0 = self.op.eval(&self.data[x].0, f); self.data[x].1 = self.op.merge(&self.data[x].1, f); } fn push(&mut self, x: usize) { let f = std::mem::replace(&mut self.data[x].1, self.op.id()); self.apply(2 * x, &f); self.apply(2 * x + 1, &f); } fn pull(&mut self, x: usize) { self.data[x].0 = self.op.fold(&self.data[2 * x].0, &self.data[2 * x + 1].0); } } // ---------- end Lazy Segment Tree ----------