Company Culture 2
Time limit5sMemory limit512 MB
Given a tree of boss relations, apply subtree-wide praise additions in real time and answer point total queries.
- Level
Medium5 of 10
- Topics
- Tree, DFS, Prefix sum, Array
- Solved
- No attempts yet
Problem
Yeongseon Corp has a custom called praise flowing down. When a boss praises a direct subordinate, that subordinate passes the same praise on to each of their own direct subordinates. So when an employee is praised, every employee below that employee receives the same praise.
Each praise carries a number for how strong it is, and a subordinate receives that same number.
This time the praise happens in real time. There are two kinds of queries.
1 i w: employee receives praise of strength from their direct boss.2 i: print the total praise employee has received so far.
Given the direct boss relations and the queries, answer every query of type 2.
Input
The first line has the number of employees and the number of queries . () Employees are numbered 1 to .
The second line has the direct boss of employees 1 through in order. A boss number is smaller than the employee's own number. Employee 1 is the president and has no boss, so -1 is given in that position.
Each of the next lines has one query. A type 1 query has the form 1 i w with and . A type 2 query has the form 2 i with . The president has no boss, so the president never receives praise.
Output
For each query of type 2, print on its own line the total praise that employee has received.