Graph Theory37 sections · 1633 units
Open in Course

Path Queries - Walkthrough

(Example execution)

Tree: 1241 - 2 - 4, 131 - 3. Values: [4,2,5,1][4, 2, 5, 1]. After DFS: tin = [0, 1, 2, 5], tout = [7, 4, 3, 6]. BIT: arr[0] = +4, arr[7] = -4, arr[1] = +2, arr[4] = -2, arr[2] = +1, arr[3] = -1, arr[5] = +5, arr[6] = -5. Path to node 44: prefix sum to tin[4] = 2 is 4+2+1=74 + 2 + 1 = 7.

Update node 22 to 1010: add delta 88 at positions 11 and 44. Now path to 44 sums to 1515. Breaking paths into these ranges lets you use data structures designed for array operations.