結果

問題 No.2543 Many Meetings
ユーザー haihamabossuhaihamabossu
提出日時 2023-11-24 22:43:34
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 7,808 bytes
コンパイル時間 13,790 ms
コンパイル使用メモリ 386,672 KB
実行使用メモリ 115,068 KB
最終ジャッジ日時 2024-09-26 09:40:50
合計ジャッジ時間 19,531 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 203 ms
114,660 KB
testcase_04 AC 202 ms
114,652 KB
testcase_05 AC 210 ms
114,596 KB
testcase_06 AC 207 ms
114,500 KB
testcase_07 AC 205 ms
114,500 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 106 ms
53,120 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 207 ms
99,500 KB
testcase_19 AC 188 ms
89,724 KB
testcase_20 AC 184 ms
90,268 KB
testcase_21 AC 224 ms
105,940 KB
testcase_22 AC 177 ms
87,932 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 37 ms
24,340 KB
testcase_32 AC 135 ms
78,692 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 WA -
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 1 ms
5,376 KB
testcase_39 WA -
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod lowest_common_ancestor {

    pub struct LowestCommonAncestor {
        pub ancestor: Vec<Vec<Option<usize>>>,
    }

    impl LowestCommonAncestor {
        pub fn new(tree: &crate::tree::Tree) -> LowestCommonAncestor {
            let n = tree.n;
            let m = (n as f64).log2().ceil() as usize;
            let mut ancestor = vec![vec![None; m]; n];
            for u in 0..n {
                ancestor[u][0] = tree.parent[u];
            }
            for k in 1..m {
                for u in 0..n {
                    if let Some(v) = ancestor[u][k - 1] {
                        ancestor[u][k] = ancestor[v][k - 1];
                    }
                }
            }
            LowestCommonAncestor { ancestor }
        }

        pub fn lca(&self, tree: &crate::tree::Tree, u: usize, v: usize) -> usize {
            let mut u = u;
            let mut v = v;
            if tree.depth[u] < tree.depth[v] {
                std::mem::swap(&mut u, &mut v);
            }
            let m = self.ancestor[0].len();
            for k in (0..m).rev() {
                if let Some(w) = self.ancestor[u][k] {
                    if tree.depth[w] >= tree.depth[v] {
                        u = w;
                    }
                }
            }
            if u == v {
                return u;
            }
            for k in (0..m).rev() {
                if let (Some(pu), Some(pv)) = (self.ancestor[u][k], self.ancestor[v][k]) {
                    if pu != pv {
                        u = pu;
                        v = pv;
                    }
                }
            }
            self.ancestor[u][0].unwrap()
        }
    }
}

pub mod scanner {

    pub struct Scanner {
        buf: Vec<String>,
    }

    impl Scanner {
        pub fn new() -> Self {
            Self { buf: vec![] }
        }

        pub fn new_from(source: &str) -> Self {
            let source = String::from(source);
            let buf = Self::split(source);
            Self { buf }
        }

        pub fn next<T: std::str::FromStr>(&mut self) -> T {
            loop {
                if let Some(x) = self.buf.pop() {
                    return x.parse().ok().expect("");
                }
                let mut source = String::new();
                std::io::stdin().read_line(&mut source).expect("");
                self.buf = Self::split(source);
            }
        }

        fn split(source: String) -> Vec<String> {
            source
                .split_whitespace()
                .rev()
                .map(String::from)
                .collect::<Vec<_>>()
        }
    }
}

pub mod tree {

    #[derive(Clone)]
    pub struct Edge {
        pub to: usize,

        pub id: usize,

        pub rev: usize,

        pub cost: isize,
    }

    pub struct Tree {
        pub n: usize,

        root: usize,

        pub edges: Vec<Vec<Edge>>,

        pub parent: Vec<Option<usize>>,

        pub depth: Vec<usize>,

        pub subtree_size: Vec<usize>,

        pub pre_order: Vec<usize>,

        pub post_order: Vec<usize>,

        pub euler_tour: Vec<usize>,
    }

    impl Tree {
        pub fn new(n: usize) -> Tree {
            Tree {
                n,
                root: 0,
                edges: vec![vec![]; n],
                parent: vec![None; n],
                depth: vec![0; n],
                subtree_size: vec![0; n],
                pre_order: vec![],
                post_order: vec![],
                euler_tour: vec![],
            }
        }

        pub fn init(scanner: &mut crate::scanner::Scanner, indexed: usize, weighted: bool) -> Tree {
            let n: usize = scanner.next();
            let mut tree = Tree::new(n);
            for id in 0..n - 1 {
                let u: usize = scanner.next::<usize>() - indexed;
                let v: usize = scanner.next::<usize>() - indexed;
                let cost: isize;
                if weighted {
                    cost = scanner.next();
                } else {
                    cost = 1;
                }
                tree.add_edge(u, v, id, cost);
            }
            tree.set_root(0);
            tree
        }

        pub fn add_edge(&mut self, u: usize, v: usize, id: usize, cost: isize) {
            {
                let to = v;
                let rev = self.edges[v].len();
                self.edges[u].push(Edge { to, id, rev, cost });
            }
            {
                let to = u;
                let rev = self.edges[u].len() - 1;
                self.edges[v].push(Edge { to, id, rev, cost });
            }
        }

        pub fn degree(&self, u: usize) -> usize {
            self.edges[u].len()
        }

        pub fn set_root(&mut self, root: usize) {
            let n = self.n;
            self.root = root;
            self.parent = vec![None; n];
            self.depth = vec![0; n];
            self.subtree_size = vec![0; n];
            self.pre_order = vec![];
            self.post_order = vec![];
            self.euler_tour = vec![];
            let mut stack: Vec<(usize, usize, usize)> = vec![];
            stack.push((root, 0, 0));
            while let Some((u, i, d)) = stack.pop() {
                if i == 0 {
                    self.subtree_size[u] += 1;
                    self.pre_order.push(u);
                }
                if i == self.degree(u) {
                    if let Some(p) = self.parent[u] {
                        self.subtree_size[p] += self.subtree_size[u];
                    }
                    self.post_order.push(u);
                    self.euler_tour.push(u);
                    continue;
                }
                stack.push((u, i + 1, d));
                let edge = &self.edges[u][i];
                if Some(edge.to) == self.parent[u] {
                    continue;
                }
                self.parent[edge.to] = Some(u);
                self.depth[edge.to] = d + 1;
                self.euler_tour.push(u);
                stack.push((edge.to, 0, d + 1));
            }
        }
    }
}

use crate::{lowest_common_ancestor::LowestCommonAncestor, scanner::Scanner, tree::Tree};
use std::{collections::VecDeque, io::Write};
fn main() {
    let mut scanner = Scanner::new();
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    let t: usize = 1;
    for _ in 0..t {
        solve(&mut scanner, &mut out);
    }
}
fn solve(scanner: &mut Scanner, out: &mut std::io::BufWriter<std::io::StdoutLock>) {
    let n = scanner.next::<usize>();
    let k = scanner.next::<usize>();
    let mut lr = (0..n)
        .map(|_| (scanner.next::<usize>(), scanner.next::<usize>()))
        .collect::<Vec<_>>();
    lr.sort_by_cached_key(|a| (a.1, a.0));
    let mut que = VecDeque::new();
    let mut tree = Tree::new(n);
    {
        let mut id = 0;
        for i in 0..n {
            let (l, r) = lr[i];
            while let Some(&(pr, pi)) = que.front() {
                if l < pr {
                    break;
                }
                que.pop_front();
                tree.add_edge(i, pi, id, 1);
                id += 1;
            }
            que.push_back((r, i));
        }
    }
    tree.set_root(n - 1);
    let lca = LowestCommonAncestor::new(&tree);
    let inf = 1e18 as usize;
    let mut ans = inf;
    for u in 0..n {
        if tree.depth[u] < k - 1 {
            continue;
        }
        let mut rem = k - 1;
        let mut j = 0;
        let mut v = u;
        while rem > 0 {
            if (rem & 1) == 1 {
                v = lca.ancestor[v][j].unwrap();
            }
            rem >>= 1;
            j += 1;
        }
        ans = ans.min(lr[v].1 - lr[u].0);
    }
    let ans = if ans == inf { -1 } else { ans as isize };
    writeln!(out, "{}", ans).unwrap();
}
0