A15400 | MEX Tree Manipulation
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Given a rooted tree, define the value of vertex $u$ in the tree recursively as the MEX $^\dagger$ of the values of its children. Note that it is only the children, not all of its descendants. In particular, the value of a leaf is $0$ .
Pak Chanek has a rooted tree that initially only contains a single vertex with index $1$ , which is the root. Pak Chanek is going to do $q$ queries. In the $i$ -th query, Pak Chanek is given an integer $x_i$ . Pak Chanek needs to add a new vertex with index $i+1$ as the child of vertex $x_i$ . After adding the new vertex, Pak Chanek needs to recalculate the values of all vertices and report the sum of the values of all vertices in the current tree.
$^\dagger$ The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For example, the MEX of $[0,1,1,2,6,7]$ is $3$ and the MEX of $[6,9]$ is $0$ .
Pak Chanek has a rooted tree that initially only contains a single vertex with index $1$ , which is the root. Pak Chanek is going to do $q$ queries. In the $i$ -th query, Pak Chanek is given an integer $x_i$ . Pak Chanek needs to add a new vertex with index $i+1$ as the child of vertex $x_i$ . After adding the new vertex, Pak Chanek needs to recalculate the values of all vertices and report the sum of the values of all vertices in the current tree.
$^\dagger$ The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For example, the MEX of $[0,1,1,2,6,7]$ is $3$ and the MEX of $[6,9]$ is $0$ .
输入格式
The first line contains a single integer $q$ ( $1 \le q \le 3 \cdot 10^5$ ) — the number of operations.
Each of the next $q$ lines contains a single integer $x_i$ ( $1 \leq x_i \leq i$ ) — the description of the $i$ -th query.
Each of the next $q$ lines contains a single integer $x_i$ ( $1 \leq x_i \leq i$ ) — the description of the $i$ -th query.
输出格式
For each query, print a line containing an integer representing the sum of the new values of all vertices in the tree after adding the vertex.
输入输出样例
输入 #1
7 1 1 3 2 5 2 1
输出 #1
1 1 3 2 4 4 7
输入 #2
8 1 1 1 1 5 6 7 8
输出 #2
1 1 1 1 3 2 4 3
In the first example, the tree after the $6$ -th query will look like this.

- Vertex $7$ is a leaf, so its value is $0$ .
- Vertex $6$ is a leaf, so its value is $0$ .
- Vertex $5$ only has a child with value $0$ , so its value is $1$ .
- Vertex $4$ is a leaf, so its value is $0$ .
- Vertex $3$ only has a child with value $0$ , so its value is $1$ .
- Vertex $2$ has children with values $0$ and $1$ , so its value is $2$ .
- Vertex $1$ has children with values $1$ and $2$ , so its value is $0$ .
The sum of the values of all vertices is $0+0+1+0+1+2+0=4$ .

- Vertex $7$ is a leaf, so its value is $0$ .
- Vertex $6$ is a leaf, so its value is $0$ .
- Vertex $5$ only has a child with value $0$ , so its value is $1$ .
- Vertex $4$ is a leaf, so its value is $0$ .
- Vertex $3$ only has a child with value $0$ , so its value is $1$ .
- Vertex $2$ has children with values $0$ and $1$ , so its value is $2$ .
- Vertex $1$ has children with values $1$ and $2$ , so its value is $0$ .
The sum of the values of all vertices is $0+0+1+0+1+2+0=4$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted