Path Sum
You are given a tree with n nodes (numbered from 1 to n), node 1 is the root of tree.Input Format
Each node has a value say Vi is the value of i'th node.
You have to perform q queries of two types:-
1 i v : change the value of i'th node to v,
2 i : add all nodes on path from root to i'th node.
First line contains single integer n.Output Format
second line contains n integers v1 v2 v3 .... vn
third line contains n-1 integers p2 p3 p4 ... pn here pi is the parent of i'th node
fourth line contains single int q
q following lines contains queries of type 1 and 2
of every query of type 2 print sum of values.Question Video Constraints
1. 1 <= N <= 10^5Sample Input
2. 1 <= Vi <= 10^4
3. 1 <= q <= 10^4
8Sample Output
4 7 9 5 2 1 3 5
1 1 1 4 3 4 1
7
1 4 3
1 3 8
2 7
1 4 6
1 1 7
2 3
2 4
10
15
13
-
Asked in Companies
-
Related Topics
Video Solution
Code Solution
{ }
{ }
Run