PrevNext
Somewhat Frequent
 0/26

Binary Jumping

Authors: Benjamin Qi, Neo Wang

Introduces the problems of finding level ancestors in a tree and computing the lowest common ancestors.

Binary Jumping

Focus Problem – read through this problem before continuing!

Tutorial

Pro Tip

Binary jumping is more commonly referred to as "binary lifting."

Solution

To solve this problem, we can use a standard binary lifting implementation where jmp(int x, int d) corresponds to the dd-th ancestor of xx.

In our jmp(int x, int d) if our final value of xx is 00, then such a node does not exist and we can simply return 1-1. This is because the lowest number a node can be is 11 (the root of the tree).

In our implementation, we test if we jump in powers of two by using the &\& operator. If the iith bit on the right is toggled, then we jump. For example, a jump of 1313 would correspond to the binary number 11011101. We would jump 3 times on bits 0,2,30, 2, 3 (in that order) counting from the right.

Illustration of the jump method

To calculate the rows required for the int up[MS][MX] array, use the formula log2N\lceil \log_2{N} \rceil which in our case simplifies to log2(2105)=18\lceil \log_2{(2\cdot 10^5)}\rceil = 18.

Pro Tip

It never hurts to add additional rows - or columns, depending on your implementation - (as long as it's reasonable)!

C++

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORE(i, a, b) for(int i = (a); i <= (b); i++)
#define F0R(i, a) for(int i = 0; i < (a); i++)
#define trav(a, x) for (auto& a : x)
int N, Q;

Problems

StatusSourceProblem NameDifficultyTagsSolutionURL
CSESEasy
Show Tags

Binary Jumping

CSESNormal
Show Tags

Func Graph

POINormal
CFNormalCheck CF
Baltic OINormalExternal Sol
Baltic OINormal
PlatHard
Show Tags

Binary Jumping

External Sol
Baltic OIVery Hard

Lowest Common Ancestor

Focus Problem – read through this problem before continuing!

Focus Problem – read through this problem before continuing!

Tutorial

Implementation

Resources
Benq

This section is not complete.

Any help would be appreciated! Just submit a Pull Request on Github.

Problems

USACO

StatusSourceProblem NameDifficultyTagsSolutionURL
PlatEasy
Show Tags

LCA

External Sol
PlatNormal
Show Tags

LCA

External Sol
PlatHard
Show Tags

LCA

External Sol
PlatHard
Show Tags

Diameter

External Sol
PlatHard
Show Tags

LCA

External Sol
PlatVery Hard
Show Tags

LCA

External Sol

General

StatusSourceProblem NameDifficultyTagsSolutionURL
CFEasy
Show Tags

BinJump

Check CF
CFNormal
Show Tags

LCA

Check CF
Baltic OINormal
CFNormal
Show Tags

LCA

Check CF
CSANormal
Show Tags

LCA

Check CSA
CFNormal
Show Tags

LCA

Check CF
DMOJNormal
Show Tags

LCA

Check DMOJ
TLXHard
Show Tags

LCA

Check TLX
TLXHard
Show Tags

LCA

Check TLX

This section is not complete.

Any help would be appreciated! Just submit a Pull Request on Github.

figure out a better way to order these, difficulties aren't rlly accurate

Module Progress:

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!

PrevNext