Company Culture 3
Time limit2sMemory limit512 MB
Employees sit in a rooted tree. A praise of w given to employee i from a subordinate adds w to i and every ancestor up to the president; type 2 queries ask an employee's running total.
- Level
Medium5 of 10
- Topics
- Tree, DFS, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
Yeongseon's company praises from the top down. When a manager praises a direct subordinate, the praise cascades to every employee below that subordinate. Yeongseon, the president, decided to reverse the direction for exactly one day. When a subordinate praises a manager, that manager and every employee above, all the way up to the president, receive the same amount of praise.
Praise arrives in real time as queries of two kinds.
1 i w: employee i receives praise of amount from one direct subordinate. That is added to employee i and to every manager above employee i. (, ) An employee with no direct subordinate never appears in this query.2 i: print the total praise employee i has received so far. ()
Input
The first line has the number of employees and the number of queries . Employees are numbered 1 through . ()
The second line has the direct manager of employee 1 through employee , in order. A manager's number is always smaller than the employee's own number. Employee 1 is the president and has no manager, so -1 is given in that position.
Each of the next lines has one query.
Output
For every query of type 2, print the total praise that employee has received, one per line.