If we keep the dials in a data structure, then to make the solution efficient enough, each update andquery operation will have to run in sub-linear time.One way to achieve this is to group adjacent dials into buckets containing about dials each. Foreach bucket, we keep track of how many times each digit appears in the bucket. The asymptoticrunning time is and the algorithm can pass in time if carefully implemented.By using a different data structure, we can reduce the time to O(log N) per query.Imagine a binary tree in which every leaf represents one dial. Every node in the tree contains 10numbers – how many times each digit appears in dials contained in the subtree rooted at that node. Theroot node of this tree contains information about all the dials. There are about 2·N nodes total.Consider first how to perform a query in this tree. Given an interval [A, B] we want to count how manydials in the interval are displaying each digit. We start from the root node and use the followingrecursive algorithm:
- · If all dials in the subtree of the current node are contained in [A, B], return the 10 numberscontained in this node.
- · If the dials [A, B] are entirely contained in the left or right child of the current node, then move to that node (the other node is of no interest).
- · Otherwise (some of the dials [A, B] are in the left subtree, and some in the right subtree), recursively query both subtrees and add the results together.
This procedure really splits the query interval into subintervals whose lengths decrease exponentially,using as large subintervals as possible. The time complexity is O(log N).To keep the entire tree updated, the update operation would need to be linear, because for example,when Luka clicks all the dials, the entire tree would need to be updated.To remedy this, we can use lazy propagation – when an update operation would update the entiresubtree rooted at some node, don't actually do this, but instead increase a "laziness" counter in thatnode. When another update or query operation would visit one of the node's children, only thenpropagate the counter to the children.The official source code demonstrates this second approach. The update and query operations arecombined in one function.