tipstar0125 / atcoder

atcoder
6 stars 0 forks source link

ダブリング #17

Open tipstar0125 opened 1 year ago

tipstar0125 commented 1 year ago

https://atcoder.jp/contests/tessoku-book/tasks/tessoku_book_be https://atcoder.jp/contests/tessoku-book/tasks/tessoku_book_ed

tipstar0125 commented 1 year ago

tessoku-book A57

input! {
    N: usize,
    Q: usize,
    A: [Usize1; N],
    XY: [(Usize1, usize); Q]
}

let mut dp = vec![vec![0_usize; N]; 30];
dp[0] = A;

for i in 1..30 {
    for j in 0..N {
        dp[i][j] = dp[i - 1][dp[i - 1][j]];
    }
}

for &(x, y) in &XY {
    let mut pos = x;
    for i in (0..30).rev() {
        if y & (1 << i) > 0 {
            pos = dp[i][pos];
        }
    }
    println!("{}", pos + 1);
}
tipstar0125 commented 1 year ago