Flatten the tree
transcript
A tree of a hundred thousand nodes. Each node has a value. Ask the total under any node. Add up each subtree? Too slow.
There are a hundred thousand questions. Each may add up a hundred thousand nodes. That is ten billion steps in all. A judge runs a hundred million a second.
Number nodes as the search enters them. A subtree is a block of numbers. Then it is an array question.
Six values, in entry order. Node one is a block of three. Its total is eleven. Node two is a block of two.
The block starts at the entry number. Its length is the subtree size. A range sum gives the total.
There is one entry for each node. A query takes about seventeen steps. A change costs about the same.
A tree becomes an array. A subtree becomes a range. Ten billion becomes seventeen. Use the array tools you know. Full breakdown in the lecture.