PROBLEM LINK : Practice , Contest PREREQUISITES : dfs , Segment trees , dp PROBLEM : Given a tree with edges directed towards root and nodes having some ticket information which allows you to travel k units towards the root with cost w . Answer Q queries i.e output the minimum cost for travelling from a node x to root . OBSERVATIONS: Consider the tree to be rooted at 1 , so now every node has a directed edge towards its parent except the root , which means there is a unique path from every node to the root 1. So if we solve the problem for one unique path or one node, we can extend it for other paths as well . EXPLANATION: Lets try to solve the problem for a node X at depth H from the root 1 . So for now forget the tree and think of it as a 1D vector V where you have all the H1 nodes that are between root and node X . The nodes inside the vector are sorted in the order of increasing depth . Lets just denote DP[x] to be the minimum cost of travelling from node X to the root . So now for the given node X , lets just iterate over all the tickets present at node X and DP state will be like this :
Code Explanation : Loop 1 simply iterates over the tickets at x and for a given ticket we can move at max K steps upwards towards the root and as we have already stored those nodes in our vector V , we can iterate over it . so the inner loop moves over those K steps to find the minimum cost . But how can we calculate vector V for every node x ? Here comes DFS in action :
Code Explanation : what we are doing over here is pushing a node u to the vector V when we haven't discovered the subtree of u and pop it when the whole subtree is discovered . Its obvious that u will always lie between the path of any node which is in subtree of u and root . And this also take cares of the sorted depth order . If you aren't getting this , try to draw a few trees . So the final code :
OBSERVATIONS: if we look at the inner loop of the code which goes up to K times and we find the minimum of DP , this can be done using any data structure that supports RMQ + Point updates in O(logn) time . Which will make the total complexity to be O(N + MlogN) and that is cool :P . So what we can do is build a segment tree that supports two operations . First : find the minimum element in range L,R and Second: update an element's value to VAL. And obviously as we need to query for the minimum element between [HK , H] so we will build the tree over height H .
Now just print DP[x] for every query . AC :D . For the actual code refer this . For example : This is the given tree . So this will be our segment tree before DFS . Now , we see for the given tree the longest path between any node and root is of 4 nodes and for computing DP[x] we just need all the DP values of the nodes which lie between x to root i.e for a given height there will be only one DP value . example : for calculating DP[7] , we need DP[1] , DP[2] , DP[4] and DP[1] is at height 1 , DP[2] is at height 2 , DP[4] is at height 3 . So during dfs and updations , at node 7 our segment tree will look like this : Now at node 7 let there be a ticket with k=2 , w=1 . So now we will query for minimum value between height [2,4] and compute our DP[7] . Now 7 has no children and dfs starts rolling back , and we update height 3 in segment tree with infinity . now at node 5 DPs we require are only DP[1] , DP[2] . The scenario of segment tree will be like this : Now Let the ticket available at node 5 be k=3 , w=7 . So now we query over segment tree between [1,3]. asked 15 Sep '16, 19:51

I cannot understand this part  "So what we can do is build a segment tree that supports two operations . First : find the minimum element in range L,R and Second: update an element's value to VAL. And obviously as we need to query for the minimum element between [HK , H] so we will build the tree over height H ." After finding minimum, we should add that value to the front of array. Then again find minimum using this array. But you didn't add any new value? answered 15 Sep '16, 20:06
@dushsingh1995 , we don't need the array this time , we are handling this with the segment tree . See for calculating solution for a node x , we have already updated our tree with DP[all nodes between the path from 1 to x] and thats all what we require . If you will be more specific , i can explain it in more details !
(15 Sep '16, 20:22)

I have split the tree into parts of length $sqrt(N)$. Since the tree is "static", now usual RMQ in 3*O(sqrt(N)) will pass yielding total complexity $O(Nsqrt(N))$. I dont understand how did u build that segment tree, what does over height H mean? answered 15 Sep '16, 20:12
Give me a few mins , i will update it with an example :)
(15 Sep '16, 20:25)
@nidzulandz , updated :)
(15 Sep '16, 21:12)
@arunnsit Got it immidietly with examples! Amazing solution :D
(15 Sep '16, 22:04)

Or we can do a bit easier: to take a minimum with binary lifting. So 1 get instead of update and get of segment tree. (A bit less to write). answered 15 Sep '16, 21:55

Or we can do the same with bin lift, which is for me a bit easier to implement. answered 15 Sep '16, 21:57

I used Sparse Table for calculating the minimum. It supports: Get minimum for range lr in O(1) time. Update the table in O(logn) time. Not for this question but for strict time limits, I would recommend sparse table. Accepted solution: https://www.codechef.com/viewsolution/11382852 answered 15 Sep '16, 22:07

What is wrong with my code? Getting WA on some cases...
SOLVED : INF was too small. Cost me 75 pts. answered 16 Sep '16, 00:34

Mine is also similar to this one but, Insted of doing a segment tree to find out minimum in among the k parents , i am using binary ascent, i store the parents at level 1,2,4,8,16,32,64 like that and minimum cost till then, answered 16 Sep '16, 02:09

Similar solution but used the segment tree storage for as the dp table instead of creating a separate one. :) answered 16 Sep '16, 11:25

Awesome solution bro.Respect _ / \ _ :) answered 16 Sep '16, 14:54

@vishveshcoder Hi,I will try to explain on how the updation works in case of sparse table. If you don't know about sparse table I would recommend you to go through topcoder tutorial first. It is quite easy to understand everything related to sparse table. Still If you are having trouble in understanding then I will explain the main idea behind sparse table For each index 'i' in the array we store the minimum of all continuous subarray of length 2^k(k belongs to whole number {0,1,2,...} ) which start at index 'i' till (i+2^k) is less than n(size of array). Representation is table[i][k] where i is the index and 2^k is the length of the continuous subarray.arr[] is the array containing the elements. For eg: if we are index i = 5 and lets say the size of array is n = 22, then for index i = 5 we will store the following information(this information is only for index i=5, we have to store such type of information for every node) : For k = 0, length = 2^k => length = 1, we store the minimum of subarray of length = 1 starting at index i = 5, which means minimum of index i = 5 only. So we have only one element and minimum of it will be itself only. => (table[5][0] = arr[5]) For k=1, length = 2^k > length = 2, we store the minimum of subarray of length = 2 starting at index i = 5, which means minimum of index i = 5 and i = 6. => (table[5][1] = min(arr[5],arr[6]) ) For k=2, length = 2^k > length = 4, we store the minimum of subarray of length = 4 starting at index i = 5, which means minimum of index i = 5,i = 6,i = 7 and i = 8.=> (table[5][2] = min(arr[5],arr[6],arr[7],arr[8]) ) For k=3, length = 2^k > length = 8, we store the minimum of subarray of length = 8 starting at index i = 5, which means minimum of index i = 5 till i = 12 (from index 5 to 12 we have 8 elements). => (table[5][3] = min(arr[5], ..... ,arr[12]) ) For k=4, length = 2^k > length = 16, we store the minimum of subarray of length = 16 starting at index i = 5, which means minimum of index i = 5 till i = 20 (from index 5 to 20 we have 16 elements). => (table[5][4] = min(arr[5], ..... ,arr[20]) ) For k=5, length = 2^k > length = 32, we store the minimum of subarray of length = 32 starting at index i = 5, which means minimum of index i = 5 till i = 36 => (from index 5 to 36 we have 32 elements). But for the last case k = 5, we can't go till index 36 since size of array is n = 22 only, so we will store the minimum till 22 only for k = 5. => (table[5][5] = min(arr[5], ..... ,arr[22]) ) Now must be wondering on how to store these values, right? Before that, Please understand that for each value of k, we will store answer for each element in the array and then we will increase our K, meaning, for k = 0 I will store the answer for each element and then only I will move on to k = 1. Similarly after I have stored the value(for k = 1) for each element, then only I will increment my k. So with that in mind, now you can move on to an example which will do 90% of the work itself. Let's say you are at index i = 5 and you need to calculate minimum for k = 4(length = 16, from index 5 till 20). Also, we know the answer for k=3 for each element in the array (How? please read the above paragraph once again). So now I will use the value stored for k = 3 to calculate the value for k = 4(which is a basic DP too). For index i=5 and for k=4, the value will be MINIMUM of table[5][3] and table[13][3] . Because table[5][3] stores the minimum for elements from arr[5] till arr[12] and table[13][3] will store the minimum of arr[13] till arr[20] (since from index 13 to 20 we have 8 elements or length = 2^3). So for minimum of arr[5] till arr[20] we can directly use the previous values quite easily and update our value of table[5][4] in O(1) time . In other words we divide our current target subarray (5  20) in two parts and use the previously computed values, since if we are calculating for length = 16 then, we must have computed for length = 8 before. Now for this problem, as we move down the tree from root to some node, I already have the minimum stored for the nodes which come in the path from root to the current node. So for current node we repeat the same procedure as we did above and update the minimum values for each length for current node. If you still have any doubt, then feel free to ask. answered 17 Sep '16, 00:13

@arunnsit Great explanation. Thanks for that. But I had one doubt which I hope you will be able to clear. The complexity you told is O(Nlog(n) + M). What I think is, since log(n) time is spent for each ticket and there are M tickets, it should have been O(N + Mlog(n)). Correct me if I am wrong. answered 20 Sep '16, 11:20
precisely , yes you are right . i mixed variable M with Q . thanks for the correction :)
(21 Sep '16, 00:02)

I solved it using heavy light decomposition. Link to my solution : https://www.codechef.com/viewsolution/11338980 answered 22 Sep '16, 23:38

I would like to say that this is one of the most well written editorials I have ever seen. It was really clever to build the segment Tree on the basis of height and not node. No need for topological sort now. answered 30 Oct '16, 17:34

Awesome explanation with the images, just give a link in official editorial as different approach to the problem