Skip to content

Commit

Permalink
tessoku-book a57
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Nov 22, 2022
1 parent 27a3e22 commit 9240271
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions tessoku-book/src/bin/a57.rs
Expand Up @@ -3,8 +3,31 @@ use proconio::{input, marker::Usize1};
fn main() {
input! {
n: usize,
q: usize,
a: [Usize1; n],
xy: [(Usize1, usize); q],
};
let ans = n - a.len();
println!("{}", ans);

let m = 30;
let mut dp = vec![vec![0_usize; n]; m + 1];
for (i, a_i) in a.iter().copied().enumerate() {
dp[0][i] = a_i;
}
for i in 1..=m {
for j in 0..n {
dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
}

for (x, y) in xy {
let mut cur = x;
let mut cnt = y;
for dp_i in dp.iter() {
if (cnt & 1) != 0 {
cur = dp_i[cur];
}
cnt >>= 1;
}
println!("{}", cur + 1);
}
}

0 comments on commit 9240271

Please sign in to comment.