Company Culture 4
InterviewTime limit2sMemory limit512 MB
On a rooted tree, praise spreads downward from an employee to all descendants or upward to all ancestors, the direction flips over time, and queries ask for an employee's accumulated praise.
- Level
Hard8 of 10
- Topics
- Tree, Prefix sum, DFS, Implementation
- Solved
- No attempts yet
Problem
Yeongseon's company praises downward. When a boss praises a direct subordinate, that subordinate's own subordinates receive praise of the same size, and it keeps spreading to the bottom of the hierarchy.
Yeongseon, the president, flips the direction in which praise spreads during the workday. The starting direction is the subordinate direction.
- Subordinate direction: when an employee receives praise, that employee and every subordinate under them each receive praise.
- Boss direction: when an employee receives praise, that employee and every boss above them each receive praise.
Praise arrives in real time. There are three kinds of queries.
1 i w: if the current direction is the subordinate direction, employee receives praise from the direct boss. If it is the boss direction, employee receives praise from one of the direct subordinates. Either way the praise spreads in the current direction. (, )2 i: print the total praise employee has received so far.3: flip the direction of praise. The subordinate direction becomes the boss direction, and the boss direction becomes the subordinate direction.
Given the direct boss of every employee and the list of queries, print the answer for every query of type 2.
Input
The first line has the number of employees and the number of queries . Employees are numbered 1 through . ()
The second line has the number of the direct boss of employee 1 through employee , in that order. The number of a direct boss is smaller than the employee's own number, and employee 1 is the president. Employee 1 has no boss, so -1 is given instead.
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.