K Generations Up — Solution & Editorial
Binary lifting: up[j][v] = 2j-th ancestor, filled by up[j][v] = up[j−1][up[j−1][v]]; decompose K into bits. Naive walking is O(NQ).
Complexity: O((N + Q) log N)
Binary lifting: up[j][v] = 2j-th ancestor, filled by up[j][v] = up[j−1][up[j−1][v]]; decompose K into bits. Naive walking is O(NQ).
Complexity: O((N + Q) log N)