Problems > K Generations Up > Editorial

K Generations Up — Solution & Editorial

Back to the Problem

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)