Salary Management at a Car Factory
Time limit1sMemory limit256 MB
Given a company tree with salary updates that add a value to all subordinates of a node and queries for a single employee's current salary, answer efficiently using an Euler tour and a range-update point-query structure.
- Level
Medium6 of 10
- Topics
- Tree, Prefix sum, DFS
- Solved
- No attempts yet
Problem
Sanggeun works at a car factory and, after several promotions, has become its president. The factory has N employees in total. Sanggeun is employee 1; the other employees are numbered from 2 to N. Every employee except Sanggeun has exactly one direct manager.
Each employee can increase or decrease the salaries of all of their subordinates at once. A subordinate means any employee below them in the organization tree, including indirect subordinates, but not the employee themselves.
Given salary-change commands and salary-query commands, write a program that prints the current salary of the requested employee for each query.
Every employee's salary is always a positive integer and at most 2^31 - 1.
Input
The first line contains the number of employees N and the number of commands M. (1 <= N, M <= 500,000)
The next N lines describe the employees. The i-th of these lines describes employee i. Employee 1 has no manager, so only their initial salary is given. For employees 2 through N, the initial salary and the direct manager's number are given.
Each of the next M lines contains one of the following commands.
p a x: Increase the salary of every subordinate of employeeabyx. (-10,000 <= x <= 10,000)u a: Print the current salary of employeea.
Output
For every u command, print the current salary of the requested employee on its own line.