E. May Holidays

Codeforces
IDCF966E
Time5000ms
Memory256MB
Difficulty
data structurestrees
English · Original
Chinese · Translation
Formal · Original
It's May in Flatland, and there are $m$ days in this month. Despite the fact that May Holidays are canceled long time ago, employees of some software company still have a habit of taking short or long vacations in May. Of course, not all managers of the company like this. There are $n$ employees in the company that form a tree-like structure of subordination: each employee has a unique integer id $i$ between $1$ and $n$, and each employee with id $i$ (except the head manager whose id is 1) has exactly one direct manager with id $p_i$. The structure of subordination is not cyclic, i.e. if we start moving from any employee to his direct manager, then we will eventually reach the head manager. We define that an employee $u$ is a subordinate of an employee $v$, if $v$ is a direct manager of $u$, or the direct manager of $u$ is a subordinate of $v$. Let $s_i$ be the number of subordinates the $i$\-th employee has (for example, $s_1 = n - 1$, because all employees except himself are subordinates of the head manager). Each employee $i$ has a bearing limit of $t_i$, which is an integer between $0$ and $s_i$. It denotes the maximum number of the subordinates of the $i$\-th employee being on vacation at the same moment that he can bear. If at some moment strictly more than $t_i$ subordinates of the $i$\-th employee are on vacation, and the $i$\-th employee himself is not on a vacation, he becomes _displeased_. In each of the $m$ days of May exactly one event of the following two types happens: either one employee leaves on a vacation at the beginning of the day, or one employee returns from a vacation in the beginning of the day. You know the sequence of events in the following $m$ days. Your task is to compute for each of the $m$ days the number of displeased employees on that day. ## Input The first line contains two integers $n$ and $m$ ($2 \leq n, m \leq 10^5$) — the number of employees in the company and the number of days in May. The second line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i \leq n$), denoting the direct managers of employees. The third line contains $n$ integers $t_1, t_2, \ldots, t_n$ ($0 \leq t_i \leq s_i$), denoting the bearing limits of empoyees. The fourth line contains $m$ integers $q_1, q_2, \ldots, q_m$ ($1 \leq |q_i| \leq n$, $q_i \ne 0$), denoting the events. If $q_i$ is positive, then the employee with id $q_i$ leaves for a vacation starting from this day, if $q_i$ is negative, then the employee $-q_i$ returns from a vacation starting from this day. In the beginning of May no employee is on vacation. It is guaranteed that if some employee leaves for a vacation, he is not on a vacation at the moment and vice versa. ## Output Print a sequence of $m$ integers $a_1, a_2, \ldots, a_m$, where $a_i$ is the number of displeased employees on the $i$\-th day. [samples] ## Note In the first sample test after employee with id 2 leaves for a vacation at the first day, the head manager with id 1 becomes displeased as he does not want any of his subordinates to go for a vacation. At the fourth day employee with id 5 becomes displeased as his last remaining employee with id 7 leaves for a vacation. At the fifth day employee with id 2 returns from the vacation, but it does not affect the number of displeased employees as the employees 5 and 1 are still displeased. At the sixth day employee with id 3 returns back from the vacation, preventing the employee with id 5 from being displeased and at the last day the head manager with id 1 leaves for a vacation, leaving the company without the displeased people at all.
现在是扁平国的五月,这个月有 $m$ 天。尽管五月假期早已被取消,但某软件公司的员工仍保留着在五月休假的习惯。 当然,并非公司所有管理者都喜欢这样。公司中有 $n$ 名员工,形成了一种树状的隶属结构:每个员工都有一个唯一的整数编号 $i$(介于 $1$ 到 $n$ 之间),每个编号为 $i$ 的员工(除编号为 1 的首席管理者外)恰好有一个直接上级,其编号为 $p_i$。隶属结构无环,即从任意员工开始,不断向上追溯其直接上级,最终必定到达首席管理者。我们定义:员工 $u$ 是员工 $v$ 的下属,当且仅当 $v$ 是 $u$ 的直接上级,或 $u$ 的直接上级是 $v$ 的下属。设 $s_i$ 表示第 $i$ 个员工的下属数量(例如,$s_1 = n -1$,因为除他自己外的所有员工都是首席管理者的下属)。 每个员工 $i$ 有一个承受上限 $t_i$,是一个介于 $0$ 和 $s_i$ 之间的整数,表示他能忍受的同时休假的下属的最大数量。如果在某一时刻,员工 $i$ 的下属中恰好有超过 $t_i$ 人正在休假,且员工 $i$ 本人没有休假,则他将变得 _不满_。 在五月的 $m$ 天中,每天恰好发生以下两种事件之一:要么一名员工在当天开始时开始休假,要么一名员工在当天开始时结束休假返回公司。你已知接下来 $m$ 天的事件序列。你的任务是计算每一天中不满员工的数量。 第一行包含两个整数 $n$ 和 $m$($2 \leq n, m \leq 10^5$)——公司员工数量和五月的天数。 第二行包含 $n -1$ 个整数 $p_2, p_3, \dots, p_n$($1 \leq p_i \leq n$),表示各员工的直接上级。 第三行包含 $n$ 个整数 $t_1, t_2, \dots, t_n$($0 \leq t_i \leq s_i$),表示各员工的承受上限。 第四行包含 $m$ 个整数 $q_1, q_2, \dots, q_m$($1 \leq |q_i| \leq n$,$q_i \ne 0$),表示事件。若 $q_i$ 为正,则编号为 $q_i$ 的员工从当天开始休假;若 $q_i$ 为负,则编号为 $-q_i$ 的员工从当天开始结束休假返回公司。五月开始时,没有任何员工在休假。保证:若某员工开始休假,则他当前未在休假;反之亦然。 请输出一个长度为 $m$ 的整数序列 $a_1, a_2, \dots, a_m$,其中 $a_i$ 表示第 $i$ 天不满员工的数量。 在第一个样例中,第一天员工 2 开始休假后,首席管理者(编号 1)变得不满,因为他不希望任何下属休假。第四天,员工 5 变得不满,因为他的最后一个仍在职的下属(编号 7)开始休假。第五天,员工 2 结束休假返回,但不影响不满员工数量,因为员工 5 和 1 仍处于不满状态。第六天,员工 3 结束休假返回,使员工 5 不再不满;最后一天,首席管理者(编号 1)开始休假,公司中不再有任何不满员工。 ## Input 第一行包含两个整数 $n$ 和 $m$($2 \leq n, m \leq 10^5$)——公司员工数量和五月的天数。第二行包含 $n -1$ 个整数 $p_2, p_3, \dots, p_n$($1 \leq p_i \leq n$),表示各员工的直接上级。第三行包含 $n$ 个整数 $t_1, t_2, \dots, t_n$($0 \leq t_i \leq s_i$),表示各员工的承受上限。第四行包含 $m$ 个整数 $q_1, q_2, \dots, q_m$($1 \leq |q_i| \leq n$,$q_i \ne 0$),表示事件。若 $q_i$ 为正,则编号为 $q_i$ 的员工从当天开始休假;若 $q_i$ 为负,则编号为 $-q_i$ 的员工从当天开始结束休假返回公司。五月开始时,没有任何员工在休假。保证:若某员工开始休假,则他当前未在休假;反之亦然。 ## Output 请输出一个长度为 $m$ 的整数序列 $a_1, a_2, \dots, a_m$,其中 $a_i$ 表示第 $i$ 天不满员工的数量。 [samples] ## Note 在第一个样例中,第一天员工 2 开始休假后,首席管理者(编号 1)变得不满,因为他不希望任何下属休假。第四天,员工 5 变得不满,因为他的最后一个仍在职的下属(编号 7)开始休假。第五天,员工 2 结束休假返回,但不影响不满员工数量,因为员工 5 和 1 仍处于不满状态。第六天,员工 3 结束休假返回,使员工 5 不再不满;最后一天,首席管理者(编号 1)开始休假,公司中不再有任何不满员工。
**Definitions** Let $ n \in \mathbb{Z} $ be the number of employees, and $ m \in \mathbb{Z} $ the number of days. Let $ T = (V, E) $ be a rooted tree with vertex set $ V = \{1, 2, \dots, n\} $, where vertex $ 1 $ is the root (head manager). For each $ i \in \{2, \dots, n\} $, let $ p_i \in V \setminus \{i\} $ denote the direct manager of employee $ i $. Let $ s_i = |\{ j \in V \mid j \text{ is a subordinate of } i \}| $ be the number of subordinates of employee $ i $. Let $ t_i \in \mathbb{Z} $ with $ 0 \le t_i \le s_i $ be the bearing limit of employee $ i $. Let $ v: V \times \{1, \dots, m\} \to \{0, 1\} $ be a state function where $ v(i, d) = 1 $ if employee $ i $ is on vacation on day $ d $, and $ 0 $ otherwise. Initial state: $ v(i, 0) = 0 $ for all $ i \in V $. Let $ q = (q_1, \dots, q_m) \in (\mathbb{Z} \setminus \{0\})^m $ be the sequence of events, where: - If $ q_d > 0 $, then $ v(q_d, d) = 1 $ and $ v(j, d) = v(j, d-1) $ for all $ j \ne q_d $. - If $ q_d < 0 $, then $ v(-q_d, d) = 0 $ and $ v(j, d) = v(j, d-1) $ for all $ j \ne -q_d $. For each employee $ i \in V $ and day $ d \in \{1, \dots, m\} $, define: $$ c_i(d) = \sum_{\substack{j \in \text{subordinates}(i) \\ v(j, d) = 1}} 1 $$ as the number of vacationing subordinates of $ i $ on day $ d $. Employee $ i $ is **displeased** on day $ d $ if: $$ v(i, d) = 0 \quad \text{and} \quad c_i(d) > t_i $$ **Constraints** 1. $ 2 \le n, m \le 10^5 $ 2. $ 1 \le p_i \le n $, $ p_i \ne i $ for all $ i \in \{2, \dots, n\} $ 3. $ 0 \le t_i \le s_i $ for all $ i \in \{1, \dots, n\} $ 4. $ 1 \le |q_d| \le n $, $ q_d \ne 0 $ for all $ d \in \{1, \dots, m\} $ 5. The subordination structure forms a tree rooted at $ 1 $, with no cycles. 6. Events are consistent: an employee leaves only if not on vacation, returns only if on vacation. **Objective** For each day $ d \in \{1, \dots, m\} $, compute: $$ a_d = \left| \left\{ i \in V \mid v(i, d) = 0 \text{ and } c_i(d) > t_i \right\} \right| $$
Samples
Input #1
7 8
4 5 1 1 5 5
0 0 0 1 2 0 0
2 6 3 7 -2 4 -3 1
Output #1
1 1 1 2 2 2 1 0
Input #2
5 6
1 2 3 4
4 0 0 1 0
1 5 2 3 -5 -1
Output #2
0 2 1 0 0 0
API Response (JSON)
{
  "problem": {
    "name": "E. May Holidays",
    "description": {
      "content": "It's May in Flatland, and there are $m$ days in this month. Despite the fact that May Holidays are canceled long time ago, employees of some software company still have a habit of taking short or long",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 5000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF966E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "It's May in Flatland, and there are $m$ days in this month. Despite the fact that May Holidays are canceled long time ago, employees of some software company still have a habit of taking short or long...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "现在是扁平国的五月,这个月有 $m$ 天。尽管五月假期早已被取消,但某软件公司的员工仍保留着在五月休假的习惯。\n\n当然,并非公司所有管理者都喜欢这样。公司中有 $n$ 名员工,形成了一种树状的隶属结构:每个员工都有一个唯一的整数编号 $i$(介于 $1$ 到 $n$ 之间),每个编号为 $i$ 的员工(除编号为 1 的首席管理者外)恰好有一个直接上级,其编号为 $p_i$。隶属结构无环,即从任意员...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ be the number of employees, and $ m \\in \\mathbb{Z} $ the number of days.  \nLet $ T = (V, E) $ be a rooted tree with vertex set $ V = \\{1, 2, \\dots, n\\} $, wh...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments