結果

問題 No.2325 Skill Tree
ユーザー haihamabossuhaihamabossu
提出日時 2023-05-28 14:41:15
言語 Rust
(1.77.0)
結果
AC  
実行時間 482 ms / 3,000 ms
コード長 3,617 bytes
コンパイル時間 2,041 ms
コンパイル使用メモリ 152,564 KB
実行使用メモリ 21,116 KB
最終ジャッジ日時 2023-08-27 10:21:03
合計ジャッジ時間 18,353 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 218 ms
4,380 KB
testcase_08 AC 129 ms
8,548 KB
testcase_09 AC 241 ms
6,432 KB
testcase_10 AC 154 ms
13,176 KB
testcase_11 AC 232 ms
9,792 KB
testcase_12 AC 423 ms
17,972 KB
testcase_13 AC 426 ms
18,172 KB
testcase_14 AC 421 ms
17,984 KB
testcase_15 AC 424 ms
17,972 KB
testcase_16 AC 421 ms
17,960 KB
testcase_17 AC 425 ms
17,968 KB
testcase_18 AC 426 ms
17,980 KB
testcase_19 AC 423 ms
17,920 KB
testcase_20 AC 423 ms
17,924 KB
testcase_21 AC 420 ms
17,980 KB
testcase_22 AC 416 ms
17,992 KB
testcase_23 AC 413 ms
17,932 KB
testcase_24 AC 410 ms
17,924 KB
testcase_25 AC 409 ms
18,132 KB
testcase_26 AC 410 ms
17,968 KB
testcase_27 AC 474 ms
21,092 KB
testcase_28 AC 477 ms
20,844 KB
testcase_29 AC 476 ms
21,080 KB
testcase_30 AC 482 ms
21,048 KB
testcase_31 AC 477 ms
21,048 KB
testcase_32 AC 459 ms
20,772 KB
testcase_33 AC 466 ms
21,104 KB
testcase_34 AC 463 ms
21,116 KB
testcase_35 AC 472 ms
20,784 KB
testcase_36 AC 479 ms
21,096 KB
testcase_37 AC 475 ms
20,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod chminmax {
    pub fn chmin<T: Copy + Ord>(a: &mut T, b: &T) -> bool {
        if *a > *b {
            *a = *b;
            true
        } else {
            false
        }
    }

    pub fn chmax<T: Copy + Ord>(a: &mut T, b: &T) -> bool {
        if *a < *b {
            *a = *b;
            true
        } else {
            false
        }
    }
}

pub mod lower_bound {
    pub fn lower_bound<T: Ord>(list: &[T], val: &T) -> usize {
        let mut l = 0;
        let mut r = list.len();
        while l < r {
            let x = (l + r) / 2;
            match list[x].cmp(val) {
                std::cmp::Ordering::Less => {
                    l = x + 1;
                }
                std::cmp::Ordering::Equal | std::cmp::Ordering::Greater => {
                    r = x;
                }
            }
        }
        l
    }

    pub fn upper_bound<T: Ord>(list: &[T], val: &T) -> usize {
        let mut l = 0;
        let mut r = list.len();
        while l < r {
            let x = (l + r) / 2;
            match list[x].cmp(val) {
                std::cmp::Ordering::Less | std::cmp::Ordering::Equal => {
                    l = x + 1;
                }
                std::cmp::Ordering::Greater => {
                    r = x;
                }
            }
        }
        l
    }
}

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<_>>()
        }
    }
}

use std::collections::VecDeque;

use crate::{chminmax::chmax, lower_bound::upper_bound, scanner::Scanner};

fn main() {
    let mut scanner = Scanner::new();
    let t: usize = 1;
    for _ in 0..t {
        solve(&mut scanner);
    }
}

fn solve(scanner: &mut Scanner) {
    let n: usize = scanner.next();
    let mut g = vec![vec![]; n];
    for v in 2..=n {
        let l: usize = scanner.next();
        let u: usize = scanner.next();
        g[u - 1].push((v - 1, l));
    }
    let mut que = VecDeque::new();
    que.push_back(0);
    let mut res1: Vec<(usize, usize)> = vec![(1, 1)];
    let mut res2: Vec<isize> = vec![-1; n];
    res2[0] = 1;
    while let Some(u) = que.pop_front() {
        for &(v, l) in g[u].iter() {
            if res2[u] == -1 {
                continue;
            }
            let now = l.max(res2[u] as usize) as isize;
            chmax(&mut res2[v], &now);
            res1.push((res2[v] as usize, v));
            que.push_back(v);
        }
    }
    res1.sort();
    let q: usize = scanner.next();
    for _ in 0..q {
        let t: usize = scanner.next();
        if t == 1 {
            let x: usize = scanner.next();
            println!("{}", upper_bound(&res1, &(x, !0)));
        } else {
            let y: usize = scanner.next();
            println!("{}", res2[y - 1]);
        }
    }
}
0