Parcel-delivery app~55 min2s · 256 MBtarget O(n + q log² n) time, O(n) spacesegment-tree · tree-recursion
A freight company runs a tree of hubs. Hub 0 is the root, and parents[v] is the hub that feeds hub v. Each hub has a congestion level, values[v] at the start.
Two kinds of events arrive in order. A change sets the congestion level of one hub. A question asks for the highest congestion level on the route from one hub to another, counting both end hubs and every hub between them. Each question must be answered at once.
Write route_peaks(parents, values, ops).
parents.length hubs, numbered from 0. parents[0] is -1, and every other entry is the id of the hub that feeds it.values[v] is the starting congestion level of hub v.ops[i] is [1, v, x] to set the level of hub v to x. It is [2, a, b] to ask for the highest level on the route from hub a to hub b.Input: parents = [-1, 0, 0, 1, 1, 2, 2, 3, 4], values = [4, 7, 2, 9, 5, 1, 8, 3, 6], ops = [[2,7,8],[2,5,6],[1,4,0],[2,7,8],[2,3,5],[1,0,20],[2,8,6],[2,7,7]]
Output: [9, 8, 9, 9, 20, 3]Hubs 1 and 2 report to 0. Hubs 3 and 4 report to 1, hubs 5 and 6 report to 2, hub 7 reports to 3, and hub 8 reports to 4. The route from hub 7 to hub 8 goes 7, 3, 1, 4, 8 with levels 3, 9, 7, 5, 6, so the peak is 9.
The solution splits the tree into chains. Each hub's heavy child is its child with the largest subtree, and a heavy chain follows heavy children. Every route is a few stretches of chains, and a chain is one stretch of a line of places, so a segment tree answers each stretch.
| Hub | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| Subtree size | 9 | 5 | 3 | 2 | 2 | 1 | 1 | 1 | 1 |
| Top of its chain | 0 | 0 | 2 | 3 | 0 | 5 | 2 | 3 | 0 |
| Place in the line | 0 | 1 | 6 | 4 | 2 | 8 | 7 | 5 | 3 |
For the first question, hub 7 has chain top 3, which is deeper than the top 0 of hub 8. So the walk takes places 4 to 5 (hubs 3 and 7, peak 9) and moves to hub 1. Hubs 1 and 8 are now on the same chain, so it takes places 1 to 3 (hubs 1, 4, 8, peak 7). The answer is 9.
Input: parents = [-1], values = [5], ops = [[2,0,0],[1,0,2],[2,0,0]]
Output: [5, 2]A route from a hub to itself is that hub.
Input: parents = [-1, 0, 1, 2], values = [3, 1, 4, 1], ops = [[2,3,0],[2,1,2],[1,2,0],[2,3,0]]
Output: [4, 4, 3]The change sets hub 2 to 0, which lowers the peak of the route from hub 3 to hub 0 to 3.
1 ≤ parents.length ≤ 40,000, parents[0] = -1, and every other entry is a valid hub that makes the hubs a tree rooted at 0values.length = parents.length, and 0 ≤ values[v] ≤ 10^91 ≤ ops.length ≤ 40,000; each change is [1, v, x] with 0 ≤ x ≤ 10^9, each question is [2, a, b], and all hub ids are below parents.lengthClimbing from one hub to the other costs up to n steps, so 40,000 questions on a chain cost about 8 × 10^8 steps or more. Heavy light decomposition cuts every route into at most about 15 chain stretches. Each stretch costs about 16 steps in the segment tree, so a question costs a few hundred steps at most.