Given a rooted tree and not necessarily binary. The tree contains N nodes, labeled 1 to N. You are given the tree in the form of an array A[1..N] of size N. A[i] denotes label of the parent of node labeled i. For clarity, you may assume that the tree satisfies the following conditions.
- The root of the tree is labeled 1. Hence A is set to 0.
- The parent of node T will always have a label less than T.
The task is to perform the following operations according to the type of query given.
- ADD, X, Y: add Y to the value at node X.
- ADDUP, X, Y: add Y to the value at node X. Then, add Y to the value at A[X] (i.e. the parent of X). The, add Y to the value at A[A[X]] (i.e. the parent of A[X]).. and so on, till you add Y to the value at root.
After you have performed all the given operations, you are asked to answer several queries of the following type
- VAL, X: print the value at node X.
- VALTREE, X: print the sum of values at all nodes in the subtree rooted at X (including X).
Source: Directi Interview | Set 13
N = 7, M = 4, Q = 5
0 1 2 2 2 1 2
ADD 6 76
ADDUP 1 49
ADD 4 48
ADDUP 2 59
N = 5, M = 5, Q = 3
0 1 1 1 3
ADD 1 10
ADD 2 20
ADD 3 30
ADD 4 40
ADDUP 5 50
Explanation: This problem is a slight variation of dfs. In this, we have stored the node’s original value and addup value in the vector of the pair. We did 2 times dfs.
- dfs1 for offline queries i.e to calculate the addup sum for each node.
- dfs2 to store the subtree sum in an array.
Now all the queries can be answered in a constant time.
Graph before dfs1
Graph after dfs1
Below is the required implementation:
291 0 0 107 59
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Lowest Common Ancestor for a Set of Nodes in a Rooted Tree
- Construct the Rooted tree by using start and finish time of its DFS traversal
- Perform append, update, delete and range sum queries on the given array
- Queries for DFS of a subtree in a tree
- Find GCD of each subtree of a given node in an N-ary Tree for Q queries
- Find parent of each node in a tree for multiple queries
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Find the path from root to the given nodes of a tree for multiple queries
- Find distance of nodes from root in a tree for multiple queries
- Count of distinct colors in a subtree of a Colored Tree with given min frequency for Q queries
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Binary Indexed Tree : Range Update and Range Queries
- Perform DDoS attack using Torshammer
- Perform n steps to convert every digit of a number in the format [count][digit]
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Cartesian tree from inorder traversal | Segment Tree
- Convert a Binary Tree into its Mirror Tree
- SQL | Top-N Queries
- XOR of a submatrix queries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sanjeev2552