#![allow(non_snake_case, unused_imports)] use monoid::Max; use proconio::{fastout, input, marker::*}; #[fastout] fn main() { input! { N: usize, mut rR: [(usize, usize); N], } let mut v = vec![0]; for &(r, R) in rR.iter() { v.push(r); v.push(R); } let (z, zi) = coordinate_compression(&v); let mut stree: segment_tree::SegmentTree> = segment_tree::SegmentTree::new(z.len()); rR.sort_by_key(|&(_r, R)| R); for (r, R) in rR { let (_, &k) = zi.range(..=r).next_back().unwrap(); let p = *zi.get(&R).unwrap(); let v = stree.prod(..=k); if stree.get(p) <= v + 1 { stree.update(p, v + 1); } } let ans = N - stree.prod(..); println!("{}", ans); } pub fn coordinate_compression( values: &[T], ) -> (Vec, std::collections::BTreeMap) { let s: std::collections::BTreeSet = values.iter().cloned().collect(); let z: Vec = s.iter().cloned().collect(); let zinv: std::collections::BTreeMap = z.iter().enumerate().map(|(i, &v)| (v, i)).collect(); (z, zinv) } pub mod monoid { pub trait Monoid { type S; // 集合 fn op(lhs: &Self::S, rhs: &Self::S) -> Self::S; // 二項演算 const E: Self::S; // 単位元 } pub struct Add { _marker: std::marker::PhantomData, } pub struct Max { _marker: std::marker::PhantomData, } pub struct Min { _marker: std::marker::PhantomData, } pub struct Update { _marker: std::marker::PhantomData, } macro_rules! impl_primitives { ($($t: ty), *) => { $( impl Monoid for Add<$t> { type S = $t; const E: Self::S = 0; fn op(lhs: &Self::S, rhs: &Self::S) -> Self::S { lhs + rhs } } impl Monoid for Max<$t> { type S = $t; const E: Self::S = <$t>::MIN; fn op(lhs: &Self::S, rhs: &Self::S) -> Self::S { std::cmp::max(*lhs, *rhs) } } impl Monoid for Min<$t> { type S = $t; const E: Self::S = <$t>::MAX; fn op(lhs: &Self::S, rhs: &Self::S) -> Self::S { std::cmp::min(*lhs, *rhs) } } impl Monoid for Update<$t> { type S = $t; const E: Self::S = <$t>::MAX; fn op(_: &Self::S, rhs: &Self::S) -> Self::S { *rhs } } )* }; } impl_primitives!(usize, isize, u128, i128, u64, i64); } pub mod segment_tree { use crate::monoid::Monoid; pub struct SegmentTree { size: usize, tree: Vec, } impl, S: Clone + Copy> SegmentTree { /// self = [e; size] pub fn new(size: usize) -> Self { Self { size, tree: vec![M::E; size << 1], } } /// self = array pub fn from(array: &[M::S]) -> Self { let size = array.len(); let mut tree = vec![M::E; size << 1]; for i in 0..size { tree[i + size] = array[i]; } for i in (1..size).rev() { tree[i] = M::op(&tree[i << 1], &tree[i << 1 | 1]); } return Self { size, tree }; } /// self[i] <- s pub fn update(&mut self, mut i: usize, s: S) { i += self.size; self.tree[i] = s; while i > 1 { i >>= 1; self.tree[i] = M::op(&self.tree[i << 1], &self.tree[i << 1 | 1]); } } /// get self[i] pub fn get(&self, i: usize) -> S { return self.tree[i + self.size]; } /// calculate Π_{i ∈ range} self[i] pub fn prod>(&self, range: R) -> S { let left = match range.start_bound() { std::ops::Bound::Included(&l) => l, std::ops::Bound::Excluded(&l) => l + 1, std::ops::Bound::Unbounded => 0, }; let right = match range.end_bound() { std::ops::Bound::Included(&r) => r + 1, std::ops::Bound::Excluded(&r) => r, std::ops::Bound::Unbounded => self.size, }; return self._prod(left, right); } fn _prod(&self, mut left: usize, mut right: usize) -> S { left += self.size; right += self.size; let (mut sl, mut sr) = (M::E, M::E); while left < right { if left & 1 == 1 { sl = M::op(&sl, &self.tree[left]); left += 1; } if right & 1 == 1 { right -= 1; sr = M::op(&self.tree[right], &sr); } left >>= 1; right >>= 1; } return M::op(&sl, &sr); } } }