CSES - Planet Queries I
Author: Dong Liu
Explanation
Construct a parent matrix (where is the th parent of ). Then, we can answer each query by using binary jumping from the starting node based on the binary representation of the distance.
Since the query distances can be up to , we set the maximum depth to .
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;const int MAXN = 2e5 + 5;const int MAXD = 30; // ceil(log2(10^9))// number of planets and queriesint n, q;// parent matrix where [i][j] corresponds to i's (2^j)th parentint parent[MAXN][MAXD];
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!